Home >Backend Development >PHP Tutorial >Laravel5.4 re-login to implement jumping to the pre-login page code sharing

Laravel5.4 re-login to implement jumping to the pre-login page code sharing

小云云
小云云Original
2018-02-08 09:05:051969browse

Sometimes we encounter an expiration time after a user logs in. After the timeout period, the user needs to log in again. This article mainly introduces to you the principles and methods of re-logging in in Laravel 5.4 to jump to the pre-login page. I hope it can help everyone. .

1. Application scenarios:

There is an expiration time after the user logs in, and the user needs to log in again after timeout. Example: When the user is on the /user/2 page and the login expires, the user will be redirected to the login page. After logging in, the user should still be on /user/2 instead of home/index.

2. Implementation Principle

After determining that the user has expired, store the user's current URL address in the session, and jump to this URL address after the next login.

3. Specific implementation in laravel

Routing middleware (determining login status) Here, the user's login status is determined based on whether the cookie has expired. The middleware only determines the login-related judgment and execution, and does not do anything redundant. Operation (when logging in, I use the cookie value $token as the session key storage), the code is as follows:

public function handle($request, Closure $next) {
 //判断cookie是否存在
 if ($token = Cookie::get('token')) {
 //判断session信息,保持状态一致
 if ($request->session()->get($token)) {
  return $next($request);
 }
 }

 //获取当前url,跳转到登陆页
 $returnUrl = urlencode($request->getRequestUri());

 return redirect('/login?reurl=' . $returnUrl); }

Login side (processing interaction with the page and jump logic) What is done here is to jump to Login page, this can be implemented in middleware, only this sentence, what is implemented with with is to flash the reurl for front-end use.

return redirect('/')->with('reurl', $returnUrl);

Page side (judging flash memory information) To determine the flash memory information, it is divided into ajax and submit login. There should be very few submits. Get the reurl under ajax, and then log in to the backend to determine the reurl.

 @if (session('reurl'))
  <input type="hidden" name="reurl" value="{{ session(&#39;reurl&#39;) }}"/>
 @endif

Login (login logic) omits the logic of determining the user account password, and storing cookies and sessions. Finally, the front end obtains the url, location.href = 'url', and jumps to the original url address.

$reurl = $request->get('reurl');

$url = empty($reurl) ? '/home/index' : $reurl;

return response($url);

Note: This article only posts the relevant code to implement re-login. Please modify the actual code according to the actual business.

Related recommendations:

thinkphp still has a problem with jumping to the pre-login page after logging in

The above is the detailed content of Laravel5.4 re-login to implement jumping to the pre-login page code sharing. For more information, please follow other related articles on the PHP Chinese website!

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