PHP achieves multi-server session sharing. memcache sharing the bottleneck of file-based Session access may be related to disk IO operations, so there is no problem in dealing with sessions with small data volumes, however, if you encounter Sesstion with a large amount of data, you may not be competent. now, you can use Memca PHP to achieve multi-server session sharing-memcache sharing.
The bottleneck of file-based Session access may be due to disk I/O operations, so there is no problem in dealing with sessions with small data volumes. However, if you encounter Sesstion with large data volumes, you may not be competent, currently, Memcache is used to store Session data, and the efficiency can naturally be improved a lot through memory.
First open the php. ini file and find the part of the session: (The semicolon is followed by a comment)
[Session]
; Handler used to store/retrieve data.
Session. save_handler = files; this is the session method. the default files are enough to store files,
There are two other methods: user and memcache.
The user mode refers to the session handle defined by yourself (that is, the user), used for session access, etc. This can extend the session to the database.
In memcache mode, you must configure memcache and session. save_path.
Use memcache for 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");
Use memcached for PHP session. save_handler
ini_set("session.save_handler","memcached");ini_set("session.save_path","127.0.0.1:11211");Multi-server session sharing with memcache using PHP
Then, you can customize a session processing mechanism. I will not talk about the implementation method of the session and paste the program directly.
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 $ your SSNAME ** @ 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 an 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 extra recycle is required. memcache has its own expiration recycle mechanism: return TRUE;} // }}/ ** {{{ initSess () ** @ param NULL ** @ return Bool TRU E/FALSE */public function initSess () {// do not use the GET/POST variable method ini_set ('session. use_trans_sid ', 0); // sets the maximum garbage collection time ini_set ('session. gc_maxlifetime ', SESS_LIFTTIME); // use the COOKIE to save the session id ('session. use_cookies ', 1); ini_set ('session. cookie_path ','/'); $ domain =' .imysql.cn '; // multiple hosts share the COOKIEini_set ('session. cookie_domain ', $ domain); // sets the session. set save_handler to user instead of the default filessessio. N_module_name ('user'); // defines 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', 'sessgc '); session_start (); return TRUE ;} // }}}// end class} // end define $ memSess = new MemacheSession; $ memSess-> initSess ();?>
? Then, you can directly include MemacheSession. inc. php in the project header file, and the previous program does not need to be modified.
Create a session
Use sessionid to query in memcached
connect("127.0.0.1", 11211);var_dump($mem->get('0935216dbc0d721d629f89efb89affa6'));?>?
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 = memcachesession.save_path = "tcp://host:port?persistent=1&weight=2&timeout=2&retry_interval=15,tcp://host2:port2"
1 floor any_luf