Home Backend Development PHP Tutorial PHP realizes multi-server sharing SESSION data_PHP tutorial

PHP realizes multi-server sharing SESSION data_PHP tutorial

Jul 21, 2016 pm 03:45 PM
php session one shared accomplish data server of website origin question

一、问题起源
稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名、密码在整个网站的各个模块中都是可以登录使用的。各个服务器共享用户数据是比较容易实现的,只需要在后端放个数据库服务器,各个服务器通过统一接口对用户数据进行访问即可。但还存在一个问题,就是用户在这个服务器登录之后,进入另一个服务器的别的模块时,仍然需要重新登录,这就是一次登录,全部通行的问题,映射到技术上,其实就是各个服务器之间如何实现共享 SESSION 数据的问题。
二、PHP SESSION 的工作原理
在解决问题之前,先来了解一下 PHP SESSION 的工作原理。在客户端(如浏览器)登录网站时,被访问的 PHP 页面可以使用 session_start() 打开 SESSION,这样就会产生客户端的唯一标识 SESSION ID(此 ID 可通过函数 session_id() 获取/设置)。SESSION ID 可以通过两种方式保留在客户端,使得请求不同的页面时,PHP 程序可以获知客户端的 SESSION ID;一种是将 SESSION ID 自动加入到 GET 的 URL 中,或者 POST 的表单中,默认情况下,变量名为 PHPSESSID;另一种是通过 COOKIE,将 SESSION ID 保存在 COOKIE 中,默认情况下,这个 COOKIE 的名字为 PHPSESSID。这里我们主要以 COOKIE 方式进行说明,因为应用比较广泛。
那么 SESSION 的数据保存在哪里呢?当然是在服务器端,但不是保存在内存中,而是保存在文件或数据库中。默认情况下,php.ini 中设置的 SESSION 保存方式是 files(session.save_handler = files),即使用读写文件的方式保存 SESSION 数据,而 SESSION 文件保存的目录由 session.save_path 指定,文件名以 sess_ 为前缀,后跟 SESSION ID,如:sess_c72665af28a8b14c0fe11afe3b59b51b。文件中的数据即是序列化之后的 SESSION 数据了。如果访问量大,可能产生的 SESSION 文件会比较多,这时可以设置分级目录进行 SESSION 文件的保存,效率会提高很多,设置方法为:session.save_path="N;/save_path",N 为分级的级数,save_path 为开始目录。当写入 SESSION 数据的时候,PHP 会获取到客户端的 SESSION_ID,然后根据这个 SESSION ID 到指定的 SESSION 文件保存目录中找到相应的 SESSION 文件,不存在则创建之,最后将数据序列化之后写入文件。读取 SESSION 数据是也是类似的操作流程,对读出来的数据需要进行解序列化,生成相应的 SESSION 变量。
三、多服务器共享 SESSION 的主要障碍及解决办法
通过了解 SESSION 的工作原理,我们可以发现,在默认情况下,各个服务器会各自分别对同一个客户端产生 SESSION ID,如对于同一个用户浏览器,A 服务器产生的 SESSION ID 是 30de1e9de3192ba6ce2992d27a1b6a0a,而 B 服务器生成的则是 c72665af28a8b14c0fe11afe3b59b51b。另外,PHP 的 SESSION 数据都是分别保存在本服务器的文件系统中。如下图所示:

/upload/20090815161113758.gif


确定了问题所在之后,就可以着手进行解决了。想要共享 SESSION 数据,那就必须实现两个目标:一个是各个服务器对同一个客户端产生的 SESSION ID 必须相同,并且可通过同一个 COOKIE 进行传递,也就是说各个服务器必须可以读取同一个名为 PHPSESSID 的 COOKIE;另一个是 SESSION 数据的存储方式/位置必须保证各个服务器都能够访问到。
简单地说就是多服务器共享客户端的 SESSION ID,同时还必须共享服务器端的 SESSION 数据。
第一个目标的实现其实很简单,只需要对 COOKIE 的域(domain)进行特殊地设置即可,默认情况下,COOKIE 的域是当前服务器的域名/IP 地址,而域不同的话,各个服务器所设置的 COOKIE 是不能相互访问的,如
www.aaa.com
的服务器是不能读写
www.bbb.com
服务器设置的 COOKIE 的。这里我们所说的同一网站的服务器有其特殊性,那就是他们同属于同一个一级域,如:aaa.infor96.com 和
www.infor96.com
都属于域 .infor96.com,那么我们就可以设置 COOKIE 的域为 .infor96.com,这样 aaa.infor96.com、www.infor96.com 等等都可以访问此 COOKIE。PHP 代码中的设置方法如下:
('session.cookie_domain', '.infor96.com');
?>
这样各个服务器共享同一客户端 SESSION ID 的目的就达到了。
第二个目标的实现可以使用文件共享方式,如 NFS 方式,但设置、操作上有些复杂。我们可以参考先前所说的统一用户系统的方式,即使用数据库来保存 SESSION 数据,这样各个服务器就可以方便地访问同一个数据源,获取相同的 SESSION 数据了。
解决办法如下图所示:

/upload/20090815161114361.gif


