转载请注明出处:http://blog.csdn.net/u011569040/article/details/46993735
index.php
<html>
<head>
<title>我的留言板</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<?php include("menu.php");?>
<h3>添加留言</h3>
<form action="doAdd.php" method="post">
<table width="380" border="0" cellpadding="4">
<tr>
<td>标题:</td>
<td><input type="text" name="title"/></td>
</tr>
<tr>
<td>留言者:</td>
<td><input type="text" name="author"/></td>
</tr>
<tr>
<td align="left" valign="top">留言内容:</td>
<td><textarea name="content" rows="5" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
show.php<html>
<head>
<title>我的留言板</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
//弹框是否删除
function dodel(id){
if(confirm("确定要删除吗?")){
window.location='del.php?id='+id;
}
}
</script>
</head>
<body>
<center>
<?php include("menu.php");?>
<h3>添加留言</h3>
<table border="1" width="900">
<tr>
<th>留言标题</th>
<th>留言人</th>
<th>留言内容</th>
<th>IP地址</th>
<th>留言时间</th>
<th>操作 </th>
</tr>
<?php
//获取留言信息,解析后输出到表格中
//1.从留言liuyan.txt信息文件中获取留言信息
$info=file_get_contents("liuyan.txt");
//2.获取留言内容最后三个@@@符号
$info=rtrim($info,"@");
if(strlen($info)>=8){
//3.以@@@拆分留言信息为一条一条的,拆分成留言数组
$lylist = explode("@@@",$info);
//4.遍历留言信息数组,对每条留言做再次解析
foreach($lylist as $k=>$v){
$ly = explode("##",$v);
echo "<tr>";
echo "<td>{$ly[0]}</td>";
echo "<td>{$ly[1]}</td>";
echo "<td>{$ly[2]}</td>";
echo "<td>{$ly[3]}</td>";
echo "<td>".date("Y-m-d H:i:s",$ly[4]+8*3600)."</td>";
echo "<td><a href='javascript:dodel({$k})'>删除</a></td>";
echo "</tr>";
}
}
?>
</table>
</center>
</body>
</html>
doAdd.php<html>
<head>
<title>我的留言板</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<?php include("menu.php");?>
<h3>添加留言</h3>
<?php
//1.获取要添加的留言信息,并且补上其它辅助信息(ip地址、添加时间)
$title=$_POST["title"];
$author=$_POST["author"];
$content=$_POST["content"];
$ip=$_SERVER["REMOTE_ADDR"];
$addtime = time();
//2.拼装(组装)留言信息
$ly = "{$title}##{$author}##{$content}##{$ip}##{$addtime}@@@";
//echo $ly;
//3.将留言信息追加到liuyan.txt文件中
$info=file_get_contents("liuyan.txt");//得到以前的数据,连接后就不会覆盖了
file_put_contents("liuyan.txt",$info.$ly);//文件写入(会覆盖原有数据)
//4.输出留言成功
echo "留言成功,谢谢!!!";
?>
</center>
</body>
</html>
del.php<html>
<head>
<title>我的留言板</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<?php include("menu.php");?>
<h3>删除留言</h3>
<?php
//执行删除指定id的留言信息
//1.获取要留言的id号
$id = $_GET["id"];
//2.从liuyan.txt中获取留言信息
$info=file_get_contents("liuyan.txt");
//3.将留言信息以@@@为符号拆分成留言数组
$lylist = explode("@@@",$info);
//4.使用unset删除指定的id留言
unset($lylist[$id]);
//5.还原留言信息为字符串,并写回liuyan.txt
$ninfo = implode("@@@",$lylist);
file_put_contents("liuyan.txt",$ninfo);
echo "删除成功";
?>
</table>
</center>
</body>
</html>
menu.php
<h2>我的留言板</h2>
<a href="index.php">添加留言</a>
<a href="show.php">查看留言</a>
<hr width="90%"/>
下载