This article is a continuation of NFS sharing for PHP multi-server session sharing. However, this time, I used memcache to store sessions. For the installation of memcache, please refer to the following: build an efficient, reliable, and stable WEB server-2. Install MySQL and memcache. Next, we will customize a session processing mechanism.
This article is a continuation of NFS sharing for PHP multi-server session sharing. However, this time, I used memcache to store sessions. For the installation of memcache, please refer to the following: build an efficient, reliable, and stable WEB server-2. Install MySQL and memcache. Next, we will customize a session processing mechanism.
This article is a continuation of NFS sharing for PHP multi-server session sharing. However, this time, I used memcache to store sessions. For the installation of memcache, please refer to the following: build an efficient, reliable, and stable WEB server-2. Install MySQL and memcache.
Next, we will customize a session processing mechanism. I will not talk about the implementation method of the session and paste the program directly.
/* Vim: set expandtab tabstop = 4 shiftwidth = 4 foldmethod = marker :*/
// ================================================ =====
// Program: Memcache-Based Session Class
// Function: Memcache-based Session Function
// Author: yejr
// Website: http://imysql.cn
// Time: 2007-01-05
// ================================================ =====
/**
* File: MemcacheSession. inc. php
* Class Name: MemcacheSession Class
* Function: automatically implements the Session Function Based on Memcache storage.
* Description: This class is used to implement the Session function.
* Set the Cookie of the client to save the SessionID,
* Save the user data on the server, and finally pass
* The Session Id in the Cookie is used to determine whether a data is a user,
* Perform corresponding data operations
*
* This method is suitable for storing Session data in Memcache memory,
* If a distributed Memcache server is built,
* It can store a considerable amount of cached data and is suitable for scenarios with a large number of concurrent users
*
* Note: this class must require PHP to install Memcache extensions or PHP APIs with Memcache.
* Get Memcache extensions visit: http://pecl.php.net
*/
// Set the SESSION validity period in seconds.
Define ('sess _ lifttime', 3600 );
// Define the memcache configuration information
Define ('memcache _ host', 'localhost ');
Define ('memcache _ port', '123 ');
If (! Defined ('memcachesession '))
{
Define ('memcachesession ', TRUE );
Class MemacheSession
{
// {Class member attribute Definition
Static $ mSessSavePath;
Static $ mSessName;
Static $ mMemcacheObj;
//}}}
// {Initialize the constructor
/**
* Constructor
*
* @ Param string $ login_user logon user
* @ Param int $ login_type User Type
* @ Param string $ login_sess logon Session Value
* @ Return Esession
*/
Public function _ construct ()
{
// My memcache is compiled using the php module and can be called directly.
// If not, please include the Memcache-client.php file yourself
If (! Class_exists ('memcache') |! Function_exists ('memcache _ connect '))
{
Die ('fatal Error: Can not load Memcache extension! ');
}
If (! Empty (self ::$ mMemcacheObj) & is_object (self ::$ mMemcacheObj ))
{
Return false;
}
Self: $ mMemcacheObj = new Memcache;
If (! Self: $ mMemcacheObj-> connect (MEMCACHE_HOST, MEMCACHE_PORT ))
{
Die ('fatal Error: Can not connect to memcache host'. MEMCACHE_HOST. ':'. MEMCACHE_PORT );
}
Return TRUE;
}
//}}}
/** {SessOpen ($ pSavePath, $ name)
*
* @ Param String $ pSavePath
* @ Param String $ pSessName
*
* @ Return Bool TRUE/FALSE
*/
Public function sessOpen ($ pSavePath = '', $ your ssname = '')
{
Self: $ mSessSavePath = $ pSavePath;
Self: $ mSessName = $ inclussname;
Return TRUE;
}
//}}}
/** {SessClose ()
*
* @ Param NULL
*
* @ Return Bool TRUE/FALSE
*/
Public function sessClose ()
{
Return TRUE;
}
//}}}
/** {SessRead ($ wSessId)
*
* @ Param String $ wSessId
*
* @ Return Bool TRUE/FALSE
*/
Public function sessRead ($ wSessId = '')
{
$ WData = self: $ mMemcacheObj-> get ($ wSessId );
// Read data first. If no data exists, initialize
If (! Empty ($ wData ))
{
Return $ wData;
}
Else
{
// Initialize an empty record
$ Ret = self: $ mMemcacheObj-> set ($ wSessId, '', 0, SESS_LIFTTIME );
If (TRUE! = $ Ret)
{
Die ("Fatal Error: Session ID $ wSessId init failed! ");
Return FALSE;
}
Return TRUE;
}
}
//}}}
/** {SessWrite ($ wSessId, $ wData)
*
* @ Param String $ wSessId
* @ Param String $ wData
*
* @ Return Bool TRUE/FALSE
*/
Public function sessWrite ($ wSessId = '', $ wData = '')
{
$ Ret = self: $ mMemcacheObj-> replace ($ wSessId, $ wData, 0, SESS_LIFTTIME );
If (TRUE! = $ Ret)
{
Die ("Fatal Error: SessionID $ wSessId Save data failed! ");
Return FALSE;
}
Return TRUE;
}
//}}}
/** {SessDestroy ($ wSessId)
*
* @ Param String $ wSessId
*
* @ Return Bool TRUE/FALSE
*/
Public function sessDestroy ($ wSessId = '')
{
Self: sessWrite ($ wSessId );
Return FALSE;
}
//}}}
/** {SessGc ()
*
* @ Param NULL
*
* @ Return Bool TRUE/FALSE
*/
Public function sessGc ()
{
// No additional recycling is required. memcache has its own expiration mechanism.
Return TRUE;
}
//}}}
/** {InitSess ()
*
* @ Param NULL
*
* @ Return Bool TRUE/FALSE
*/
Public function initSess ()
{
$ Domain = '.imysql.cn ';
// Do not use the GET/POST VARIABLE METHOD
Ini_set ('session. use_trans_sid ', 0 );
// Set the maximum time to live for garbage collection
Ini_set ('session. gc_maxlifetime', SESS_LIFTTIME );
// Use cookies to save SESSION IDs
Ini_set ('session. use_cookies ', 1 );
Ini_set ('session. cookie_path ','/');
// Multiple hosts share the COOKIE that saves the SESSION ID
Ini_set ('session. cookie_domain ', $ domain );
// Set session. save_handler to user instead of the default files
Session_module_name ('user ');
// Define the method name for each SESSION operation:
Session_set_save_handler (
Array ('memachesession', 'sessopen '), // corresponds to the static method My_Sess: open (), the same below.
Array ('memachesession', 'sessclose '),
Array ('memachesession ', 'ssread '),
Array ('memachesession', 'sswrite '),
Array ('memachesession ', 'sessdestroy '),
Array ('memachesession', 'ssgc ')
);
Session_start ();
Return TRUE;
}
//}}}
} // End class
} // End define
$ MemSess = new MemacheSession;
$ MemSess-> initSess ();
?>
Then, directly includeMemacheSession.inc.phpAnd the previous program does not need to be modified.
Special thanks: the implementation of the Session Class Based on Memcache storage by night travelers.
Note: in future versions of memcache PECL, you can directly set php. ini to set your session. save_handler, which is roughly as follows:
session.save_handler = memcache
session.save_path = "tcp://host:port?persistent=1&weight=2&timeout=2&retry_interval=15,tcp://host2:port2"
For more information, see php_memcache.h cvs log.