PHP 下利用共享内存处理seion的代码(2)_PHP应用_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

PHP 下利用共享内存处理seion的代码(2)

作者:黑客防线网安PHP维护基地 来源:黑客防线网安PHP维护基地 浏览次数:0

本篇关键词:代码处理内存利用
黑客防线网安网讯:     function write($session_id, $data_value) { $tmp_session_id=-1; //first ,I will read session index from share_mem $shm_index_key=ftok(SESSION_INDEX_HANDLE,"c"); $sem_index_...
     function write($session_id, $data_value)
{
$tmp_session_id=-1;
//first ,I will read session index from share_mem
$shm_index_key=ftok(SESSION_INDEX_HANDLE,"c");
$sem_index_id=sem_get($shm_index_key,1,0644,0);
sem_acquire($sem_index_id);
$shm_index_id=shm_attach( $shm_index_key,SESSION_INDEX_SIZE);
$session_index=shm_get_var($shm_index_id,1);
if($session_index)
{
if(is_array($session_index) && isset($session_index[$session_id])) 
{
$tmp_session_id=$session_index[$session_id]["session_hash_id"];//得到当前session的hash id
$session_index[$session_id]["lastvisit"]=time();
}
else
{
$hash_id=ShareMemSession::hash($session_id);
while(array_search($hash_id,$session_index))
{
if ($hash_id<5000)
$hash_id=5000;
else
$hash_id++;
}
$tmp_index=array();
$tmp_index["session_hash_id"]=$hash_id;
$tmp_index["lastvisit"]=time();
$session_index[$session_id]=$tmp_index;
$tmp_session_id=$hash_id;
}
shm_put_var($shm_index_id,1,$session_index);
}
else
{
shm_detach($shm_index_id);
sem_release($sem_index_id);
return false;
}
$shm_data_key=ftok(SESSION_PATH_HANDLE,"c");
$sem_data_id=sem_get($shm_data_key,1,0644,0);
sem_acquire($sem_data_id);
$shm_data_id=shm_attach( $shm_data_key,SESSION_DATA_SIZE); 
$result=shm_put_var($shm_data_id,$tmp_session_id,$data_value);
shm_detach($shm_data_id);
sem_release($sem_data_id);
return true;
}
function destroy($session_id){
$tmp_session_id=-1;
//first ,I will read session index from share_mem
$shm_index_key=ftok(SESSION_INDEX_HANDLE,"c");
$sem_index_id=sem_get($shm_index_key,1,0644,0);
sem_acquire($sem_index_id);
$shm_index_id=shm_attach( $shm_index_key,SESSION_INDEX_SIZE);
$session_index=shm_get_var($shm_index_id,1);
if($session_index)
{
if(is_array($session_index) && isset($session_index[$session_id])) 
{
$tmp_session_id=$session_index[$session_id]["session_hash_id"];//得到当前session的hash id
  unset($session_index[$session_id]);
shm_put_var($shm_index_id,1,$session_index);
shm_detach($shm_index_id);
sem_release($sem_index_id);
}
}
if($tmp_session_id!=-1)
{
$shm_data_key=ftok(SESSION_PATH_HANDLE,"c");
$sem_data_id=sem_get($shm_data_key,1,0644,0);
sem_acquire($sem_data_id);
$shm_data_id=shm_attach( $shm_data_key,SESSION_DATA_SIZE); 
$result=shm_remove_var($shm_data_id,$tmp_session_id);
shm_detach($shm_data_id);
sem_release($sem_data_id);
}
return true;
}
function gc($maxlifetime = SHARED_SESS_TIME)
{
$shm_index_key=ftok(SESSION_INDEX_HANDLE,"c");
$sem_index_id=sem_get($shm_index_key,1,0644,0);
sem_acquire($sem_index_id);
$shm_index_id=shm_attach( $shm_index_key,SESSION_INDEX_SIZE);
$session_index=shm_get_var($shm_index_id,1);
$tmpresult=array();
$check_time=time();
if($session_index)
{
foreach($session_index as $key=>$value)
{
if($value["lastvisit"]+$maxlifetime<$check_time)
$tmpresult[]=array("session_id"=>$key,"hash_id"=>$value["session_hash_id"]);
// $tmp_session_id=$session_index[$session_id]["session_hash_id"];//得到当前session的hash id
// unset($session_index[$session_id]);
}
$shm_data_key=ftok(SESSION_PATH_HANDLE,"c");
$sem_data_id=sem_get($shm_data_key,1,0644,0);
sem_acquire($sem_data_id);
foreach($tmpresult as $item)
{
$tmp_session_id=$item["session_id"];//得到当前session的hash id
unset($session_index[$session_id]);
$result=shm_remove_var($shm_data_id,$item["hash_id"]);
}
shm_put_var($shm_index_id,1,$session_index);
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14008-1.html
网站维护教程更新时间:2012-03-30 05:07:25  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer