Use Memcached to implement session mechanism in php to replace native session support in PHP

Source: Internet
Author: User

Method File
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 Copy codeThe Code is as follows: <? Php
// 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 ValueCopy codeThe Code is as follows: <? Php
/*
Set the session value File: session_set.php
*/
Include_once "memcachedsession. php ";
_ Session_start ();
$ _ SESSION ['a'] = time ();
?>

Get session ValueCopy codeThe Code is as follows: <? Php
/*
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

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.