Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Laravel achieve re-login and jump to the pre-login page

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to re-login Laravel to jump to the login page of the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.

1. Application scenarios:

There is an expiration time after the user logs in, and the timed-out user needs to log in again. Example: when the user is on the / user/2 page, the login expires and jumps to the login page. After login, the user should also be in / user/2 instead of home/index.

Second, the realization principle

After judging the expiration of the user, store the user's current url address to session, and jump to this url address after the next login.

Third, the concrete implementation in laravel

Routing middleware (judging login status) here determines the login status of users based on the expiration of cookie. The middleware only judges and executes login-related judgments and does not do unnecessary operations (I use the value of $token of cookie as the key storage of session when logging in). The code is as follows:

Public function handle ($request, Closure $next) {/ / determine whether if exists in cookie ($token = Cookie::get ('token')) {/ / determine session information, keep the status consistent if ($request- > session ()-> get ($token)) {return $next ($request);}} / / get the current url, jump to the landing page $returnUrl = urlencode ($request- > getRequestUri ()); return redirect (' / login?reurl='. $returnUrl);}

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

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

The page side (judging flash information) judges the information of flash memory. Here it is divided into ajax and submit login. Submit should be rare. Get reurl under ajax, and then log in at the backend to judge reurl.

@ if (session ('reurl')) @ endif

Login (login logic) omits the logic of judging the user's account password and storing cookie,session, and finally the front end gets url,location.href = 'url', to jump to the original url address.

$reurl = $request- > get ('reurl'); $url = empty ($reurl)?' / home/index': $reurl;return response ($url)

Note: only the relevant code for re-login is posted in this article. Please modify the actual code according to the actual business.

These are all the contents of the article "how to re-login to the pre-login page in Laravel". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report