Use Memcached to implement the session mechanism under PHP to replace PHP's native session support_PHP tutorial

WBOY
Release: 2016-07-21 15:35:11
Original
1141 people have browsed it

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. If not, add a sessionid Give it 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 check the data in memcached.
2. The user can modify the session value in the session container by himself during the execution of the page
3. The page will finally use the user's session container as the value, use the user's sessionid as the key, and save this key-value pair to
Inside memcached

Copy code The code is as follows:

//memcached server connection address
$_MEMCACHEAUTH = array(
'host' => 'localhost'
, 'port' => 11211
);
/*
Get some initialization setting values
*/
$_SESSION_NAME = ini_get("session.name"); //The name of sessionid
$_SESSION_TIME = ini_get("session.cookie_lifetime"); //The maximum storage time of the sessionid cookie
$_SESSION_EXPIRE = ini_get("session.gc_maxlifetime"); //The expiration time of the session key-value pair in memcached
$_SESSION_MEMKEY = ""; //sessionid value
/*
Customized_ session_start() method, replace PHP's native session_start() method
The logic should be relatively 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, this function will be executed when the page is finished
register_shutdown_function("_session_save_handler");
}
/*
The last method executed on the page, used to save the session value and close 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 session value
Copy code The code is as follows:

/*
Set session value file: session_set.php
*/
include_once "memcachedsession.php";
_session_start();
$_SESSION['a'] = time();
?>

Get session value
Copy the code The code is as follows:

/*
Get the session value file: session_get.php
*/
include_once " memcachedsession.php";
_session_start();
function getsession()
{
echo $_SESSION['a'];
}
getsession();
? >

The buffering application of Memcached is still very good, haha,,,
Repost: jincon's package blog http://www.yi1.com.cn

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322279.htmlTechArticleMethod 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. If not, add a session...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!