first, the origin of the problemSlightly larger sites, usually have several servers, each server running a different function of the module, the use of different two-level domain names, and a strong overall site, the user system is unified, that is, a set of user names, passwords 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 access user data. However, there is still a problem, that is, after the user login to another server after the other module, still need to log back in, this is a login, all the problems, mapping to the technology, in fact, how each server to achieve sharing SESSION data problem.
Ii. How the PHP SESSION works
Before you solve the problem, let's 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 results in a client's unique ID session ID (this ID can be obtained/set through the function session_id). Session ID can be retained in two ways in the client, so that when requesting a different page, the PHP program can learn the client's session ID, one is to automatically add session ID to the Get URL, or POST form, by default, the variable name is P Hpsessid; the other is to save the SESSION ID in a cookie by using a cookie, by default, the name of the cookie is PHPSESSID. Here we mainly explain in the COOKIE way, because the application is more extensive.
So where does the SESSION data be stored? It is, of course, on the server side, but not in memory, but in a file or database. By default, the session set in PHP.ini is saved by files (Session.save_handler = files), that is, the session data is saved using a read-write file, and the directory saved by the session file is SESSION.S AVE_PATH specifies that the file name is prefixed with Sess_, followed by the SESSION ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the SESSION data after the serialization. If the access volume is large, may produce the session file will be more, then you can set the hierarchical directory to save the session file, the efficiency will be improved a lot, the setting method is: Session.save_path= "N;/save_path", N is a graded series, save _path is the starting directory. When the session data is written, PHP will obtain the client's session_id, and then according to the session ID to the specified session file to save the directory to find the corresponding session file, does not exist to create, and finally to serialize the data to write to the file. Reading session data is a similar process, and the data that is read needs to be deserialized to generate the corresponding session variables.
three, multi-server sharing session of the main obstacles and solutionsBy understanding how the SESSION works, we can see that, by default, each server is individually
A client generates session ID, such as for the same user browser, the session ID generated by a server is 30de1e9de3192ba6ce2992d27a1b6a0a, and B server generates c72665af28a8b14c0fe11afe3b59b51b. In addition, the SESSION data of PHP is stored separately in
In the file system of this server.
Once you have identified the problem, you can proceed with the solution. To share session data, two goals must be achieved: one is that each server must have the same session ID as the same client, and it can be passed through the same COOKIE, which means that each server must be able to read the same name as PHPSESSID. Cookies;
The other is how the SESSION data is stored/placed to ensure that each server has access to it. In short, it is more
The server shares the session ID of the client and must also share the session data on the server side.
The implementation of the first goal is actually very simple, only the domain of the COOKIE can be specially set to
By default, the domain of the COOKIE is the domain/IP address of the current server, and the domain is different, each server is set
Cookies are not accessible to each other.
Iv. Implementation of the CodeFirst create the data table, MySQL SQL statements are as follows: Create TABLE ' Sess ' (' sesskey ' varchar (+) NOT null default ' ', ' expiry ' bigint (a) not null de Fault ' 0 ', ' data ' longtext not NULL, PRIMARY key (' Sesskey '), key ' expiry ' (' expiry ')) Type=myisam Sesskey for SESSION id,e The Xpiry is the session expiration time and data is used to save the session.
By default, session data is saved as a file and you want to save it as a database, you must redefine the handler functions for each operation of the session. PHP provides the Session_set_save_handle () function, you can use this function to customize the session of the process, of course, first of all to change the Session.save_handler to user, can be set in PHP: Session_m Odule_name (' user ');
Next, 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 a function name for each operation, which in turn is:
Open, close, read, write, destroy, garbage collection. There are detailed examples in the PHP manual,
Here we use OO way to implement these operations, the detailed code is as follows:
<?php define (' My_sess_time ', 3600); SESSION Survival Duration Class My_sess {function init () {$domain = '. infor96.com '; Do not use Get/post variable mode ini_set (' Session.use_trans_sid ', 0); Set garbage collection Maximum Lifetime ini_set (' Session.gc_maxlifetime ', my_sess_time); How to save SESSION ID using a COOKIE ini_set (' session.use_cookies ', 1); Ini_set (' Session.cookie_path ', '/'); Multi-host sharing save SESSION ID COOKIE ini_set (' Session.cookie_domain ', $domain); Set the Session.save_handler to user, Instead of the default files session_module_name (' user '); Defines the method name for each operation of the SESSION: Session_set_save_handler (' my_sess ', ' open '), Corresponds to the static method My_sess::open (), 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 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 the table sess, it is easy to produce fragments, Therefore, the table is optimized for 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 item that can be placed in a configuration file (for example: 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); Initialize SESSION settings, must run before session_start ()!! My_sess:: Init ();?>
|
v. Legacy issuesIf the site has a large number of visits, the session will be read and written frequently to the database operation, so the efficiency will be significantly reduced. Considering that the session data is generally not very large, you can try to write a multi-threaded program with C/java, the HASH table to save session data, and through the socket communication data read and write, so that the SESSION is stored in memory, read and write speed should be much faster. Load balancing can also be used to offload server load.
Editor
http://www.bkjia.com/PHPjc/446839.html www.bkjia.com true http://www.bkjia.com/PHPjc/446839.html techarticle first, the problem originates slightly larger website, usually has several servers, each server runs the different function module, uses the different two level domain name, but a whole strong net ...