There is no problem not worth solving, and no technology not worth learning!

In Laravel 6, replace the Session implementation with the corresponding cookie implementation

测试功能,符合预期,在跳转后的网页的 Cookie 中已经存在新设置的 Key

1. In the program, whether it is a preview mode is implemented based on session. But when the user logs out, it will also automatically exit the preview mode. as shown in Figure 1

在程序中,是否为预览模式,是基于 session 实现的。但是当用户退出登录时,也会自动退出预览模式
Figure 1

2. The performance of exiting the preview mode is that the preview banner at the bottom of the page has disappeared. as shown in Figure 2

退出预览模式的表现便是页面底部的预览横幅已经消失
Figure 2

3. Reference:session regenerate session id , if you use the built-in function LoginController, Laravel will automatically regenerate the Session ID in the authentication. The final decision is replaced with the corresponding cookie implementation

4. The replacement code is implemented in the following order


// $request->session()->forget(XxxService::THEME_PREVIEW_ID);
Cookie::queue(Cookie::forget(XxxService::THEME_PREVIEW_ID));
// $request->session()->put(XxxService::THEME_PREVIEW_NAME, $themePreviewName);
Cookie::queue(Cookie::make(XxxService::THEME_PREVIEW_NAME, $themePreviewName));

// $request->session()->forget([XxxService::THEME_PREVIEW_ID, XxxService::THEME_PREVIEW_NAME]);
Cookie::queue(Cookie::forget(XxxService::THEME_PREVIEW_ID));
Cookie::queue(Cookie::forget(XxxService::THEME_PREVIEW_NAME));

$request->session()->has(XxxService::THEME_PREVIEW_NAME);
Cookie::has(XxxService::THEME_PREVIEW_NAME);

$request->session()->get(XxxService::THEME_PREVIEW_NAME);
Cookie::get(XxxService::THEME_PREVIEW_NAME);


5. The test function is in line with the expectations. The newly set key has already existed in the cookie of the webpage after the jump. as shown in Figure 3

测试功能,符合预期,在跳转后的网页的 Cookie 中已经存在新设置的 Key
Figure 3

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.