简体   繁体   中英

php how to redirect to last visited page when logged out from session

I have a couple of pages protected with a login; add.php , settings.php and archive.php At the top of each of these pages i have this code:

// check login
session_start();
if(!isset($_SESSION['blog_login'])){
    $_SESSION['last_visited'] = $_SERVER['REQUEST_URI'];
    header("Location: login.php");
    exit();
}

When logging out, the page request goes to logout.php . Logout.php looks like below:

session_start();
unset($_SESSION['blog_login']);
header("Location: login.php");

When logging in again, i want to go to the page i was before. This is my login.php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $error = NULL;
    if(isset($_POST['username'],$_POST['password'])){
        $user = array(
                        "username" => $admin_name,
                        "password"=> $admin_passw           
                );
        $username = $_POST['username'];
        $pass = $_POST['password'];
        if($username == $user['username'] && $pass == $user['password']){
            session_start();
            $_SESSION['blog_login'] = $username;
            header('Location:'.$_SESSION['last_visited']);                      
        }
        else {
            $error = '<div class="alert alert-danger">Incorrect login data</div>';

        }

    }
}

Unfortunately, the header('Location:'.$_SESSION['last_visited']); line does not send me to the previous page i was before logging out. What i am doing wrong here?

You can use $_SERVER['HTTP_REFERER'] for this.

Skip the line $_SESSION['last_visited'] = $_SERVER['REQUEST_URI']; in add.php , settings.php and archive.php

In your logout.php add the line $_SESSION['last_visited'] = $_SERVER['HTTP_REFERER'];

So logout.php:

session_start();
unset($_SESSION['blog_login']);
$_SESSION['last_visited'] = $_SERVER['HTTP_REFERER']; //bind the last visited page you came from to a session
header("Location: login.php");

(Don't need to change login.php )

you can store your current page URL in a variable after logout redirects to your URL address.

It seems to be one of the following two reasons.

  1. You did not start session on your login.php page, it must start with: session_start();

  2. $_SERVER['REQUEST_URI'] does not output anything.

Use $_COOKIE and set a cookie for last_visited_page then after user logs in check if cookie exist and if cookie exist then redirect to last_visited page using cookie.

You should not use $_SESSION for this because you log someone out then you clear $_SESSION data using session_destroy(); and when user revisits your website a new session is started. But if you use cookie then you don't have to worry about that. Because cookie will be saved in browser so if he logins from same browser you can use $_COOKIE['last_visited_page'] and send him to his last visited page.

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