laravel5.6 and thinkphp3.2 use redis to share session

不言
Release: 2023-04-02 20:54:01
Original
2095 people have browsed it

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.

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.

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!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!