session_set_save_handler回调函数的形参有些什么?

xmlscript 2011-05-08 06:59:38
php.net 官网的示例如下:

session_set_save_handler("open", "close", "read", "write", "destroy", "gc");

function open($save_path, $session_name)
function close()
function read($id)
function write($id, $sess_data)
function destroy($id)
function gc($maxlifetime)

关键是,我无处可查哪里规定了这些参数,php.net官网哪里找呢,没写?还是我真的不会找了。。。
...全文
81 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄袍披身 2011-05-10
  • 打赏
  • 举报
回复

<?php
function open($save_path, $session_name)
{
global $sess_save_path;

$sess_save_path = $save_path;
return(true);
}

function close()
{
return(true);
}

function read($id)
{
global $sess_save_path;

$sess_file = "$sess_save_path/sess_$id";
return (string) @file_get_contents($sess_file);
}

function write($id, $sess_data)
{
global $sess_save_path;

$sess_file = "$sess_save_path/sess_$id";
if ($fp = @fopen($sess_file, "w")) {
$return = fwrite($fp, $sess_data);
fclose($fp);
return $return;
} else {
return(false);
}

}

function destroy($id)
{
global $sess_save_path;

$sess_file = "$sess_save_path/sess_$id";
return(@unlink($sess_file));
}

function gc($maxlifetime)
{
global $sess_save_path;

foreach (glob("$sess_save_path/sess_*") as $filename) {
if (filemtime($filename) + $maxlifetime < time()) {
@unlink($filename);
}
}
return true;
}

session_set_save_handler("open", "close", "read", "write", "destroy", "gc");

session_start();

// proceed to use sessions normally

?>

黄袍披身 2011-05-10
  • 打赏
  • 举报
回复
哪里规定了这些参数是什么参数? open close read?

open
Open function, this works like a constructor in classes and is executed when the session is being opened. The open function expects two parameters, where the first is the save path and the second is the session name.
close
Close function, this works like a destructor in classes and is executed when the session operation is done.
read
Read function must return string value always to make save handler work as expected. Return empty string if there is no data to read. Return values from other handlers are converted to boolean expression. TRUE for success, FALSE for failure.
write
Write function that is called when session data is to be saved. This function expects two parameters: an identifier and the data associated with it.


Note:

The "write" handler is not executed until after the output stream is closed. Thus, output from debugging statements in the "write" handler will never be seen in the browser. If debugging output is necessary, it is suggested that the debug output be written to a file instead.


destroy
The destroy handler, this is executed when a session is destroyed with session_destroy() and takes the session id as its only parameter.
gc
The garbage collector, this is executed when the session garbage collector is executed and takes the max session lifetime as its only parameter.

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