www/test/mess文件夹下的两个文件:index.php和mess.db
其实现的一个简单版本:
index.php
<?php
//接收表单数据
$str = $_GET['mname']."<hr>";
file_put_contents("mess.db" , $str , FILE_APPEND);
?>
<!DOCTYPE html>
<html>
<head>
<!--meta charset="UTF-8"-->
<title>mess</title>
</head>
<body>
<h2>文件留言板</h2>
<hr>
<form action="index.php">
<h2>请留言:</h2>
<textarea name="mname" id="" cols="100" rows="10"></textarea>
<br>
<input type="submit" value="send">
</form>
<h2>看留言:</h2>
<?php
readfile("mess.db");
?>
</body>
</html>
将index分解成两文件index.php和rev.php
index.php
<!DOCTYPE html>
<html>
<head>
<!--meta charset="UTF-8"-->
<title>mess</title>
</head>
<body>
<form action="rev.php" method="post">
<h2>请留言:</h2>
<textarea name="mname" id="" cols="100" rows="10"></textarea>
<br>
<input type="submit" value="send">
</form>
<h2>看留言:</h2>
<?php
echo file_get_contents("mess.db");
?>
</body>
</html>
rev.php
<?php
//header('content-type:text/html;charset=utf-8');
$filename = "mess.db";
$str = $_POST['mname'];
if(!empty($str)) {
$str .= "<hr>";
file_put_contents($filename,$str,FILE_APPEND);
echo "<script>location='index.php'</script>";
}
else {
echo "<script>alert('留言不能为空!')</script>";
echo "<script>location='index.php'</script>";
}
?>