Jump to content

Pdo Class Function To $_Session['userid']


aljosa

Recommended Posts

Hi PHPFreaks,

I'm banging my head with problem how could I initialize $_SESSION['userid'] on user login with MYSQL field "userid" value. I'll then use this session value from "userid" for users content identification for SQL publish and retrive from/to MySQL table.

 

I have Class:

 

<?php
class Users {
public $userid = null;
public $username = null;
public $email = null;
public $password = null;
// Escape dirty
public function __construct( $data = array() ) {
if( isset( $data['userid'] ) ) $this->userid = stripslashes( strip_tags( $data['userid'] ) );
if( isset( $data['username'] ) ) $this->username = stripslashes( strip_tags( $data['username'] ) );
if( isset( $data['email'] ) ) $this->email = stripslashes( strip_tags( $data['email'] ) );
if( isset( $data['password'] ) ) $this->password = stripslashes( strip_tags( $data['password'] ) );
}
// Add form variables to
public function storeFormValues( $params ) {
//store the parameters
$this->__construct( $params );
}
// User login
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 email = :email AND password = :password AND active IS NULL LIMIT 1";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "email", $this->email, PDO::PARAM_STR );
$stmt->bindValue( "password", $this->password, PDO::PARAM_STR );
$stmt->execute();
$valid = $stmt->fetchColumn();
if( $valid ) {
$success = true;
}
$con = null;
return $success;
} catch ( PDOException $e ) {
echo $e->getMessage();
return $success;
}
}
?>

 

and login.php

 

<?php

if(isset($_SESSION['stat']) == 1) :
header('Location:index.html');
else:

include_once('includes/config.php');

$errs = '';


if( !(isset( $_POST['login'] ) ) ) {
} else {
$usr = new Users;
$usr->storeFormValues( $_POST );

if( $_POST['email'] == "" || $_POST['password'] == "" ) {
$errs = '<p class="error">All fields are required.</p>';

} else {

if( $usr->userLogin() ) {

if ( $_POST['email'] == $usr->email && $_POST['password'] == $usr->password ) {
$_SESSION['email'] = $usr->email;
$_SESSION['userid'] = $usr->userid;
$_SESSION['stat'] = 1;
header('Location:index.html');
}

} else {
$errs = '<p class="error">Invalid Email or Password.</p>';
}
}
}
?>

 

Any help or suggestions appreciated from some PHP Guru. Oh.. And one more thing is userid from session secure enough for content to user linking in MySQL? Thank you all...

Link to comment
Share on other sites

Yuhuuu thank you Muddy_Funster you rock man!!!!

 

I just added your $this->userid = $valid['userid']; inside function userLogin and in login.php $_SESSION['userid'] = $usr->userid; echos out $userid from MySQL.

 

Thank you man a trillion times :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.