PHP to implement multi-server sharing session data _php skills

Source: Internet
Author: User
Tags class definition garbage collection session id php session

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 understanding the working principle of the session, we can find that by default, each server will have its own sessions ID for the same client separately, for example, for the same user browser, a server generates an ID that is 30DE1E9DE3192BA6CE2992D27A1B6A0A, while the B server generates C72665AF28A8B14C0FE11AFE3B59B51B. In addition, the PHP session data are stored separately in the file system of the 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 COOKIE, and 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 actually very simple, only need to make a special setting for the domain of the cookie, by default, the domain of the cookie is the domain name/IP address of the current server, and if the domain is different, the cookies set by each server are not accessible to each other, such as
Www.aaa.com
The server is not read-write
Www.bbb.com
Server settings for cookies. Here we call the same Web server has its particularity, that is, they belong to the same level of domain, such as: Aaa.infor96.com and
Www.infor96.com
All belong to the domain. infor96.com, then we can set the domain of the cookie to. infor96.com so that aaa.infor96.com, www.infor96.com, and so on can access this cookie. The Setup method in the PHP code is as follows:
(' Session.cookie_domain ', '. infor96.com ');
?>
The purpose of sharing the same client session ID for each server is achieved.
The implementation of the second goal can use file sharing methods, such as NFS, but it is somewhat complex to set up and operate. We can refer to the previous approach of the unified user system, that is, using a database to save session data, so that each server can easily access the same data source, get the same session data.
The solution is shown in the following illustration:


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 ')
) Engine=myisam DEFAULT Charset=utf8 collate=utf8_unicode_ci
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 a
Session_set_save_handle ()
function, you can use this function to customize the session process, of course, first of all to the Session.save_handler to user, you can set in PHP:
(' user ');
?>
Next, let's focus on the Session_set_save_handle () function, which 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, which is open, close, read, write, destroy, Garbage collected. There are detailed examples in the PHP manual where we use OO to implement these operations, and the detailed code is as follows:
(' 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 instead of 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 (), 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 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 issuesIf 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.