Insert table column value in the Php Session
Hello, in my mysql database I have table called users which has primary
key column called userID which is Auto Increment column. I would like to
get inside the session array the value of this column for the logged in
user, here is how the array looks like right now:
array(4) { ["loggedIn"]=> bool(true) ["username"]=> string(9) "user"
["password"]=> string(3) "123" ["userID"]=> NULL }
This is the code of my login form:
session_start();
$usr = new Users;
$usr->storeFormValues( $_POST );
if( $usr->userLogin() ) {
header( 'Location: cursos.php' ) ;
$_SESSION["loggedIn"] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['userID'] = $_POST['userID'];
and this is my login function:
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 * FROM users WHERE username = :username AND 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->execute();
$valid = $stmt->fetchColumn();
if( $valid ) {
$success = true;
}
$con = null;
return $success;
How this can be acheived ? If anyone can help I would appreciate it. Thank
you.
No comments:
Post a Comment