简体   繁体   English

登录后将用户重定向到上一页

[英]redirect user to previous page after login

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']. 在$ _SESSION ['last_visited']中是logout.php,我需要获取上一页,我也尝试使用$ _SERVER ['HTTP_REFERER']。 Can somebody tell me how i can get previous page? 有人可以告诉我如何获得上一页吗?

You can store url inside a session may, $_SESSION['visited_page'] inside header. 您可以将URL存储在会话可能的内部,将$ _SESSION ['visited_pa​​ge']存储在标头中。 Every time user visit different url this session value updated. 每次用户访问不同的URL时,此会话值都会更新。 During logout, you can assign url inside $_SESSION['visited_page'] to $_SESSION['last_visited'] and unset $_SESSION['visited_page']. 注销期间,您可以将$ _SESSION ['visited_pa​​ge']中的url分配给$ _SESSION ['last_visited'],并取消设置$ _SESSION ['visited_pa​​ge']。 Now you have the url user last visit. 现在您有了url用户的最后访问。 I hope you get my point. 希望你明白我的意思。

When redirecting in your logout function you could check the last_visited variable. 在注销功能中重定向时,可以检查last_visited变量。 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 然后,当您登录时,检查是否设置了$_GET['last_visited'] ,如果已设置,则从登录名重定向到该页面

You should first get user refer page in a variable using $_SERVER['HTTP_REFERER']; 您应该首先使用$ _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 并且在这种情况下,您应该首先检查用户是否将页面引用为空,因为您的用户可以直接访问您的登录页面,然后您的$ refPage变量将为空,因此在单击登录页面之后将停留在此处

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']; 或者,您可以使用隐藏的输入类型,在其中可以设置值$ _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. “>并且,当用户单击“登录”时,您可以获得refPage值并重定向上一页。还应选中“空引用页”,因为您的用户可以直接访问您的登录页。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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