4. Code Implementation
First create a data table. The SQL statement of MySQL is as follows: CREATE TABLE `sess` (
`sesskey` varchar(32) NOT NULL default ' ',
`expiry` bigint(20) NOT NULL default '0',
`data` longtext NOT NULL,
PRIMARY KEY (`sesskey`),
KEY `expiry` (`expiry `)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
sesskey is the SESSION ID, expiry is the SESSION expiration time, and data is used to save SESSION data.
By default, SESSION data is saved in file mode. If you want to save it in database mode, you must redefine the processing functions of each SESSION operation. PHP provides the
session_set_save_handle()
function. You can use this function to customize the SESSION processing process. Of course, you must first change session.save_handler to user, which can be set in PHP:
('user');
?>
Next we will focus on the session_set_save_handle() function. This function has six parameters: session_set_save_handler ( string open, string close, string read, string write, string destroy, string gc )
Each parameter is the function name of each operation. These operations are: open, close, read, write, destroy, and garbage collection. There are detailed examples in the PHP manual. Here we use OO to implement these operations. The detailed code is as follows:
('MY_SESS_TIME', 3600); //SESSION survival time
//Class definition
class My_Sess
{
function init()
{
$domain = '.infor96.com';
//Do not use GET/POST variable method
ini_set('session. use_trans_sid', 0);
//Set the maximum lifetime of garbage collection
ini_set('session.gc_maxlifetime', MY_SESS_TIME);
//How to use COOKIE to save SESSION ID
ini_set('session .use_cookies', 1);
ini_set('session.cookie_path', '/');
//Multiple hosts share the COOKIE that saves the SESSION ID
ini_set('session.cookie_domain', $domain) ;
//Set session.save_handler to user instead of the default files
session_module_name('user');
//Define the method names corresponding to various SESSION operations:
session_set_save_handler(
array('My_Sess', 'open'), //corresponds to the static method My_Sess::open(), the same below.
array('My_Sess', 'close'),
array(' My_Sess', 'read'),
array('My_Sess', 'write'),
array('My_Sess', 'destroy'),
array('My_Sess', 'gc')
);
} //end function
function open($save_path, $session_name) {
return true;
} //end function
function close() {
global $MY_SESS_CONN;
if ($MY_SESS_CONN) { //Close the database connection
$MY_SESS_CONN->Close();
}
return true;
} //end function
function read($sesskey) {
global $MY_SESS_CONN;
$sql = 'SELECT data FROM sess WHERE sesskey=' . $MY_SESS_CONN->qstr($sesskey) . ' AND expiry>=' . time ();
$rs =& ​​$MY_SESS_CONN->Execute($sql);
if ($rs) {
if ($rs->EOF) {
return ";
} else { //Read SESSION data corresponding to SESSION ID
$v = $rs->fields[0];
$rs->Close();
return $ v;
} //end if
} //end if
return ";
} //end function
function write($sesskey, $data) {
global $ MY_SESS_CONN;

$qkey = $MY_SESS_CONN->qstr($sesskey);
$expiry = time() + My_SESS_TIME; //Set expiration time

//Write SESSION
$arr = array(
'sesskey' => $qkey,
'expiry' => $expiry,
'data' => $data);
$MY_SESS_CONN ->Replace('sess', $arr, 'sesskey', $autoQuote = true);
return true;
} //end function
function destroy($sesskey) {
global $MY_SESS_CONN;
$sql = 'DELETE FROM sess WHERE sesskey=' . $MY_SESS_CONN->qstr($sesskey);
$rs =& ​​$MY_SESS_CONN->Execute($sql);
return true;
} //end function
function gc($maxlifetime = null) {
global $MY_SESS_CONN;
$sql = 'DELETE FROM sess WHERE expiry. time();
$MY_SESS_CONN->Execute($sql);
//Due to frequent deletion operations on the sess table, fragmentation is likely to occur.
//So the table is optimized during garbage collection.
$sql = 'OPTIMIZE TABLE sess';
$MY_SESS_CONN->Execute($sql);
return true;
} //end function
} ///:~
//Use ADOdb as the database abstraction layer.
require_once('adodb/adodb.inc.php');
//Database configuration items can be placed in the configuration file (such as: config.inc.php).
$db_type = 'mysql';
$db_host = '192.168.212.1';
$db_user = 'sess_user';
$db_pass = 'sess_pass';
$db_name = 'sess_db ';
//Create a database connection, this is a global variable.
$GLOBALS['MY_SESS_CONN'] =& ADONewConnection($db_type);
$GLOBALS['MY_SESS_CONN']->Connect( $db_host, $db_user, $db_pass, $db_name);
/ /Initialize SESSION settings, must be run before session_start()! !
My_Sess::init();
?>
5. Remaining issues If the website has a large number of visits, SESSION's reading and writing will frequently operate on the database. This will significantly reduce efficiency.Considering that SESSION data is generally not very large, you can try to write a multi-threaded program in C/Java, use a HASH table to save the SESSION data, and read and write data through socket communication. In this way, the SESSION is saved in the memory, and the reading and writing speed is improved. It should be much faster. In addition, server load can be shared through load balancing. But these are just some of my own thoughts and assumptions, and have not been put into practice

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320377.htmlTechArticle1. Larger websites where the problem originates usually have several servers, each running different functions. modules, using different second-level domain names, and a comprehensive network...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles