PHP and MySQL Acces Levels -
i have been trying add simple "access" level check, , can not give me out value database, null; though same query user, pass check.
anyhow, here code, might able little better done!
*updated according comment
public function userlogin() { $success = false; try { $con = new pdo(db_dsn, db_username, db_password); $con->setattribute(pdo::attr_errmode, pdo::errmode_exception); $sql = "select * users username = :username , password = :password limit 1"; $stmt = $con->prepare($sql); $stmt->bindvalue(":username", $this->username, pdo::param_str); $stmt->bindvalue(":password", hash("sha256", $this->password . $this->salt), pdo::param_str); // $stmt->bindvalue("access", $this->access, pdo::param_int); $stmt->execute(); $valid = $stmt->fetchcolumn(); if ($valid) { $success = true; session_start(); $_session['username'] = $this->username; } $con = null; return $success; } catch (pdoexception $e) { echo $e->getmessage(); return $success; } } public function auth() { $con = new pdo(db_dsn, db_username, db_password); $con->setattribute(pdo::attr_errmode, pdo::errmode_exception); $sql = "select access users access = :1 or access = :2"; $stmt = $con->prepare($sql); $stmt->bindvalue(":access", $this->access, pdo::param_int); $stmt->execute(); $access = $stmt->fetchcolumn(); if ($access == 1) { session_start(); $_session['isadmin'] = $this->access; } if ($access == 2) { session_start(); $_session['isuser'] = $this->access; } }
i have got file called "headerauth.php" little div block has welcome $_session['username'] in works, , test/developing reasons var_dump @ end, gives result :
array 'username' => string 'test' (length=4)
when had auth in same block userlogin function, value used
null;
there @ least 3 mistakes in code:
$stmt->bindvalue > pdostatement::bindvalue expects first parameter either integer (for question mark statement parameters) or string (for named parameters). if using named parameters must begin colon! example
$stmt->bindvalue(":username", $this->username, pdo::param_str);
session_start($_session) > session_start not expect parameters (void)
$stmt->bindvalue("access", $this->access, pdo::param_int) > there no named parameter access in sql query
exception should thrown there.
do have custom exception handler / display_errors off / error_reporting off? not understand, why no exception thrown..
Comments
Post a Comment