Home > PHP Framework > Laravel > Regarding the solution of laravel5.6 and thinkphp3.2 using redis to share session

Regarding the solution of laravel5.6 and thinkphp3.2 using redis to share session

藏色散人
Release: 2021-06-29 10:36:15
forward
2363 people have browsed it

In the actual project, the historical project was developed using thinkphp3.2, and now the function needs to be updated. The updated function part is planned to be developed using laravel5.6.

Problem Description

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.

Program design

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.

Solution

thinkphp enables redis support

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");
Copy after login
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.

laravel opens redis

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.

laravel gets thinkphp's session

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);
Copy after login

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

Related tutorial recommendations: "laravel tutorial"

The above is the detailed content of Regarding the solution of laravel5.6 and thinkphp3.2 using redis to share session. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template