《php程序设计》这本书中的这个例子是错的,大家帮我看看如何更正这个错误
vvhot 2004-08-09 02:37:15 第一个文件:log.inc
<?php
// this file is Log.inc
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) {
fwrite($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');
}
}
?>
第二个文件:front.php
<?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("f:\public\log");
session_register('l');
$l->write("Created $now");
echo("Created 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>
第三个文件:next.php
<?php
include_once('Log.inc');
session_start();
?>
<html><head><title>Next Page</title></head>
<body>
<?php
$now = strftime("%c");
$l->write("Viewed page 2 at $now");
echo "The log contains:<p>";
echo nl2br($l->read());
?>
</body></html>
运行front.php,结果正常,但点击结果中的 Move to the next page 链接后跳转到next.php时出错,提示:
Notice: Undefined variable: l in F:\public\next.php on line 9
Fatal error: Call to a member function write() on a non-object in F:\public\next.php on line 9
怎样更正?我用的是php5,win2003,apache2.0.50
试过了,在php4下一样出错