- Rongsen.Com.Cn 版权所有 2008-2010 京ICP备08007000号 京公海网安备11010802026356号 朝阳网安编号:110105199号
- 北京黑客防线网安工作室-黑客防线网安服务器维护基地为您提供专业的
服务器维护
,企业网站维护
,网站维护
服务 - (建议采用1024×768分辨率,以达到最佳视觉效果) Powered by 黑客防线网安 ©2009-2010 www.rongsen.com.cn
作者:黑客防线网安PHP维护基地 来源:黑客防线网安PHP维护基地 浏览次数:0 |
本段代码利用共享内存处理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;
}
我要申请本站:N点 | 黑客防线官网 | |
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479 |