Home Backend Development PHP Tutorial Implementation method of using memcache to share session data in thinkPHP with multiple domain names_php example

Implementation method of using memcache to share session data in thinkPHP with multiple domain names_php example

Aug 04, 2016 am 08:56 AM
memcache thinkphp shared Multiple domain names

The example in this article describes the implementation method of using memcache to share session data in thinkPHP with multiple domain names. Share it with everyone for your reference, the details are as follows:

1. Origin of the problem

Slightly larger websites usually have several servers. Each server runs modules with different functions and uses different second-level domain names. However, for a comprehensive website, the user system is unified, that is, a set of user names. , the password can be used to log in in each module of the entire website. It is relatively easy for each server to share user data. You only need to put a database server on the back end, and each server can access user data through a unified interface. But there is still a problem, that is, after the user logs in to this server, when entering other modules of another server, he still needs to log in again. This is a one-time login, and all common problems are mapped to technology. In fact, it is between various servers. How to share SESSION data.

2. How PHP SESSION works

Before solving the problem, let’s first understand how PHP SESSION works. When the client (such as a browser) logs in to the website, the visited PHP page can use session_start() to open the SESSION, which will generate the client's unique identification SESSION ID (this ID can be obtained/set through the function session_id()). The SESSION ID can be retained on the client in two ways, so that when requesting different pages, the PHP program can learn the client's SESSION ID; one is to automatically add the SESSION ID to the GET URL, or the POST form, by default. Below, the variable name is PHPSESSID; the other is to save the SESSION ID in the COOKIE through COOKIE. By default, the name of this COOKIE is PHPSESSID. Here we mainly use the COOKIE method for explanation, because it is widely used.

So where is the SESSION data stored? On the server side of course, but instead of being saved in memory, it's saved in a file or database. By default, the SESSION saving method set in php.ini is files (session.save_handler = files), that is, the SESSION data is saved by reading and writing files, and the directory where the SESSION file is saved is specified by session.save_path, and the file name starts with sess _ is the prefix, followed by SESSIONID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the SESSION data after serialization. If the number of visits is large, there may be more SESSION files generated. In this case, you can set up a hierarchical directory to save SESSION files, which will improve the efficiency a lot. The setting method is: session.save_path="N;/save_path", N is hierarchical. level, save_path is the starting directory. When writing SESSION data, PHP will obtain the client's SESSION_ID, and then use this SESSION ID to find the corresponding SESSION file in the specified SESSION file storage directory. If it does not exist, create it, and finally serialize the data and write it to the file. . Reading SESSION data is a similar operation process. The read data needs to be deserialized and the corresponding SESSION variable is generated.

3. Main obstacles and solutions for sharing SESSION between multiple servers

By understanding the working principle of SESSION, we can find that by default, each server will generate a SESSION ID for the same client. For example, for the same user browser, the SESSION ID generated by server A is 30de1e9de3192ba6ce2992d27a1b6a0a, while server B generates SESSION ID The generated one is c72665af28a8b14c0fe11afe3b59b51b. In addition, PHP's SESSION data are stored separately in the file system of this server.

After identifying the problem, you can start to solve it. If you want to share SESSION data, you must achieve two goals: One is that the SESSION ID generated by each server for the same client must be the same and can be passed through the same COOKIE, which means that each server must be able to read the same SESSION ID. The COOKIE named PHPSESSID; the other is the storage method/location of SESSION data that must be accessible to all servers. Simply put, multiple servers share the client's SESSION ID and must also share the server's SESSION data.

The realization of the first goal is actually very simple. You only need to specially set the domain of COOKIE. By default, the domain of COOKIE is the domain name/IP address of the current server. If the domain is different, each server The set COOKIE cannot be accessed by each other. For example, the server of www.aaa.com cannot read or write the COOKIE set by the server of www.bbb.com. The servers of the same website we are talking about here have their own particularity, that is, they belong to the same first-level domain. For example: tieba.xiaoyuan.com and www.xiaoyuan.com both belong to the domain .xiaoyuan.com, then we can Set the domain of the COOKIE to .xiaoyuan.com, so that tieba.xiaoyuan.com, www.xiaoyuan.com, etc. can access this COOKIE. The setting method in PHP code is as follows:

<&#63;php
ini_set('session.cookie_domain', '.xiaoyuan.com');
&#63;>

Copy after login

In this way, the purpose of each server sharing the same client SESSION ID is achieved.

The second goal can be achieved by using file sharing. There are two ways to solve it, one is to use data storage session, and the other is to try memcache. Use MEMCACHE to solve it here.

I use the thinkphp framework, which already supports memcache access to sessions. After setting up the memcache server, you only need to set the memcache IP and port in the configuration file, and then specify the COOKIE_DOMAIN parameter, and then you can operate normally. Operate in session mode, and now you can share sessions with multiple domain names

Readers who are interested in more thinkPHP related content can check out the special topics of this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology" Summarize".

I hope that what is described in this article will be helpful to everyone’s PHP program design based on the ThinkPHP framework.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to share wallpaperengine with friends How to share wallpaperengine with friends Mar 18, 2024 pm 10:00 PM

Users can share the wallpapers they obtain with friends when using WallpaperEngine. Many users do not know how to share WallpaperEngine with friends. They can save their favorite wallpapers locally and then share them with friends through social software. How to share wallpaperengine with friends Answer: Save it locally and share it with friends. 1. It is recommended that you save your favorite wallpapers locally and then share them with friends through social software. 2. You can also upload it to the computer through a folder, and then click Share using the creative workshop function on the computer. 3. Use Wallpaperengine on the computer, open the options bar of the creative workshop and find

How to share screen on WeChat Enterprise How to share screen on WeChat Enterprise Feb 28, 2024 pm 12:43 PM

More and more enterprises choose to use exclusive enterprise WeChat, which not only facilitates communication between enterprises and customers and partners, but also greatly improves work efficiency. Enterprise WeChat has rich functions, among which the screen sharing function is very popular. During the meeting, by sharing the screen, participants can display content more intuitively and collaborate more efficiently. So how to share your screen efficiently in WeChat Enterprise? For users who don’t know yet, this tutorial guide will give you a detailed introduction. I hope it can help you! How to share screen on WeChat Enterprise? 1. In the blue area on the left side of the main interface of Enterprise WeChat, you can see a list of functions. We find the "Conference" icon. After clicking to enter, three conference modes will appear.

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Windows 11 Folder Sharing Guide: Easily Share Your Files and Data Windows 11 Folder Sharing Guide: Easily Share Your Files and Data Mar 13, 2024 am 11:49 AM

In daily life and work, we often need to share files and folders between different devices. Windows 11 system provides convenient built-in folder sharing functions, allowing us to easily and safely share the content we need with others within the same network while protecting the privacy of personal files. This feature makes file sharing simple and efficient without worrying about leaking private information. Through the folder sharing function of Windows 11 system, we can cooperate, communicate and collaborate more conveniently, improving work efficiency and life convenience. In order to successfully configure a shared folder, we first need to meet the following conditions: All devices (participating in sharing) are connected to the same network. Enable Network Discovery and configure sharing. Know the target device

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Shared printer cannot connect to printer Shared printer cannot connect to printer Feb 22, 2024 pm 01:09 PM

With the development of the digital era, shared printers have become an indispensable part of the modern office environment. However, sometimes we may encounter the problem that the shared printer cannot be connected to the printer, which will not only affect work efficiency, but also cause a series of troubles. This article aims to explore the reasons and solutions for why a shared printer cannot connect to the printer. There are many reasons why a shared printer cannot connect to the printer, the most common of which is network issues. If the network connection between the shared printer and the printer is unstable or interrupted, normal operation will not be possible.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

See all articles