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

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

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

本篇关键词:代码处理内存利用
黑客防线网安网讯:     本段代码利用共享内存处理session数据。 <?php define("SESSION_PATH_HANDLE","session.data"); define("SESSION_INDEX_HANDLE","index.data"); define("SESSION_INDEX_SIZE",1048576)...

     本段代码利用共享内存处理session数据


<?php
define("SESSION_PATH_HANDLE","session.data");
define("SESSION_INDEX_HANDLE","index.data");
define("SESSION_INDEX_SIZE",1048576);
define("SESSION_DATA_SIZE",10485760);
define("SHARED_SESS_TIME",3600);
class ShareMemSession
{

function init()
{
if(!file_exists(SESSION_INDEX_HANDLE))
{
$handle=fopen(SESSION_INDEX_HANDLE,"wb");
fwrite($handle,"I will use this file for session index");
fclose($handle);
$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=array();
$shm_put_var($shm_index_id,1,$session_index);
shm_detach($shm_index_id);
sem_release($sem_index_id);
}
if(!file_exists(SESSION_PATH_HANDLE))
{
$handle=fopen(SESSION_PATH_HANDLE,"wb");
fwrite($handle,"I will use this file for session");
fclose($handle);
$shm_data_key=ftok(SESSION_PATH_HANDLE,"c");
$sem_data_id=sem_get($shm_data_key,1,0644,0);
$shm_data_id=shm_attach( $shm_data_key,SESSION_DATA_SIZE); 
shm_detach($shm_data_id);
sem_release($sem_data_id);
}
ini_set(’session.use_trans_sid’, 0);
//设置垃圾回收最大生存时间
ini_set(’session.gc_maxlifetime’, SHARED_SESS_TIME);

//使用 COOKIE 保存 SESSION ID 的方式
ini_set(’session.use_cookies’, 1);
ini_set(’session.cookie_path’, "/");
//多主机共享保存 SESSION ID 的 COOKIE
// ini_set(’session.cookie_domain’, $domain);
session_module_name("user");
session_set_save_handler(
array("ShareMemSession", "open"),
array("ShareMemSession", "close"),
array("ShareMemSession", "read"),
array("ShareMemSession", "write"),
array("ShareMemSession", "destroy"),
array("ShareMemSession", "gc")
);

}
function open($save_path, $session_name)
{
return true;
}

function close(){
return true;
}

function read($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])) return "";
$tmp_session_id=$session_index[$session_id]["session_hash_id"];//得到当前session的hash id
  $session_index[$session_id]["lastvisit"]=time();
shm_put_var($shm_index_id,1,$session_index);
shm_detach($shm_index_id);
sem_release($sem_index_id);
}
else 
{
shm_detach($shm_index_id);
sem_release($sem_index_id);
return "";
}
$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_get_var($shm_data_id,$tmp_session_id);
shm_detach($shm_data_id);
sem_release($sem_data_id);
return $result;
}

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14007-1.html
网站维护教程更新时间:2012-03-30 05:07:25  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer