Home >Backend Development >PHP Tutorial >Example of jumping back to the original page you want to visit after logging in in php_PHP tutorial

Example of jumping back to the original page you want to visit after logging in in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:061439browse

In many websites, users first visit a page to log in, but they did not log in at that time and then logged in. After waiting for the user to log in successfully, they definitely want to return to the page they last visited. Now I will introduce to you how to jump back after logging in. It turns out that the easiest way to access the page instance

is to directly use php $_SERVER['HTTP_REFERER']


If I want to log in on the A.php page


Now jump to the B.php page, we only need to add the following code to b.php

The code is as follows
 代码如下 复制代码

$url = $_SERVER['HTTP_REFERER'];
header("location:$url");

Copy code

$url = $_SERVER['HTTP_REFERER'];

header("location:$url");


 代码如下 复制代码

protected function checkLogin() {
       if (没有登录){          
       $thisurl = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];//当前URL
       $thisurl = urlencode($thisurl);//这里要注意需要把获取到的url转码,不然后面不好传递URL
           redirect("http://".$_SERVER["HTTP_HOST"]."/cityosweb/default.php/Index/login?url=".$thisurl);           
       }
   }

However, the above method has many shortcomings, such as parameters, etc., but under the IE browser, if you jump through the location of js, then this value cannot be obtained.

 代码如下 复制代码

$this->checkLogin();

Now I will make a comprehensive one.

First create a method to determine whether you are logged in, if not logged in

            if (not logged in){                                           $thisurl = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];//Current URL ​​​​ $thisurl = urlencode($thisurl);//It should be noted here that the obtained url needs to be transcoded, otherwise it will be difficult to pass the URL later
The code is as follows Copy code
 代码如下 复制代码

public function login() {
        $url = $_GET['url'];
        $this->assign('url',$url);
        $this->assign('title','Login');
        $this->display('user/reg_new.html');
    }

protected function checkLogin() {
redirect("http://".$_SERVER["HTTP_HOST"]."/cityosweb/default.php/Index/login?url=".$thisurl);          } }

Then call this method on the page that requires login to ask questions:
The code is as follows Copy code
$this->checkLogin(); This will jump to the login page if you are not logged in. And bring the URL of your previous page:
Then get the URL to submit login:
The code is as follows Copy code
public function login() { $url = $_GET['url'];            $this->assign('url',$url);            $this->assign('title','Login'); $this->display('user/reg_new.html'); } Obtain the url from the template and submit it to the php backend. After logging in, jump to this url and it's done. http://www.bkjia.com/PHPjc/631554.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631554.htmlTechArticleOn many websites, users first visit a page to log in, but are not logged in at the time and then log in, waiting for the user to log in successfully. I will definitely want to return to the page I visited last time, so I will...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn