Home > Article > Backend Development > laravel5.6 and thinkphp3.2 use redis to share session
This article mainly introduces the solution of using redis to share sessions between laravel5.6 and thinkphp3.2. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
In practice Among the projects, the historical project was developed using thinkphp3.2. Now it is necessary to update the functions. The updated functions are planned to be developed using laravel5.6.
There are several key issues that need to be solved, one of which is the issue of user authentication. That is, after system 1 only logs in, system 2 automatically recognizes its login information. That is to say, the essence needs to be solved: the problem of laravel and thinphp sharing session.
session uses redis for storage. Thinkphp and laravel access redis together and automatically obtain the authenticated user information stored in redis based on the same cookie.
thinkphp3.2 does not have redis support for integrated sessions. At this time, we use the settings in php to enable it. 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 time, when thinkphp stores the session, it will be PHPREDIS_SESSION is stored as a prefix.
The method of laravel opening redis is relatively complicated. For specific information, you need to refer to the official documentation to use composer to install Predis and perform related configurations.
Because laravel completely deprecated PHP's built-in session. So there is no way for us to get any information through $_SESSION (you may even get an undefined variable error). Because laravel's cookies are encrypted. Therefore, we cannot use laravel's own cookies to obtain cookie information.
The specific implementation ideas are:
1 Get the native cookie.
2 Join PHPREDIS_SESSION to form a key
3 Use redis to directly obtain the value stored in the 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 entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
The above is the detailed content of laravel5.6 and thinkphp3.2 use redis to share session. For more information, please follow other related articles on the PHP Chinese website!