PHP to implement multiple server sharing session data

Source: Internet
Author: User
Tags class definition file system garbage collection implement session id mysql php session domain name
session| Server | data

First, the origin of the problem
Slightly larger sites, there are usually several servers, each server running different functions of the module, the use of different level two domain name, and a strong overall site, the user system is unified, that is, a set of user name, password in the entire site of the various modules can be logged in use. Each server sharing user data is relatively easy to implement, only need to put a database server on the back end, each server through the unified interface to user data access can be. But there is a problem, that is, the user after the server login, access to another server module, still need to log in, this is a login, all the problems, mapping to technology, in fact, is how each server to share the session data problem.

Second, the PHP session working principle
Before you solve the problem, start by understanding how the PHP session works. When a client (such as a browser) logs on to a Web site, the accessed PHP page can use Session_Start () to open the session, which produces the client's unique identifier (this ID can be obtained/set through the function session_id ()). The session ID can be kept on the client in two ways, so that when a different page is requested, the PHP program can learn the client's session ID, either by automatically adding the session ID to the URL of Get, or by the POST's form, by default, the variable is named P Hpsessid; the other is to save the session ID in a cookie by using a cookie, which by default is named Phpsessid. Here we mainly in the COOKIE way to explain, because the application is more extensive.
So where is the session data stored? Of course it's on the server side, but not in memory, but in a file or database. By default, the session Save method set in php.ini is files (Session.save_handler = files), which saves session data in a read-write file, while the session file is saved in a directory by SESSION.S AVE_PATH specifies that the filename is prefixed with sess_, followed by the session ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the session data after serialization. If the visit is large, may produce the session file will be more, at this time can set the hierarchical directory to save the session file, the efficiency will be improved a lot, set the method is: session.save_path= "N;/save_path", N for graded series, save _path is the start directory. When the session data is written, PHP obtains the session_id of the client, and then finds the corresponding session file in the directory of the specified session file, which is not present, and then the data is serialized and then written to the file. Reading session data is similar to the operation process, to read out the data needs to be serialized, generate the corresponding session variables.

Three, multi-server sharing session of the main obstacles and solutions
By learning how the session works, we can see that by default, each server will be individually aligned
A client generates a session ID, such as for the same user browser, the session ID generated by server A is 30de1e9de3192ba6ce2992d27a1b6a0a, and the B server generates c72665af28a8b14c0fe11afe3b59b51b. In addition, PHP's session data is stored separately in the
In the file system of this server. As shown in the following illustration:



Once you have identified the problem, you can proceed to solve it. To share session data, you have to implement two goals: one is that each server must have the same session ID for the same client, and it can be passed through the same COOKIE, which means that each server must be able to read the same one named Phpsessid Cookies;
The other is how the session data is stored/positioned to ensure that each server is accessible. simply put, the session ID of a multiple-server shared client, and the server-side session data must also be shared.

The implementation of the first goal is simple enough to specifically set the domain of the COOKIE,
By default, the domain of the COOKIE is the domain name/IP address of the current server, and each server is set by a different domain.
Cookies are not accessible to each other, such as

Four, code implementation
First create the data table, the MySQL SQL statement is as follows:
CREATE TABLE ' Sess ' (
' Sesskey ' varchar not NULL default ' ",
' Expiry ' bigint not NULL default ' 0 ',
' Data ' longtext not NULL,
PRIMARY KEY (' Sesskey '),
KEY ' expiry ' (' expiry ')
) Type=myisam
Sesskey is session id,expiry for session expiration, and data is used to save session data.

By default, session data is saved as a file, and you want to save it using the database, you must redefine the processing functions for each operation of the session. PHP provides the Session_set_save_handle () function, you can use this function to customize the session process, of course, first of all to change the Session.save_handler to user, you can set in PHP:
Session_module_name ("user");
Next, let's 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, garbage collection. There are detailed examples in the PHP manual,
Here we use OO to implement these operations, the detailed code is as follows:

Code:

<?php
Define ("My_sess_time", 3600);
Session Survival Time Long
Class definition
Class My_sess
{
function init ()
{
$domain = ". Infor96.com";
Do not use the Get/post variable method
Ini_set ("Session.use_trans_sid", 0);
Set maximum lifetime for garbage collection
Ini_set ("Session.gc_maxlifetime", my_sess_time);

How to save session IDs with cookies
Ini_set ("Session.use_cookies", 1);
Ini_set ("Session.cookie_path", "/");
Multi-host sharing cookies that hold session IDs
Ini_set ("Session.cookie_domain", $domain);

Set the Session.save_handler to user,
Rather than the default files
Session_module_name ("user");
Defines the name of the method corresponding to the session's actions:
Session_set_save_handler (
Array ("My_sess", "open"),
Corresponds to the static method My_sess::open (), hereinafter.
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) {
To close a 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 the session data corresponding to the 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 of table sess, it is easy to produce fragments,
Therefore, the table is optimized in 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");
A database configuration entry that can be placed in a 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, which is a global variable.
$GLOBALS ["My_sess_conn"] =& adonewconnection ($db _type);
$GLOBALS ["My_sess_conn"]->connect ($db _host, $db _user, $db _pass, $db _name);
Initialization session settings must be run before session_start ()!!
My_sess::init ();
?>

v. Legacy issues
If the site is a large number of visits, session reading and writing will frequently operate on the database, so that the efficiency will be significantly reduced. Considering that the session data generally will not be very large, you can try to write a multithreaded program with C/java, save session data with a HASH table, and read and write data through the socket communication, so that the session is stored in memory, read and write speed should be much faster. In addition, load balancing can also be used to share the server load. But these are just some of my own ideas and assumptions that have not been practiced:(



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.