laravel5.6 and thinkphp3.2 using Redis to share session scenarios

Source: Internet
Author: User
This article mainly introduces the laravel5.6 and thinkphp3.2 use Redis shared session of the scheme, has a certain reference value, now share to everyone, the need for friends can refer to

In the actual project, the historical Project uses thinkphp3.2 development, now needs to update the function, updates the function part, intends to use the laravel5.6 to develop.

Problem description

There are several key issues that need to be addressed, one of which is the issue of user authentication. That is, when system 1 is logged in, System 2 automatically recognizes its login information. That is, the essence needs to be solved: Laravel and thiniphp share session problems.

Program Design

Session is stored using Redis. thinkphp and Laravel access Redis, which automatically acquires authenticated user information stored in Redis based on the same cookie.

Solution Solutions

thinkphp Turn on Redis support

thinkphp3.2 does not have redis support for the integrated session, at this point we use the settings in PHP to turn it on. In index.php, we add the following two lines of statements:

Ini_set ("Session.save_handler", "Redis"), Ini_set ("Session.save_path", "tcp://redis:6379");
You can also add it to the config.php configuration file

At this point, the thinkphp is stored as a prefix when the session is stored in phpredis_session.

Laravel Open Redis

Laravel the way to open Redis is relatively slightly more complicated. For specific information, you need to refer to the official documentation using composer to install Predis and configure it.

Laravel get thinkphp's session

Since Laravel completely deprecated the built-in session of PHP. So there's no way we can get any information from $_session (you'll even get a variable undefined error). Because the Laravel cookie is processed by encryption. Therefore, we also cannot use Laravel's own cookie to obtain cookie information.

The concrete implementation of the idea is:
1 get the native cookie.
2 Phpredis_session to form key
3 using Redis to get the value stored directly in key

... use Illuminate\support\facades\redis;    .. $cookie = $_cookie[' Phpsessid ');    $session = Redis::get (' phpredis_session: '. $cookie);

This $session is the session value in the thinkphp system.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.