简体   繁体   中英

redirect to profile page if user logged in

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

You incorrectly write your condition.== instead of =

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

Let me know,if this solve your problem

I solved it by add this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM