session的问题,书中的源码得不到正常结果

fountain521 fountain521 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
<?php
// Make sure to include class definition BEFORE session is started
class sleepy
{
var $status;
var $counter;

// Constructor
function sleepy()
{
$this->counter = 1;
$this->status = "I'm alive";
}

// Wakeup is called each time this object is deserialized
function __wakeup()
{
$this->counter++;
$this->status = "I'm awake";
}

// Sleep is called each time this object is serialized
function __sleep()
{
$this->status = "I'm asleep";
// If this array is not returned, the object is not serialized
return array("status", "counter");
}
} // end class sleepy

// Initiate the session
session_start();

if (!isset($_SESSION["sleepy_obj"]))
{
$sleepy_obj = new sleepy;
session_register("sleepy_obj");
}
?>
<html>
<head>
<title>Session and Objects</title>
</head>
<body>
<?php
$buf = sprintf("Status: %s. Counter: %s.", $_SESSION["sleepy_obj"]->status, $_SESSION["sleepy_obj"]->counter);
echo $buf;
?>
</body>
</html>
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
fountain521
基础编程 21,889 成员 140,329 内容