The method of implementing multi-server sharing session data with PHP _php skill

Source: Internet
Author: User
Tags class definition garbage collection session id php session php define
PHP to implement multiple server sharing session 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 solving the problem, let's take a look at 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 does the data for the session be saved? 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 servers are cookies that cannot read and write to www.bbb.com server settings.

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 belong to the domain. infor96.com, then we can set the domain of the COOKIE as. Infor96.com, so aaa.infor96.com, www.infor96.com, and so on can access this COOKIE. The Setup method in the PHP code is as follows:

 <?php ini_set('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 ',
   ' da Ta ' 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:

 <?php  session_module_name('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:

 <?php define(' My_sess_time ',3600);Session Lifetime long//class definitionClassMy_sess{functionInit()     {$domain='. Infor96.com ';Do not use the Get/post variable methodIni_set(' Session.use_trans_sid ',0);Set maximum lifetime for garbage collectionIni_set(' Session.gc_maxlifetime ',My_sess_time);How to save session IDs with cookiesIni_set(' Session.use_cookies ',1);Ini_set(' Session.cookie_path ','/');Multi-host sharing cookies that hold session IDsIni_set(' Session.cookie_domain ',$domain);Set the Session.save_handler to user instead of the default filesSession_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 FunctionfunctionOpen($save _path,$session _name) {returnTrue; }End FunctionfunctionClose() {Global$MY _sess_conn; if ($MY _sess_conn) {To close a database connection$MY _sess_conn->Close(); } returnTrue; }End FunctionfunctionRead($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 IfReturn''; }End FunctionfunctionWrite($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); ReturnTrue; }End FunctionfunctionDestroy($sesskey) {Global$MY _sess_conn;$sql=' DELETE from Sess WHERE sesskey= '.$MY _sess_conn->Qstr($sesskey);$rs=&$MY _sess_conn->Execute($sql); ReturnTrue; }End FunctionfunctionGc($maxlifetime=Null) {Global$MY _sess_conn;$sql=' DELETE from Sess WHERE expiry< '.Time();$MY _sess_conn->Execute($sql);Because of frequent deletions to the table sess, it is easy to generate fragmentation//So the table is optimized for garbage collection.$sql=' OPTIMIZE TABLE sess ';$MY _sess_conn->Execute($sql); ReturnTrue; }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  Setup 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.