229 lines
4.1 KiB
PHP
229 lines
4.1 KiB
PHP
<?php
|
|
|
|
/* $Id: file.php 8 2009-01-11 06:02:53Z john $ */
|
|
|
|
|
|
defined('SE_PAGE') or exit();
|
|
|
|
|
|
class SESessionStorageMemcached extends SESessionStorage
|
|
{
|
|
public $_db;
|
|
|
|
|
|
public $_compress = NULL;
|
|
|
|
|
|
public $_persistent = FALSE;
|
|
|
|
|
|
public $_servers;
|
|
|
|
|
|
|
|
|
|
function __construct( $options=array() )
|
|
{
|
|
|
|
|
|
/*if( !$this->test() )
|
|
{
|
|
die("The memcached extension isn't available");
|
|
}*/
|
|
|
|
$options['servers'] = array(array('host'=>'127.0.0.1','port'=>'11211'));
|
|
// Default server connection
|
|
if( empty($options['servers']) ) $options['servers'] = array(array('host'=>'127.0.0.1','port'=>'11211'));
|
|
|
|
|
|
parent::__construct($options);
|
|
//$this->register($options);
|
|
$this->_compress = ( isset($options['compress']) ? $options['compress'] : FALSE );
|
|
$this->_persistent = ( isset($options['persistent']) ? $options['persistent'] : FALSE );
|
|
$this->_servers = ( isset($options['servers']) ? $options['servers'] : array() );
|
|
|
|
}
|
|
|
|
|
|
function test()
|
|
{
|
|
|
|
global $memcache_test_servers;
|
|
|
|
if(!class_exists('Memcached')){
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
//$memcache = new Memcached();
|
|
//$memcache->addServer(@$session_options['servers'][0]['host'], @$session_options['servers'][0]['port']);
|
|
//$memcache->addServer(@$session_options['servers'][0]['host'], @$session_options['servers'][0]['port']);
|
|
|
|
|
|
|
|
if( empty($memcache_test_servers) )
|
|
return TRUE;
|
|
// Note: will return true if at least one connection succeeds
|
|
|
|
//echo $memcache_server_info['host']." 1111". $memcache_server_info['port'];
|
|
foreach( $memcache_test_servers as $memcache_server_info )
|
|
{
|
|
if( ($memcache_obj = $memcache->addServer($memcache_server_info['host'], $memcache_server_info['port'])) )
|
|
{
|
|
$memcache->quit($memcache_obj);
|
|
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
public function open($save_path, $session_name)
|
|
{
|
|
|
|
$this->_db = new Memcached;
|
|
|
|
|
|
|
|
for( $i=0, $n=count($this->_servers); $i < $n; $i++ )
|
|
{
|
|
|
|
$server = $this->_servers[$i];
|
|
|
|
$this->_db->addServer($server['host'], $server['port'], $this->_persistent);
|
|
|
|
}
|
|
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
|
|
|
|
function close()
|
|
{
|
|
//$this->_db->quit();
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
|
|
|
|
function read($id)
|
|
{
|
|
|
|
|
|
|
|
$sess_id = 'sess_'.$id;
|
|
$lifetime = ini_get("session.gc_maxlifetime");
|
|
$expire = $this->_db->get($sess_id.'_expire');
|
|
// set prune period
|
|
if( $expire + $lifetime < time() )
|
|
{
|
|
|
|
$this->_db->delete($sess_id);
|
|
$this->_db->delete($sess_id.'_expire');
|
|
}
|
|
else
|
|
{
|
|
$this->_db->replace($sess_id.'_expire', time());
|
|
}
|
|
|
|
if($this->_db->get($sess_id)==false){
|
|
|
|
return '';}
|
|
|
|
return $this->_db->get($sess_id);
|
|
}
|
|
|
|
|
|
|
|
|
|
function write($id, $session_data)
|
|
{
|
|
|
|
/*$file = '/home/httpd/html/mojo.bateworld.com/public_html/bwadmin/people.txt';
|
|
// Open the file to get existing content
|
|
$current = file_get_contents($file);
|
|
// Append a new person to the file
|
|
$current .= "John Smith\n";
|
|
// Write the contents back to the file
|
|
file_put_contents($file, $current);
|
|
*/
|
|
|
|
|
|
|
|
$sess_id = 'sess_'.$id;
|
|
|
|
if( $this->_db->get($sess_id.'_expire') )
|
|
{
|
|
$this->_db->replace($sess_id.'_expire', time(), 0);
|
|
}
|
|
else
|
|
{
|
|
$this->_db->set($sess_id.'_expire', time(), 0);
|
|
}
|
|
|
|
if( $this->_db->get($sess_id) )
|
|
{
|
|
$this->_db->replace($sess_id, $session_data, $this->_compress);
|
|
}
|
|
else
|
|
{
|
|
$this->_db->set($sess_id, $session_data, $this->_compress);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
function destroy($id)
|
|
{
|
|
|
|
$sess_file = $this->_getFilePath($id);
|
|
@unlink($sess_file);
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
|
|
function gc($maxlifetime)
|
|
{
|
|
|
|
|
|
$sess_files = glob($this->_save_path . DIRECTORY_SEPARATOR . 'sess_*');
|
|
foreach( $sess_files as $filename)
|
|
{
|
|
if( filemtime($filename) + $maxlifetime < time() )
|
|
{
|
|
@unlink($filename);
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _getFilePath($id)
|
|
{
|
|
$sess_id = 'sess_'.$id;
|
|
return $this->_save_path . DIRECTORY_SEPARATOR . $sess_id . '.php';
|
|
//return $this->_save_path . DIRECTORY_SEPARATOR . $sess_id . '_' . $this->_session_name . '.php';
|
|
}
|
|
|
|
|
|
}
|