0

i read many articles about it but i cant find out how to solve my problem. When my script unset session a simply want to redirect him to login when he can easily login and get back on page where he was logged out.

public static function expired()
    {
        if (isset($_SESSION['logged_id']))
        {
            if (time() - $_SESSION['time'] > 3600)
            {
                header('Location: http://nostools.cz/scripts/logout.php');
                $_SESSION['last_visited'] = $_SERVER['REQUEST_URI'];
            } else {
                $_SESSION['time'] = time();
                return true;
            }
        }
    }

    public function logout()
    {
        $onlinedestroy = Query::getInstance()->delete('online', array('user_id', '=', $_SESSION['logged_id']));
        unset($_SESSION['logged_id']);
        $this->redirect("http://nostools.cz/content/login/");
    }

this scrip is on every page. In $_SESSION['last_visited'] is logout.php and i need to get previous page i also tried to use $_SERVER['HTTP_REFERER']. Can somebody tell me how i can get previous page?

Jakub Staněk
  • 1,023
  • 1
  • 10
  • 11

3 Answers3

3

You can store url inside a session may, $_SESSION['visited_page'] inside header. Every time user visit different url this session value updated. During logout, you can assign url inside $_SESSION['visited_page'] to $_SESSION['last_visited'] and unset $_SESSION['visited_page']. Now you have the url user last visit. I hope you get my point.

Tonmoy
  • 91
  • 3
1

When redirecting in your logout function you could check the last_visited variable. Add it to the url you're redirecting to like so:

$this->redirect("http://nostools.cz/content/login?last_visited=" . $last_visited );

Then when you log in you check if $_GET['last_visited'] is set, if it is then you redirect from your login to that page

Shogunivar
  • 1,237
  • 11
  • 18
0

You should first get user refer page in a variable using $_SERVER['HTTP_REFERER']; in your login page.

LIKE:

<?php 
    session_start();
    $refPage = $_SERVER['HTTP_REFERER']; 
?>

And now when the user clicks to Login then change header location to user refer page

LIKE:

<?php 
if(isset($_POST[login])){
    session_start();
    header('location:' . $refPage);
}
?>

And in this time you should first check that user refers page empty or not because your user can visit direct your login page then your $refPage variable will be empty so after Click to Login page stays here

LIKE:

<?php
if(isset($_POST[login])){
    session_start();
    $refPage = $_SERVER['HTTP_REFERER'];  // get reffer page url
    if(empty($refPage)){ 
        header('location: yourredirectpage'); // if ref page is empty then set default redirect page.
    }else{
        header('location:' . $refPage); // or if ref page in not empty then redirect page to reffer page
    }
}
?>

Or you can use input type hidden where you can set value $_SERVER['HTTP_REFERER'];

LIKE:

"> And when a user clicks to Login then you can get the refPage value and redirect the previous page. And you should also check empty refer page. Because your user can visit direct your login page.

Thank you.

Obaidul Haque
  • 916
  • 11
  • 18