Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP and MySQL Acces Levels
    text
    copied!<p>I have been trying to add a simple "access" level check, and I can not get it to give me out the value from the Database, I always get Null; even though it is almost the same query as for the User, Pass check.</p> <p>Anyhow, here is my code, you might be able to get it even a little better done!</p> <p><strong>*Updated According to Comment</strong></p> <pre><code> public function userLogin() { $success = false; try { $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); $con-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1"; $stmt = $con-&gt;prepare($sql); $stmt-&gt;bindValue(":username", $this-&gt;username, PDO::PARAM_STR); $stmt-&gt;bindValue(":password", hash("sha256", $this-&gt;password . $this-&gt;salt), PDO::PARAM_STR); // $stmt-&gt;bindValue("access", $this-&gt;access, PDO::PARAM_INT); $stmt-&gt;execute(); $valid = $stmt-&gt;fetchColumn(); if ($valid) { $success = true; session_start(); $_SESSION['username'] = $this-&gt;username; } $con = null; return $success; } catch (PDOException $e) { echo $e-&gt;getMessage(); return $success; } } public function auth() { $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); $con-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT access FROM users WHERE access = :1 OR access = :2"; $stmt = $con-&gt;prepare($sql); $stmt-&gt;bindValue(":access", $this-&gt;access, PDO::PARAM_INT); $stmt-&gt;execute(); $access = $stmt-&gt;fetchColumn(); if ($access == 1) { session_start(); $_SESSION['isAdmin'] = $this-&gt;access; } if ($access == 2) { session_start(); $_SESSION['isUser'] = $this-&gt;access; } } </code></pre> <p>I have got another file called "headerauth.php" it is a little DIV block that has a Welcome $_SESSION['username'] in it that works, and for test/developing reasons a Var_Dump at the end, which gives this result :</p> <blockquote> <p>array 'username' => string 'test' (length=4)</p> </blockquote> <p>When I had the Auth in the same block as the userLogin function, the value used to be </p> <blockquote> <p>Null;</p> </blockquote>
 

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