PHP文本式留言板——php经典实例

文件结构:

index.php  主页和添加页

show.php  查看留言页

ly.db  文本保存页

doAdd.php  添加功能页

doDel.php  删除功能页

 index.php

 1 <html>
 2     <head>
 3         <title>文本式留言板</title>
 4         <meta charset='utf-8' />
 5     </head>
 6     <body>
 7         <center>
 8             <h1>文本式留言板</h1>
 9             <a href='index.php'>添加留言</a>
10             &nbsp;&nbsp;&nbsp;&nbsp;
11             <a href='show.php'>查看留言</a>
12             <hr/>
13             <form action='doAdd.php' method='post'>
14                 <table  width='500' cellpadding='10' >
15                     <tr>
16                         <td align='right'>标题</td>
17                         <td><input type='text' name='title' size='30' /></td>
18                     </tr>
19                     <tr>
20                         <td align='right'>作者</td>
21                         <td><input type='text' name='author' /></td>
22                     </tr>
23                     <tr>
24                         <td align='right'>内容</td>
25                         <td><textarea name='content' cols='50' rows='6'></textarea></td>
26                     </tr>
27                     <tr align='center'>
28                         <td colspan='2'>
29                             <input type='submit' value='添加' />
30                             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
31                             <input type='reset' value='清空' />
32                         </td>
33                     </tr>
34                 </table>
35             </form>
36         </center>
37     </body>
38 </html>

 show.php

 1 <html>
 2     <head>
 3         <title>文本式留言板</title>
 4         <meta charset='utf-8' />
 5     </head>
 6     <body>
 7         <center>
 8             <h1>文本式留言板</h1>
 9             <a href='index.php'>添加留言</a>
10             &nbsp;&nbsp;&nbsp;&nbsp;
11             <a href='show.php'>查看留言</a>
12             <hr/>
13             
14             <table  width='800' border='1' cellpadding='5' >
15                 <tr>
16                     <th>标题</th>
17                     <th>作者</th>
18                     <th>内容</th>
19                     <th>i p</th>
20                     <th>时间</th>
21                     <th>操作</th>
22                 </tr>
23                 <?php
24                     //读文件
25                     $info=rtrim(file_get_contents("ly.db"),"@");
26                     if($info != ""){
27                         //拆分
28                         $list=explode("@@",$info);
29                         
30                         //var_dump($list);
31                         //遍历
32                         foreach($list as $k=>$v){
33                         
34                             //拆分v
35                             $oncely=explode("##",$v);
36 
37                             echo "<tr>";
38                                 echo "<td>{$oncely[0]}</td>";
39                                 echo "<td>{$oncely[1]}</td>";
40                                 echo "<td>{$oncely[2]}</td>";
41                                 echo "<td>{$oncely[3]}</td>";
42                                 echo "<td>{$oncely[4]}</td>";
43                                 echo "<td><a href='doDel.php?k={$k}'>删除</a></td>";
44                             echo "</tr>";
45                         }
46                     
47                     }else{
48                         echo "无数据";
49                     }
50                     
51                 
52                 
53                 ?>
54             </table>
55         
56         </center>
57     </body>
58 </html>

doAdd.php

 1 <?php
 2     if(empty($_POST)){
 3         //如果不是post提交自动跳回
 4         header("location:index.php");
 5         die;
 6     }
 7     //标题不能为空
 8     if($_POST['title']==""){
 9         echo "标题不能为空!";
10         header("refresh:1;url=index.php");
11         die;
12     }
13     //作者不能为空
14     if($_POST['author']==""){
15         echo "作者不能为空!";
16         header("refresh:1;url=index.php");
17         die;
18     }
19     //内容不能为空
20     if($_POST['content']==""){
21         echo "内容不能为空!";
22         header("refresh:1;url=index.php");
23         die;
24     }
25     
26     //拼接字符串
27     $info=$_POST['title']."##".$_POST['author']."##".$_POST['content'].
28     "##".$_SERVER['REMOTE_ADDR']."##".date("Y-m-d H:i")."@@";
29     
30     //写入db文件
31     file_put_contents("ly.db",$info,FILE_APPEND);
32     
33     echo "ok";
34     header("refresh:1;url=index.php");
35     
36     
37 
38 ?>

doDel.php

 1 <?php
 2 
 3     if(!isset($_GET['k'])){
 4         //k没传过来直接跳回
 5         header("location:show.php");
 6         die;
 7     }
 8     
 9     $k=$_GET['k'];
10     
11     //读文件
12     $info=rtrim(file_get_contents("ly.db"),"@");
13     
14     //拆分
15     $list=explode("@@",$info);
16     
17     //删除
18     unset($list[$k]);
19     
20     if(count($list)>0){
21         //数组转字符串回填到db文件
22         file_put_contents("ly.db",implode("@@",$list)."@@");
23     }else{
24         //数组为空回填一个空字符
25         file_put_contents("ly.db","");
26     }
27     
28     echo "ok";
29     header("refresh:1;url=show.php"); 
30 
31 ?>

 下载地址:http://files.cnblogs.com/files/wordblog/wblyb.zip

原文地址:https://www.cnblogs.com/wordblog/p/6848740.html