Use Memcached to implement the session mechanism in php to replace native session support in PHP. Method file session implementation File: memcachedsession. php implementation principle (also the implementation principle of PHP internal session): 1. First, determine whether the client has a sessionid. a. add a session method file if no session id exists.
Session implementation File: memcachedsession. php
Implementation principle (also the implementation principle of PHP internal session ):
1. check whether the client has a sessionid,
A. add a sessionid to the client, usually a 32-bit hash code, and initialize an array as a session container.
B. If the client has a sessionid, use this sessionid to query data in memcached.
2. you can modify the session value in the session container during page execution.
3. at the end of the page, the user's session container is used as the value, and the user's sessionid is used as the key to save the key-value pair
Inside memcached
The code is as follows:
// Memcached server connection address
$ _ MEMCACHEAUTH = array (
'Host' => 'localhost'
, 'Port' = & gt; 11211
);
/*
Get some initialization settings
*/
$ _ SESSION_NAME = ini_get ("session. name"); // The name of sessionid
$ _ SESSION_TIME = ini_get ("session. cookie_lifetime"); // The maximum cookie retention time of sessionid
$ _ SESSION_EXPIRE = ini_get ("session. gc_maxlifetime"); // expiration time of session key-value pairs in memcached
$ _ SESSION_MEMKEY = ""; // sessionid value
/*
The custom _ session_start () method replaces the native session_start () method of PHP.
The logic should be clear.
*/
Function _ session_start ()
{
Global $ _ SESSION_NAME, $ _ SESSION_TIME, $ _ SESSION_MEMKEY;
Global $ _ SESSION;
Global $ _ MEMCACHEAUTH, $ _ sessionmem;
$ _ Sessionmem = memcache_connect ($ _ MEMCACHEAUTH ['host'], $ _ MEMCACHEAUTH ['port']);
If (empty ($ _ COOKIE [$ _ SESSION_NAME])
{
$ _ SESSION_MEMKEY = md5 (uniqid ());
Setcookie ($ _ SESSION_NAME, $ _ SESSION_MEMKEY, $ _ SESSION_TIME ,"/");
$ _ SESSION = array ();
}
Else
{
$ _ SESSION_MEMKEY =$ _ COOKIE [$ _ SESSION_NAME];
$ _ SESSION = memcache_get ($ _ sessionmem, $ _ SESSION_MEMKEY );
If ($ _ SESSION = FALSE)
{
$ _ SESSION = array ();
}
}
// Register a handler, which will be executed when the page is executed
Register_shutdown_function ("_ session_save_handler ");
}
/*
The method executed at the end of the page to save the session value and disable the memcached connection.
*/
Function _ session_save_handler ()
{
Global $ _ sessionmem;
Global $ _ SESSION, $ _ SESSION_NAME, $ _ SESSION_EXPIRE, $ _ SESSION_MEMKEY;
Memcache_set ($ _ sessionmem, $ _ SESSION_MEMKEY, $ _ SESSION, 0, $ _ SESSION_EXPIRE );
Memcache_close ($ _ sessionmem );
}
?>
Test File:
Set the session value
The code is as follows:
/*
Set the session value File: session_set.php
*/
Include_once "memcachedsession. php ";
_ Session_start ();
$ _ SESSION ['A'] = time ();
?>
Get session value
The code is as follows:
/*
File for obtaining the session value: session_get.php
*/
Include_once "memcachedsession. php ";
_ Session_start ();
Function getsession ()
{
Echo $ _ SESSION ['A'];
}
Getsession ();
?>
Memcached's buffer application is still very good ,,,
Reprinted: jincon's package blog http://www.yi1.com.cn
Reset session implementation File: memcachedsession. php implementation principle (also the implementation principle of PHP internal session): 1. First, determine whether the client has a sessionid. a. add a session if no session exists...