Denis Dumitras
Denis Dumitras

Reputation: 29

Php session error undefined index

session_start();

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

    $extractabout = $db->prepare("SELECT * FROM user WHERE username = :username && password = :password");

    $extractabout->execute([
        'username' => $_POST['username'],
        'password' => $_POST['password']
    ]);

    $infos = $extractabout->rowCount() ? $extractabout : [] ;

    foreach ($infos as $info) {
        if(!empty($info['username']) && !empty($info['password']) && !empty($info['id']) && !empty('role')) {
            $_SESSION['username'] == $info['username'];
            $_SESSION['password'] == $info['password'];         
        }

    }

    if(isset($_SESSION['username'], $_SESSION['password'], $_SESSION['user_id'], $_SESSION['role'])) {
        header("Location: test.php");
    }   

}

?>

I have an error when I give $_SESSION['username'] the value of $info['username'] called :

Undefined index: username
Undifined index: password

Upvotes: 1

Views: 1031

Answers (3)

Needhi Agrawal
Needhi Agrawal

Reputation: 1326

You have syntax errors in your code as,corrected one is:

if(!empty($info['username']) && !empty($info['password']) && !empty($info['id']) && !empty($info['role'])) {
    $_SESSION['username'] = $info['username'];
    $_SESSION['password'] = $info['password']; 
}

Upvotes: -1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

use = for assign values in session not ==(compare).

Also what is !empty('role') i think it would be !empty($info['role'])

if(!empty($info['username']) && !empty($info['password']) && !empty($info['id']) && !empty($info['role'])) {
        $_SESSION['username'] = $info['username'];
        $_SESSION['password'] = $info['password'];                     
}

Upvotes: 0

Rizier123
Rizier123

Reputation: 59681

You don't assign the values, you only have to use one = so use this:

$_SESSION['username'] = $info['username'];
$_SESSION['password'] = $info['password']; 

Also i would put a exit(); after each header so that you are sure the script stops to be executed!

header("Location: test.php");
exit();

Upvotes: 2

Related Questions