1
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

1
  • Multiple errors spotted. == is different from =. Remember to exit() after header("Location: $url"); call Commented Jan 8, 2015 at 11:23

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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'];                     
}

Comments

-1

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']; 
}

3 Comments

Why separate isset() calls? That's not necessary.
This isset($_SESSION['username'], $_SESSION['password'], $_SESSION['user_id'], $_SESSION['role'])) is wrong, i just correct the syntax.
@NeedhiAgrawal you can use multiple check with comma separate in isset() follow stackoverflow.com/questions/14476609/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.