21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
class Log {
var $filename;
var $ fp;
function Log($filename) {
$this->filename = $filename;
$this->open();
}
function open() {
$this->fp = fopen($this->filename, "a") or die("Can't open {$this->filename}");
}
function write($note) {
fputs($this->fp, "$note\n");
}
function read() {
return join(' ', file($this->filename));
}
function __wakeup() {
$this->open();
}
function __sleep() {
// write information to the account file
fclose($this->fp);
return array('filename');
}
}
?>
<?php
include_once('Log.inc');
session_start();
?>
<html><head><title>Front Page</title></head>
<body>
<?php
$now = strftime("%c");
if (!session_is_registered('l')) {
$l = new Log("persistent_log");
session_register('l');
$l->write("Create $now");
echo("Create session and persistent log object.<p>");
}
$l->write("Viewed first page $now");
echo "The log contains:<p>";
echo nl2br($l->read());
?>
<a href = "next.php">Move to the next page</a>
</body>
</html>
<?php
include_once('Log.inc');
session_start();
?>
<html><head><title>Next Page</title></head>
<body>
<?php
$now = strftime("%c");
$l->write("Viewed page2 at $now");
echo "The log contains:<p>";
echo nl2br($l->read());
?>
</body>
</html>