首页 home.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文本式留言板</title>
</head>
<body>
<?php include('head.php'); ?>
<center>
<form action="add.php" method="post">
<table width='400' border='0' cellpadding='4'>
<tr><td align="right">留言标题:</td><td><input type="text" name="title"></td></tr>
<tr><td align="right">留言者:</td><td><input type="text" name="author"></td></tr>
<tr><td align="right" valign='top'>留言内容:</td><td><textarea name="content" rows='5' cols='30'></textarea></td></tr>
<tr><td colspan='2' align="center"><input type="submit" name="sub" value="提交">
<input type="reset" name="reset" value="重置"></td></tr></table>
</form>
</center>
</body>
</html>
添加留言页面 add.php
<?php
include('head.php');
echo "<center>";
if(isset($_POST['sub'])){
$title=$_POST['title'];
$author=$_POST['author'];
$content=$_POST['content'];
$ip=$_SERVER['REMOTE_ADDR'];
$time=time();
$ly=$title.'##'.$author.'##'.$content.'##'.$ip.'##'.$time.'@@@';
$notes=$ly.@file_get_contents("liuyan.txt");
file_put_contents("liuyan.txt",$notes);
echo "留言成功<br>";
echo "<a href='home.php'>返回首页</a>";
}
echo "</center>";
显示留言页面 show.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>显示留言</title>
<style>
td {text-align:center}
</style>
<script>
function check($id){
if(confirm('确定要删除吗?')){
window.location='del.php?id='+$id;
}
}
</script>
</head>
<body>
<?php
include('head.php');
?>
<center>
<table width="800" border="1" >
<tr bgcolor="#ccc"><th>ID</th><th>标题</th><th>留言者</th><th>内容</th><th>IP</th><th>时间</th><th>操作</th></tr>
<?php
$notes=file_get_contents('liuyan.txt');
if(!empty($notes)){
$notes=rtrim($notes,'@');
$arr=explode('@@@',$notes);
for($i=0;$i<count($arr);$i++){
$note[$i]=explode('##',$arr[$i]);
echo "<tr>";
echo "<td>{$i}</td>";
echo "<td>{$note[$i][0]}</td>";
echo "<td>{$note[$i][1]}</td>";
echo "<td>{$note[$i][2]}</td>";
echo "<td>{$note[$i][3]}</td>";
echo "<td>{$note[$i][4]}</td>";
echo "<td><a href='javascript:check({$i})'>删除</a></td>";
echo "</tr>";
}
}
?>
</table>
</center>
</body>
</html>
删除留言 del.php
<?php
include('head.php');
echo "<center>";
$id=$_GET['id'];
$notes=file_get_contents('liuyan.txt');
$list=explode('@@@',$notes);
//unset()没有返回值
unset($list[$id]);
$notes=implode('@@@',$list);
file_put_contents('liuyan.txt',$notes);
echo "留言删除成功<br>";
echo "<a href='show.php'>返回</a>";
echo "</center>";
公共头部 head.php
<center><h1>文本式留言板</h1>
<h3><a href="home.php">添加留言</a></h3>
<h3><a href="show.php">查看留言</a></h3>
<hr>
</center>