- [Session]
- ; Handler used to store/retrieve data.
- Session.save_handler = files; This is the way of the session, the default files can be used to indicate the storage.
Copy CodeThere are two different ways to do this, user and memcache. User mode refers to the handle of the session defined by itself (that is, the user), for the session access, etc., which can be extended to the database in the session. Memcache mode, you need to configure the Memcache, but also configure the Session.save_path. Use Memcache to make PHP 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 CodeUse memcached to make PHP Session.save_handler:
- Ini_set ("Session.save_handler", "memcached");
- Ini_set ("Session.save_path", "127.0.0.1:11211");
Copy CodeThe following introduction, PHP implementation of multi-server session sharing Memcache sharing method. Example: Customizing the 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: Autonomous implementation of the Session function based on memcache storage
- * Description: This class is to implement the function of the session, basically through
- * Set the client's cookie to save the SessionID,
- * Then save the user's data on the server side, finally through the
- * The session ID in the cookie to determine if a data is a user,
- * then perform the corresponding data operation
- *
- * This method is suitable for storing session data in Memcache memory mode,
- * At the same time if you build a distributed Memcache server,
- * Able to save quite a bit of cache data, and suitable for a larger number of concurrent users.
- *
- * Note: This class must require PHP to install the memcache extension or must have memcache PHP API
- * Get memcache Extension visit: http://pecl.php.net
- */
- Set the SESSION effective time, in seconds
- Define (' Sess_lifttime ', 3600);
- Defining Memcache configuration information
- Define (' memcache_host ', ' localhost ');
- Define (' Memcache_port ', ' 10000 ');
- if (!defined (' memcachesession '))
- {
- Define (' Memcachesession ', TRUE);
- Class Memachesession
- {
- {{{} class member Property definition
- Static $mSessSavePath;
- Static $mSessName;
- Static $MMEMCACHEOBJ;
- // }}}
- {{{} Initialize constructor
- /**
- * Constructor function
- *
- * @param string $login _user logged in user
- * @param int $login _type user type
- * @param string $login _sess Login Session Value
- * @return Esession
- */
- Public Function __construct ()
- {
- My memcache is compiled in the form of 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, and if not, initialize a
- 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 need for additional recycling, Memcache has its own outdated recycling mechanism
- return TRUE;
- }
- // }}}
- /** {{initsess ()
- *
- * @param NULL
- *
- * @return Bool True/false
- */
- Public Function initsess ()
- {
- Do not use Get/post variable mode
- Ini_set (' Session.use_trans_sid ', 0);
- Set the maximum time to live for garbage collection
- Ini_set (' Session.gc_maxlifetime ', sess_lifttime);
- How to save SESSION ID using a COOKIE
- Ini_set (' session.use_cookies ', 1);
- Ini_set (' Session.cookie_path ', '/');
- $domain = '. imysql.cn ';
- Multi-host sharing COOKIE that holds 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 that corresponds to each operation of the SESSION:
- Session_set_save_handler (
- Array (' memachesession ', ' sessopen '),//corresponds to static method My_sess::open (), same as 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 CodeYou can use this class by including MemacheSession.inc.php directly in the header file in your program. The test instance. 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 Code2, use SessionID to memcached query:
- get_session.php
- $mem = new Memcache;
- $mem->connect ("127.0.0.1", 11211);
- Var_dump ($mem->get (' 0935216dbc0d721d629f89efb89affa6 '));
- ?>
Copy CodeNote: In future versions of Memcache PECL, you can set the php.ini settings Session.save_handler directly. Like what: Session.save_handler = memcache Session.save_path = "tcp://host:port?persistent=1&weight=2&timeout=2& Retry_interval=15,tcp://host2:port2 " |