-
- [Session]
- ; Handler used to store/retrieve data.
- session.save_handler = files; This is the session method, the default files is enough, which means file storage.
Copy the code
There are two other ways, user and memcache.
The user mode refers to the handle of the session defined by oneself (that is, the user), which is used for session access, etc. This can store the session extension in the database.
In memcache mode, you need to configure memcache and session.save_path.
Use memcache for PHP’s session.save_handler:
-
- ini_set("session.save_handler", "memcache");
- ini_set("session.save_path", "tcp://127.0.0.1:11211,tcp://192.168.1.12:11211") ;
Copy code
Use memcached for PHP's session.save_handler:
-
- ini_set("session.save_handler","memcached");
- ini_set("session.save_path","127.0.0.1:11211");
Copy code
The following introduces how PHP implements memcache sharing for multi-server session sharing.
example:
Customize session processing mechanism.
-
- /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
- //==================== ========================
- // Program: Memcache-Based Session Class
- // Function: Session function class based on Memcache storage
- //= ==========================================
-
- /**
- * File: MemcacheSession.inc.php
- * Class name: MemcacheSession Class
- * Function: Independently implement the Session function based on Memcache storage
- * Description: This class is to implement the Session function, basically through
- * Set the client's Cookie To save the SessionID,
- * then save the user's data on the server side, and finally determine whether a data belongs to the user through
- * the Session Id in the cookie,
- * and then perform corresponding data operations
- *
- * This method is suitable for Memcache The way to store session data in memory,
- * At the same time, if you build a distributed Memcache server,
- * can save a lot of cached data, and is suitable for situations where there are a lot of users and large concurrency
- *
- * Note: This class must require PHP The Memcache extension is installed or the PHP API of Memcache is required
- * To obtain the Memcache extension, please visit: http://pecl.php.net
- */
-
- //Set the SESSION validity time in seconds
- define('SESS_LIFTTIME', 3600);
-
- //Define memcache configuration information
- define('MEMCACHE_HOST', 'localhost');
- define('MEMCACHE_PORT' ', '10000');
-
- if (!defined('MemcacheSession'))
- {
- define('MemcacheSession', TRUE);
- class MemacheSession
- {
- // {{{ Class member attribute definition
- static $mSessSavePath ;
- static $mSessName;
- static $mMemcacheObj;
- // }}}
-
- // {{{ Initialization constructor
- /**
- * Constructor
- *
- * @param string $login_user Login user
- * @param int $login_type User type
- * @param string $login_sess Login Session value
- * @return Esession
- */
- public function __construct()
- {
- //My memcache It is compiled as a 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 = '', $pSessName = '')
- {
- self::$mSessSavePath = $pSavePath;
- self::$mSessName = $pSessName;
- 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 the data first, if not, initialize one
- 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 required, memcache has its own expired recycling mechanism
- return TRUE;
- }
- // }}}
-
- /**{{{ initSess()
- *
- * @param NULL
- *
- * @return Bool TRUE/FALSE
- */
- public function initSess()
- {
- //Do not use GET/POST variable method
- ini_set('session.use_trans_sid', 0);
-
- //Set the maximum lifetime of garbage collection
- ini_set('session.gc_maxlifetime', SESS_LIFTTIME) ;
-
- //How to use COOKIE to save SESSION ID
- ini_set('session.use_cookies', 1);
- ini_set('session.cookie_path', '/');
-
- $domain = '.imysql.cn';
- //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 names corresponding to various SESSION operations:
- session_set_save_handler(
- array('MemacheSession', 'sessOpen'), //corresponds to the static method My_Sess::open(), the same below.
- array('MemacheSession', 'sessClose'),
- array('MemacheSession', 'sessRead'),
- array('MemacheSession', 'sessWrite'),
- array('MemacheSession', 'sessDestroy'),
- array ('MemacheSession', 'sessGc')
- );
-
- session_start();
- return TRUE;
- }
- // }}}
- }//end class
- }//end define
-
- $memSess = new MemacheSession;
- $memSess->initSess();
- ?>
Copy the code
Include MemacheSession.inc.php directly in the header file in the program to use this class.
Test example.
1. Create a session
-
- //set_session.php
- session_start();
- if (!isset($_SESSION['admin'])) {
- $_SESSION['TEST'] = 'wan';
- }
- print $_SESSION['admin'];
- print "/n";
- print session_id();
- ?>
Copy code
2, use sessionid to query in memcached:
-
- //get_session.php
- $mem = new Memcache;
- $mem->connect("127.0.0.1", 11211);
- var_dump($mem->get( '0935216dbc0d721d629f89efb89affa6'));
- ?>
Copy code
Note: memcache PECL In future versions, you can directly set php.ini to set session.save_handler.
For example:
session.save_handler = memcache
session.save_path = "tcp://host:port?persistent=1&weight=2&timeout=2&retry_interval=15,tcp://host2:port2" |