PHP实例一之简单的留言板

注:参考兄弟连教学视频写的一个简单的留言板模块,获得更多内容请参考我的博文。

示例:文本式留言板
需要的知识点:
  1.文件操作:
   file_put_contents();文件的写入函数
   file_get_contents();文件的读取函数
  2.字符串的处理函数
   explode();将字串拆分成数组的函数
   implode();将数组以指定分割符合并成字串的函数
  3.数组
     foreach() 遍历数组
     unset()  销毁变量
     全局数组:
     $_POST[]
     $_SERVER["REMOTE_ADDR"];//获取客户端的IP地址
     time();//获取当前系统的时间戳
     date();日期转换函数;
示例的目录结构:
====================================
|--index.php 添加留言信息界面
|
|--doAdd.php获取留言信息,并执行添加操作的php文件
|
|--show.php显示留言信息的界面
|
|--del.php 执行删除留言信息的界面
|
|--liuyan.txt 用于储存留言信息的文件

代码实现:

index.php部分:

  1. <html>  
  2. <head>  
  3. <title>我的留言板</title>  
  4. </head>  
  5. <body>  
  6. <center>  
  7.  <h2>我的留言板</h2>  
  8. <a href = "index.php">添加留言</a>  
  9. <a href = "show.php" >查看留言</a>  
  10. <hr width = "90%">  
  11. <h3>添加留言</h3>  
  12. <form action = "doAdd.php" method = "post">  
  13.  <table width = "380" border = "0" cellpadding = "4">  
  14.       <tr>  
  15.              <td  align = "right">标题:</td>  
  16.              <td><input type = "text" name = "title"></td>  
  17.      </tr>  
  18.       <tr>  
  19.              <td  align = "right">留言者:</td>  
  20.              <td><input type = "text" name = "author"></td>  
  21.      </tr>  
  22.      <tr>  
  23.              <td align = "right" valign = "top">留言内容:</td>  
  24.              <td><textarea name = "content" row = "5" cols = "30"></textarea></td>  
  25.      </tr>  
  26.       <tr>  
  27.                 
  28.          <td colspan = "2" align = "center"><input type = "submit" value = "提交">  
  29.                                    <input type = "reset" value = "重置"></td>  
  30.      </tr></table>  
  31. </form>  
  32. </center>  
  33.   
  34. </body>  
  35. </html>  

doAdd.php 添加留言部分

  1. <html>  
  2. <head>  
  3. <title>我的留言板</title>  
  4. </head>  
  5. <body>  
  6. <center>  
  7.  <h2>我的留言板</h2>  
  8. <a href = "index.php">添加留言</a>  
  9. <a href = "show.php" >查看留言</a>  
  10. <hr width = "90%">  
  11. <h3>添加留言</h3>  
  12.  <?php  
  13.     //执行留言信息添加操作  
  14. //1.获取要添加的留言信息,并补上其他辅助信息(ip地址、添加时间)  
  15. $title = $_POST["title"];  
  16. $author = $_POST["author"];  
  17. $content = $_POST["content"];  
  18. $ip = $_SERVER["REMOTE_ADDR"];  
  19. $addtime = time();  
  20. //2.拼装留言信息  
  21. $ly = "{$title}##{$author}##{$content}##{$ip}##{$addtime}@@@";  
  22. //echo $ly;  
  23. //3. 将留言添加到liuyan.txt文件中  
  24. $info = file_get_contents("liuyan.txt");  
  25. file_put_contents("liuyan.txt",$info.$ly);  
  26. echo "</br>";  
  27. //file_put_contents("liuyan.txt",$ly); 直接输出会覆盖上一条留言!  
  28. //4.输出留言成功!  
  29. echo "留言成功!";  
  30.   
  31.  ?>  
  32. </center>  
  33.   
  34. </body>  
  35. </html>  


show.php 留言显示部分:

  1. <html>  
  2. <head>  
  3. <title>我的留言板</title>  
  4. <script>  
  5. function dodel(id){  
  6.   if(confirm("确定要删除么?"))  
  7.   {  
  8.     window.location ='del.php?id='+id;   
  9.   }  
  10. }  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <center>  
  15.  <h2>我的留言板</h2>  
  16. <a href = "index.php">添加留言</a>  
  17. <a href = "show.php" >查看留言</a>  
  18. <hr width = "90%">  
  19. <h3>查看留言</h3>  
  20.  <table border = "1" width = "700" >  
  21.      <tr>  
  22.        <th>留言标题</th>  
  23.            <th>留言人</th>  
  24.            <th>留言内容</th>  
  25.        <th>IP地址</th>  
  26.  <th>留言时间</th>  
  27.  <th>操作</th>  
  28.     </tr>  
  29. <?php  
  30.   // 获取留言信息,解析后输出到表格中  
  31. // 1.从留言liuyan.txt中获取留言信息  
  32.   $info = file_get_contents("liuyan.txt");  
  33.   // 2.去除留言内容最后的三个@@@符号  
  34.   $info = rtrim($info,"@");  
  35.   if(strlen($info)>=8){  
  36.   // 3.以@@@符号拆分留言信息为一条一条的(将留言信息以@@@符号拆分成留言数组)  
  37.   $lylist = explode("@@@",$info);  
  38.      
  39.   // 4.遍历留言信息数组,对每条留言做再次解析;  
  40.   foreach($lylist as $k=>$v){  
  41.    $ly = explode("##",$v);  
  42.    echo "<tr>";  
  43.    echo "<td>{$ly[0]}</td>";  
  44.    echo "<td>{$ly[1]}</td>";  
  45.    echo "<td>{$ly[2]}</td>";  
  46.    echo "<td>{$ly[3]}</td>";  
  47.    echo "<td>".date("Y-m-d H:i:s",$ly[4])."</td>";  
  48.      echo "<td><a href = 'javascript:dodel({$k})'>删除</a></td>";  
  49.    
  50.   }  
  51.   }  
  52.      
  53. ?>  
  54. </center>  
  55.   
  56. </body>  
  57. </html>  


del.php 留言删除部分:

    1. <html>  
    2. <head>  
    3. <title>我的留言板</title>  
    4. </head>  
    5. <body>  
    6. <center>  
    7.  <h2>我的留言板</h2>  
    8. <a href = "index.php">添加留言</a>  
    9. <a href = "show.php" >查看留言</a>  
    10. <hr width = "90%">  
    11. <h3>删除留言</h3>  
    12.    
    13. <?php  
    14. //1. 获取要删除留言号  
    15. $id = $_GET["id"];  
    16. // 2.从留言liuyan.txt中获取留言信息  
    17.   $info = file_get_contents("liuyan.txt");  
    18.    
    19.   //3.(将留言信息以@@@符号拆分成留言数组)  
    20.   $lylist = explode("@@@",$info);  
    21.   //4.使用unset删除指定的id留言  
    22.   unset($lylist[$id]);  
    23.   //还原留言信息为字串,并写回留言文件  
    24.   $newinfo = implode("@@@",$lylist);  
    25.   file_put_contents("liuyan.txt",$newinfo);  
    26.   echo "删除成功!";  
    27.   
    28.    
    29.      
    30. ?>  
    31. </center>  
    32.   
    33. </body>  
    34. </html> 
posted @ 2017-08-25 13:42  郭帅  阅读(279)  评论(0)    收藏  举报