PHP文件操作之留言板的实现
一、引导页
<div align="center">
<h1>基于文件的留言本</h1>
<?php
date_default_timezone_set('PRC');
@$string = file_get_contents('message.txt');
if(!empty($string)) {
$string = rtrim($string, '&^');
$arr = explode('&^', $string);
echo "<br />";
foreach ($arr as $value) {
list($username, $content, $time) = explode('&#', $value);
echo "用户名为:<font color='green'>{$username}</font>   留言内容:<font color='red'>{$content}</font> <font>时间为:" . date('Y-m-d H:i:s', $time) . "</font>";
echo "<br />";
}
}
?>
<form action="write.php" method="post">
用户名:<input type="text" name="username" /><br />
留言内容:<textarea name="content"></textarea><br />
<input type="submit" value="提交">
</form>
</div>
二、文件写入页面
<?php
$filename = "message.txt";
$fp = fopen($filename,'a');
$time = time();
$username = trim($_POST['username']);
$content = trim($_POST['content']);
$string = $username."&#".$content."&#".$time."&^";
fwrite($fp,$string);
fclose($fp);
header('location:index.php');