php学习--简单的IO读写操作实现记录功能

log.php
+展开
-PHP
<?php
class log
{
var $filename,$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()
{
fclose($this->fp);
return array('filename');
}
}
?>


first.php
+展开
-HTML
<?php
include("log.php");
?>
<html>
  <head><title>First Page</title></head>
  <body>
  <?php
  $now=strftime("%c");
$l=new log("log");
$l->write("Create $now");
echo "Create session and log object;<br/>";
$l->write("Viewed fist page $now");
echo "The log contains:<br/>";
echo nl2br($l->read());
  ?>
  <br>
  <a href="next.php">Next Page</a>
  </body>
</html>


next.php
+展开
-PHP
<?php
include("log.php");
?>
<html>
  <head><title>Next Page</title></head>
  <body>
  <?php
  $now=strftime("%c");
  $l=new log("log");
  $l->write("Viewed next page at $now");
  echo "The log contains:<br/>";
  echo nl2br($l->read());
  ?>
  </body>
</html>

加支付宝好友偷能量挖...


原创文章,转载请注明出处:php学习--简单的IO读写操作实现记录功能

评论(0)Web开发网
阅读(69)喜欢(0)PHP/apache/Perl