代码如下(摘自《php4.1 从入门到精通》)<?php
// Make sure to include class definition BEFORE session is started
class sleepy
{
var $status, $counter;
// constructor
function sleepy() {
$this->counter = 1;
$this->status = "I'm alive";
}
// Wakeup is called each time this object is deseralized
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");
}
}
// Initiate the session
session_start();
if (!isset($_SESSION["sleepy"]))
{
echo "do register";
session_register("sleepy");
$_SESSION["sleepy"] = new sleepy;
}
?>
<html>
<head>
<title>Session and Objects</title>
</head>
<body>
<?php
$buf = sprintf("Status: %s. Counter: %s",
$_SESSION["sleepy"]->status, $_SESSION["sleepy"]->counter);
echo $buf;
echo "<br />";
?>
</body>
</html>就这段代码,当把session.auto_start设置为0时 将成功显示
Status: I'm awake. Counter: 2 等字样
但当把session.auto_start设置为1时 却显示
Status: . Counter:
数字与字符串都无法显示出来 请问是何原因? 设置auto_start的话是否要做别的改动?
// Make sure to include class definition BEFORE session is started
class sleepy
{
var $status, $counter;
// constructor
function sleepy() {
$this->counter = 1;
$this->status = "I'm alive";
}
// Wakeup is called each time this object is deseralized
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");
}
}
// Initiate the session
session_start();
if (!isset($_SESSION["sleepy"]))
{
echo "do register";
session_register("sleepy");
$_SESSION["sleepy"] = new sleepy;
}
?>
<html>
<head>
<title>Session and Objects</title>
</head>
<body>
<?php
$buf = sprintf("Status: %s. Counter: %s",
$_SESSION["sleepy"]->status, $_SESSION["sleepy"]->counter);
echo $buf;
echo "<br />";
?>
</body>
</html>就这段代码,当把session.auto_start设置为0时 将成功显示
Status: I'm awake. Counter: 2 等字样
但当把session.auto_start设置为1时 却显示
Status: . Counter:
数字与字符串都无法显示出来 请问是何原因? 设置auto_start的话是否要做别的改动?
php 将自动执行 session_start()
如果程序中又执行了 session_start(),那么将造成 session 丢失