forel
forel

Reputation: 33

PDO user session

I'm converting old mysql to PDO and I have my first problem.

I'd like to echo out user info based on $_SESSION['user_id'] but user_id from database is not assigned to $_SESSION['user_id'] so I get Notice: Undefined index: user_id in in account page on $user_id = $_SESSION['user_id']; and print_r($_SESSION['user_id']); line.

I do have session_start(); on top of the page.

Class

class User {
public $user_id = null;

public function userInfo($user_id) {
    global $db;

    $query = $db->prepare("SELECT user_id, username, email FROM users WHERE user_id = :id");
    $query->bindValue(':id', $user_id);
    $query->execute();

    return $query->fetch();
}

public function login($login, $password){
    global $db;

    $query = $db->prepare("SELECT COUNT(user_id) as count, user_id FROM users WHERE username = :user AND password = :pass");

    $query->bindValue(':user', $login);
    $query->bindValue(':pass', $password);
    $query->execute();

    $result = $query->fetchColumn();

    if ($result) {  
        $this->user_id = $result['user_id'];
    }

    return $result;
   }
}

Login

if (isset($_POST['login'], $_POST['password'])) {

$login = $_POST['login'];
$password = sha1($_POST['password']);

$errors = array();

if (empty($login) || empty($password))
{
 $errors[] = 'All fields required!';
} 
else {
    $user = new User;
    $log_in = $user->login($login, $password); 

    if($log_in) {
        $_SESSION['user_id'] = $user->user_id;
        header('Location: account.php');
        exit();
    } else { 
            $errors[] = 'Username or password incorrect!';
           }
}

if (!empty($errors)){

        foreach ($errors as $error)
        {
            echo '<div id="error"><strong>', $error, '</strong></div><br />';
        }
    }
}

Account page

$user = new User;
$user_id = $_SESSION['user_id'];
$data = $user->userInfo($user_id);

echo $data['username'],'<br />';
echo $data['email'],'<br />';

echo '<pre>';
print_r($_SESSION['user_id']);
echo '</pre>';

if(isset($_SESSION['user_id'])){

    echo '<pre>';
    print_r($data);
    echo '</pre>';

    echo $data['username'],'<br />';
    echo $data['email'],'<br />';

    }else {
    echo 'Please log in!';
}

Upvotes: 0

Views: 2091

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157887

$_SESSION['user_id'] but user_id from database is not assigned to $_SESSION['user_id'] so I get Notice: Undefined index: user_id in in account page

This is logically incorrect statement.
The first part of it can be proven true only if you verity it immediately, i.e.:

$_SESSION['user_id'] = ...//being populated 
var_dump($_SESSION['user_id']);die;

if it won't die displaying correct value - only then you can say that it isn't being populated. Otherwise all your further deductions would be incorrect. As there can be 1000s reasons for the (although correctly populated) to not show on the another page.
Well, if it won't die here - move this var-die code upper to see what is going wrong. And so on.

Sorry, but whole question looks like "debug my code for me".
Why don't you debug it yourself, using all the power of the interpreter and environment you have at hand and indispensable ability to run it as many times as you wish?
Why do you ask strangers to debug your code just mentally, running your code just using their brains? It is erroneous by design and extremely inefficient!
Please, debug your code yourself, find certain problem and then ask a particular question regarding it.

Upvotes: 0

Zak
Zak

Reputation: 25205

The main problem is, your login method is returning data, and your calling code is treating it as if it is setting internal variables:

$user = new User;
$log_in = $user->login($login, $password); 

if($log_in) {
    $_SESSION['user_id'] = $user->user_id;

see.. your user_id is actually in $log_in, not in $user... correct that and you should be fine.

Upvotes: 1

Related Questions