Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use sessions in PDO?
    primarykey
    data
    text
    <p>I am still redoing and getting rid of old mysql_* commands in my code. I tried to transfer my session login form old code and this is what I got so far:</p> <pre><code> public function login($user, $password) { if (!empty($user) &amp;&amp; !empty($password)) { $password = $web-&gt;doHash($user, $password); // in this function is (return sha1(strtoupper($user).':'.strtoupper($password)) $stmt = $db_login-&gt;prepare("SELECT * FROM account WHERE username=:user AND pass_hash=:password"); $stmt-&gt;bindValue(':user', $user, PDO::PARAM_STR); $stmt-&gt;bindValue(':password', $password, PDO::PARAM_STR); $stmt-&gt;execute(); $rows = $stmt-&gt;rowCount(); if ($rows &gt; 0) { $results_login = $stmt-&gt;fetch(PDO::FETCH_ASSOC); $_SESSION['user_name'] = $results_login['username']; $_SESSION['user_id'] = $results_login['id']; return true; } else { return false; } } else { return false; } } </code></pre> <p>After that I am using checks if user logged on site:</p> <pre><code> public function isLogged() { return (!empty($_SESSION['user_id']) &amp;&amp; !empty($_SESSION['user_name'])); } </code></pre> <p>But it seems - this function returns always empty because $_SESSION does not exists in PDO? And of course logout is used in this form on my sites:</p> <pre><code> public function logout() { unset($_SESSION['user_id']); unset($_SESSION['user_name']); } </code></pre> <p>But I think PDO has different way of handling session? I did not find any so what is it can i somehow add $_SESSION in PDO withou changing code much? </p> <p>I am using variables <code>$_SESSION['user_name']</code> and <code>$_SESSION['user_id']</code> in all over my web project.</p> <p>Summary:</p> <p>1) How to use sessions in PDO correctly?</p> <p>2) What is difference between using <code>$stmt-&gt;fetch(PDO::FETCH_ASSOC);</code> and <code>$stmt-&gt;fetchAll();</code></p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload