Home > database >  redirect to profile page if user logged in
redirect to profile page if user logged in

Time:04-14

I have a login form on RedBean PHP. I need to implement redirection to profile if user was logged and just closed page, but when I open my start page I need to type email and password again and again. How to redirect user immediately to profile page if he did not log out.

I tried to use if($_SESSION['logged_user'] = $user) {header('Location: /profile');, but it did not work. Please assist. I use OpenServer Panel.

Code:

$data = $_POST;
if(isset($data['do_login'])) {
    $user = R::findOne('users', 'email = ?', array($data['email']));
    if($user) {
        // var_dump($user);
        if(password_verify($data['password'], $user->password)) {
            $_SESSION['logged_user'] = $user;
            header('Location: /profile');
            // echo '<div style="color: green;">You are authorized! Go to <a href="/profile">Profile page</a></div>';
            
        }else {
            $errors[] = 'Password is not correct!';
        }
    } else {
        $errors[] = "User with such email doesn't exist!";
    }

    if(!empty($errors)) {
        echo '<div style="color: red;">'.array_shift($errors).'</div>';
    }
}
if($_SESSION['logged_user'] = $user) {
    header('Location: /profile');
}

CodePudding user response:

You incorrectly write your condition.== instead of =

if($_SESSION['logged_user'] == $user) {
header('Location: /profile');
    }

Let me know,if this solve your problem

CodePudding user response:

I solved it by add this code:

if(isset($_SESSION['logged_user'])) {     
    header('Location: /profile');
}
  • Related