<?php
/**
* set memcache hash model
*/
ini_set('memcache.hash_function','crc32');
/**
* define memcache servers array
*
* the const value can not be defined as an array and I don't like to use global key word
* in my methods.so I serialize the array.
* Otherwise,you can rewrite this class with global key word.
*
*/
define('MEM_HOST_ARRAY',serialize(
array(
array('192.168.1.163',8000), //array(host,port)
array('192.168.1.164',8000
)
)));
/**
* Define session max life time
*
*/
define('SESS_MAXLIFETIME',get_cfg_var('session.gc_maxlifetime'));
/**
* Project: sessHandle: Session inter-server solution with memcache
* File: sessHandle.class.php
* @author Guogs <guogs at foxmail dot com>
* @version v1.0.1
*
*/
class sessHandle{
/**
* static method for connectting to memcache server
*
* @return Resources
*/
static function connMemServer(){
if(class_exists('Memcache')){
$conn=new Memcache;
foreach (unserialize(MEM_HOST_ARRAY) as $key=>$value){
$conn->addServer($value[0],$value[1]);
}
if($conn){
return $conn;
}else{
self::sessException("Can Not Connect To Memcache Server .",1);
return false;exit;
}
}else{
self::sessException("Your Server Does Not Supports Memcache Exception.",2);
return false;exit;
}
}
/**
* static method for opening session
*
* @param string $sessSavePath
* @param string $sessName
* @return bool true or false
*/
static function open($sessSavePath,$sessName){
return self::connMemServer()?true:false;
}
/**
* static method for closing session
*
* @return bool
*/
static function close(){
return self::connMemServer()->close()?true:false;
}
/**
* static mathod for reading session
*
* @param string $sessID
* @return bool true or false
*/
static function read($sessID){
$data=self::connMemServer()->get($sessID);
if(!empty($data)){
return $data;
}else{
if(self::connMemServer()->set($sessID,'',0,SESS_MAXLIFETIME)==1){
return true;
}else{
self::sessException("Can Not Set Session.",3);
return false;exit;
}
}
}
/**
* static method for writing sessions
*
* @param string $sessID
* @param string $sessData
* @return bool true or false
*/
static function write($sessID,$sessData){
if(self::connMemServer()->set($sessID,$sessData,0,SESS_MAXLIFETIME)==1){
return true;
}else{
self::sessException("Can Not Set Session.",4);
return false;exit;
}
}
/**
* static method for destroying sessions
*
* @param string $sessID
* @return true or false
*/
static function destroy($sessID){
if(self::connMemServer()->delete($sessID)==1){
return true;
}else{
self::sessException('Can Not Delete Session.',5);
return false;exit;
}
}
/**
* static method for session recycling
* memcache has its self memory management mechanism.
* So we need not to do nothing.Just give it a signal
* @return bool true
*/
static function gc(){
return true;
}
/**
* static method for counting sessions
*
* @return int
*/
static function sessCount(){
$hostArray=unserialize(MEM_HOST_ARRAY);
$memInfo=self::connMemServer()->getExtendedStats();
if(count($hostArray)>1){
$count=0;
foreach ($hostArray as $key=>$value){
$count+=$memInfo[implode(':',$value)]==false?0:($memInfo[implode(':',$value)]['curr_items']);
}
}else{
$count=$memInfo['curr_items'];
}
return $count;
}
/**
* static method for getting server status
*
* @return array
*/
static function sessServerStatus(){
return self::connMemServer()->getExtendedStats();
}
/**
* static method for reporting exception
*
* @param string $message
* @param int $code
*/
static function sessException($message,$code){
echo __CLASS__." Error.\n ErrorNo.:".$code." ErrorMssages:".$message;
}
/**
* static method for rewriting session method
*
* @return bool true
*/
static function init(){
foreach (unserialize(MEM_HOST_ARRAY) as $key=>$value){
$string.='tcp://'.implode(':',$value).',';
}
ini_set('session.save_handler','memcache');
ini_set('session.save_path',trim($string,','));
session_module_name('user'); //Pay attention:if you do not set this option this class will not work.
session_set_save_handler( //Pay attention to the order: open,close,read,write,destroy,gc.
array('sessHandle','open'),
array('sessHandle','close'),
array('sessHandle','read'),
array('sessHandle','write'),
array('sessHandle','destroy'),
array('sessHandle','gc')
);
return true;
}
}
sessHandle::init(); //commit
?>