How to jump to the previous page after PHP login

  • 2020-12-13 18:51:45
  • OfStack

The project requirements

When visiting the website page, some pages need authorization to visit, at this time will ask the user to log in, redirect to the login page login.php, how to achieve login and return to the page just visited.

Solution 1:

Before jumping to the login page, save the url of the current page to cookie. After the login authentication authorization is passed, take this url value from cookie and jump to the page specified by this url.

The specific implementation

My current program is based on the ThinkPHP framework, we will have a parent class controller, so I will add the code to set cookie to the _initialize() function in BaseAction, which will greatly simplify the program.


$refer = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Cookie::set('refer', $refer);

In the function of login detection, we add:


$refer = Cookie::get('refer');

Now this $refer is the page we visited before, we can return this parameter via AJAX and then jump, or jump directly using the program, depending on your application requirements.

Solution 2:

In addition to saving it as cookie, I believe you have also seen many large websites directly obtained in the form of GET, Drupal login mechanism is such.
The idea is as follows:
Before jumping to the login page, the url of the page visited by the visitor is passed as a parameter. After login verification, after granting access rights, the page specified by the url is jumped to.
For example, url before login is: ES47en. html When the visitor visits, click without permission, jump to the address of the login page is ES49en. php? url= ES52en.html. In this way, the parameter openphp.html can be obtained by means of GET when logging in. After successful login verification, the page of ES57en.html can be redirected to.

Generally speaking, I only have these two ideas. If you have a better idea, I sincerely hope you can tell me.


Related articles: