网站地图    收藏   

主页 > php专栏 > php综合实列 >

php简易留言板程序代码 - 综合实例

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] 这是一个最基础的留言板程序了,但是己经有了留言板程序基本功能,很适合于php初学者用用,学习用啊,当然也可以用于企业网站也是很不错的...

php简易留言板程序代码

这是一个最基础的留言板程序了,但是己经有了留言板程序基本功能,很适合于php初学者用用,学习用啊,当然也可以用于企业网站也是很不错的哦。

index.php

  1. <?php 
  2. session_start(); 
  3. $con=mysql_connect('localhost','root','root') or die('链接数据库失败!'); 
  4. mysql_query('set names utf8'); 
  5. mysql_select_db('GuestBook'); 
  6. $pagesize = 10;//每一页显示多少留言记录 
  7. if(isset($_GET['page'])&&$_GET['page']!='') $page=$_GET['page']; 
  8. else $page=0; 
  9. $sql = "SELECT a . * , b.name, b.email, b.qq, c.revert_time, c.revert 
  10.   FROM post a 
  11.   LEFT JOIN revert c ON ( a.id = c.post_id ) , guest b 
  12.   WHERE a.guest_id = b.id 
  13.   ORDER BY a.id DESC"; 
  14. $numRecord = mysql_num_rows(mysql_query($sql)); 
  15. $totalpage = ceil($numRecord/$pagesize); 
  16. $recordSql = $sql. " LIMIT ".$page*$pagesize.",".$pagesize; 
  17. $result = mysql_query($recordSql); 
  18. ?> 
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  20. <html xmlns="http://www.w3.org/1999/xhtml"> 
  21. <head> 
  22. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  23. <title>PHPiask简易留言板</title> 
  24. <style type="text/css"> 
  25. <!-- 
  26. body { 
  27.  margin-left: 0px; 
  28.  margin-top: 0px; 
  29. } 
  30. a:link { 
  31.  text-decoration: none; 
  32.  color: #FF6600; 
  33. } 
  34. a:visited { 
  35.  text-decoration: none; 
  36. } 
  37. a:hover { 
  38.  text-decoration: underline; 
  39. } 
  40. a:active { 
  41.  text-decoration: none; 
  42. } 
  43. .STYLE1 { 
  44.  color: #FFFFFF; 
  45.  font-weight: bold; 
  46.  font-size: 16px; 
  47. } 
  48. td{ 
  49. font-size:12px; 
  50. } 
  51. .tdhx { 
  52.  font-style: italic; 
  53.  line-height: 1.5; 
  54.  text-decoration: underline; 
  55. } 
  56. --> 
  57. </style> 
  58. <script language="javascript"> 
  59. function checkInput(){ 
  60.  var Email = document.getElementById('email'); 
  61.  var QQ = document.getElementById('qq'); 
  62.  var name = document.getElementById('name'); 
  63.  var post = document.getElementById('post'); 
  64.  //验证用户名:不能超过10个字符(5个汉字),不能输入非法字符,不能为空 
  65.  nameValue = name.value.replace(/s+/g,""); 
  66.  var SPECIAL_STR = "~!%^&*();"?><[]{}\|,:/=+—"; 
  67.  var nameflag=true; 
  68.  for(i=0;i<nameValue.lenght;i++){ 
  69.   if (SPECIAL_STR.indexOf(nameValue.charAt(i)) !=-1) 
  70.   nameflag=false; 
  71.  } 
  72.  if(nameValue==''){ 
  73.   alert('请填写用户名称!');  
  74.   return false; 
  75.  } 
  76.  if(nameValue.length>10){ 
  77.   alert('用户名称最多10个字符(5个汉字)!'); 
  78.   return false; 
  79.  } 
  80.   
  81.  if(nameflag===false){ 
  82.   alert('用户名称不能包含非法字符请更改!'); 
  83.   return false; 
  84.  } 
  85.  //验证QQ号码 
  86.  var par =/^[1-9]d{4,12}$/; 
  87.  if(QQ.value!=''&&!par.test(QQ.value)){ 
  88.   alert('请输入正确的QQ号码'); 
  89.   return false; 
  90.  } 
  91.  //验证Email地址 
  92.  var emailpar = /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/; 
  93.  if(Email.value!=''&&!emailpar.test(Email.value)){ 
  94.   alert('请输入正确的邮箱地址!'); 
  95.   return false; 
  96.  } 
  97.  if(QQ.value==''&&Email.value==''){ 
  98.   alert('邮箱和QQ必选其一'); 
  99.   return false;   
  100.  } 
  101.  if(post.value==""){ 
  102.   alert('请输入留言内容!'); 
  103.   return false;    
  104.  } 
  105.  if(post.value.length>400){ 
  106.   alert('留言内容太长!'); 
  107.   return false;    
  108.  } 
  109. } 
  110. </script> 
  111. </head> 
  112. <body> 
  113. <table width="800" border="0" align="center"> 
  114.   <tr> 
  115.     <td height="80" bgcolor="#003366"><span class="STYLE1"> 简易留言板教程(<a href="http://www.phpiask.com">PHP iask</a>)</span></td> 
  116.   </tr> 
  117.   <tr> 
  118.     <td height="5" bgcolor="#efefef"></td> 
  119.   </tr> 
  120. </table> 
  121. <table width="800" border="0" align="center" bgcolor="#fefefe"> 
  122. <?php 
  123. while($rs=mysql_fetch_object($result)){ 
  124. ?> 
  125.   <tr> 
  126.     <td class="tdhx">留言人:<?php echo $rs->name?> |Email:<?php echo $rs->email?>|QQ:<?php echo $rs->qq?>|留言时间:<?php echo date("Y-m-d H:i:s",$rs->post_time+8*3600)?></td> 
  127.   </tr> 
  128.   <?php 
  129.   if(isset($_SESSION['login'])&&$_SESSION['login']){ 
  130.   ?> 
  131.     <tr> 
  132.     <td class="tdhx"><a href="revert.php?id=<?php echo $rs->id?>">回复</a> | <a href="delete.php?id=<?php echo $rs->id?>">删除</a></td> 
  133.   </tr> 
  134.   <?php 
  135.   } 
  136.   ?> 
  137.   <tr> 
  138.     <td>留言内容:<?php echo nl2br(htmlspecialchars($rs->post))?><br/> 
  139.     <font color="Red"> 
  140.     回复内容:<?php echo nl2br(htmlspecialchars($rs->revert))?>[<?php if($rs->revert_time!="") echo date("Y-m-d H:i:s",$rs->revert_time+8*3600)?> ] 
  141.     </font> 
  142.      
  143.     </td> 
  144.   </tr> 
  145.   <tr><td height="3px"  bgcolor="##FF6600"></td></tr> 
  146. <?php 
  147. } 
  148. ?> 
  149. </table> 
  150. <table width="800" border="0" align="center" bgcolor="#B1C3D9"> 
  151.   <tr> 
  152.     <td > 
  153. <?php  
  154. if($page>0) echo "<a href='index.php?page=".($page-1)."'>上一页|</a>" ; 
  155. if($page<$totalpage-1) echo "<a href='index.php?page=".($page+1)."'>下一页</a>" ; 
  156. ?></td> 
  157.   </tr> 
  158. </table><form action="post.php" method="post" id="postForm" name="postForm"> 
  159. <table width="800" border="0" align="center" cellspacing="1" bgcolor="#efefef"> 
  160.    
  161.   <tr> 
  162.     <td width="117" bgcolor="#FFFFFF">姓名:</td> 
  163.     <td width="673" bgcolor="#FFFFFF"><label> 
  164.       <input type="text" name="name" id="name" /> 
  165.     </label></td> 
  166.   </tr> 
  167.   <tr> 
  168.     <td bgcolor="#FFFFFF">Email:</td> 
  169.     <td bgcolor="#FFFFFF"><label> 
  170.       <input type="text" name="email" id="email" /> 
  171.     </label></td> 
  172.   </tr> 
  173.   <tr> 
  174.     <td bgcolor="#FFFFFF">QQ:</td> 
  175.     <td bgcolor="#FFFFFF"><label> 
  176.       <input type="text" name="qq" id="qq"/> 
  177.     </label></td> 
  178.   </tr> 
  179.   <tr> 
  180.     <td colspan="2" bgcolor="#FFFFFF">留言内容:</td> 
  181.   </tr> 
  182.   <tr> 
  183.     <td colspan="2" bgcolor="#FFFFFF"><label> 
  184.       <textarea name="post" id="post" cols="40" rows="5"></textarea> 
  185.     </label></td> 
  186.   </tr> 
  187.   <tr> 
  188.     <td colspan="2" bgcolor="#FFFFFF"><label> 
  189.       <input type="submit" name="Submit" value="提交" onclick="return checkInput();"/> 
  190.       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  191.       <input type="reset" name="Submit2" value="重置" /> 
  192.     </label><a href="login.php">管理员登录</a></td> 
  193.   </tr> 
  194. </table></form> 
  195. </body> 
  196. </html> 

 

post.php文件
  1. <?php 
  2. header('content-type:text/html;charset=utf-8'); 
  3. //如果PHP设置的自动转义函数未开启,就转义这些值 
  4. if(!get_magic_quotes_gpc()){ 
  5.  foreach ($_POST as &$items){ 
  6.   $items = addslashes($items); 
  7.  } 
  8. } 
  9. $name = $_POST['name']; 
  10. $qq = $_POST['qq']; 
  11. $email = $_POST['email']; 
  12. $post = $_POST['post']; 
  13. if($name==""||strlen($name)>10){ 
  14.  echo <<<tem 
  15.  <script language="javascript"> 
  16.  alert('请输入正确的有户名'); 
  17.  history.go(-1); 
  18.  </script> 
  19. tem; 
  20. exit(); 
  21. } 
  22. if($qq==""&&$email==""){ 
  23.  echo <<<tem 
  24.  <script> 
  25.  alert('Email和QQ必须输入一个!'); 
  26.  history.go(-1); 
  27.  </script> 
  28. tem; 
  29. exit(); 
  30. } 
  31. if($qq!=""&&(!is_numeric($qq)||$qq>9999999999||$qq<=9999)){ 
  32.  echo <<<tem 
  33.  <script> 
  34.  alert("请输入正确的QQ号码"); 
  35.  history.go(-1); 
  36.  </script> 
  37. tem; 
  38. exit(); 
  39. } 
  40. if($email!=""&&(!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$email)||strlen($email)>60)){ 
  41.  echo <<<tem 
  42.  <script> 
  43.  alert("请输入正确的Email"); 
  44.  history.go(-1); 
  45.  </script> 
  46. tem; 
  47. exit(); 
  48. } 
  49. if(strlen($post)>400){ 
  50.  echo <<<tem 
  51.  <script> 
  52.  alert("输入的留言内容太长!"); 
  53.  history.go(-1); 
  54.  </script> 
  55. tem; 
  56. exit(); 
  57. } 
  58. //链接数据库 
  59. $con=mysql_connect('localhost','root','root') or die('链接数据库失败!'); 
  60. mysql_query('set names utf8'); 
  61. mysql_select_db('GuestBook'); 
  62. //把客户信息插入guest表 
  63. $insertSql="insert into guest (name,qq,email) values ('$name','$qq','$email')"; 
  64. if(mysql_query($insertSql)){ 
  65.  $guestid = mysql_insert_id(); 
  66. } 
  67. else{ 
  68.  echo $insertSql; 
  69.  echo mysql_error(); 
  70.  echo "数据插入失败!"; 
  71.  exit(); 
  72. } 
  73. //把以上插入取得的客户id和留言信息插入到post表中 
  74. $post_time = time(); 
  75. $insertPostSql = "insert into post(guest_id,post,post_time) values('$guestid','$post','$post_time')"; 
  76. if(mysql_query($insertPostSql)){ 
  77.  echo <<<tem 
  78.  <script> 
  79.  alert("留言成功"); 
  80.  location.href="index.php"; 
  81.  </script> 
  82. tem; 
  83. } 
  84. else{ 
  85.  echo <<<tem 
  86.  <script> 
  87.  alert("留言失败"); 
  88.  location.href="index.php"; 
  89.  </script> 
  90. tem; 
  91. } 
  92. ?> 

下面为后台管理管理的页面login.php

  1. <?php 
  2. session_start(); 
  3. if(isset($_POST['Submit'])){ 
  4.  if(!get_magic_quotes_gpc()){ 
  5.   foreach ($_POST as &$items){ 
  6.    $items = addslashes($items); 
  7.   } 
  8.  } 
  9.  if($_POST['username']=='phpiask'&&md5($_POST['password'])=='6dc88b87062a5de19895e952fa290dad'){ 
  10.   $_SESSION['login']=true; 
  11.   echo "<script>alert('管理员登录成功');location.href='index.php';</script>"; 
  12.   exit(); 
  13.  } 
  14.  else { 
  15.   echo "<script>alert('登录失败!');</script>"; 
  16.  } 
  17. } 
  18. ?> 
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  20. <html xmlns="http://www.w3.org/1999/xhtml"> 
  21. <head> 
  22. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  23. <title>无标题文档</title> 
  24. </head> 
  25. <body> 
  26. <table> 
  27. <tr> 
  28. <td> 
  29. <form action="login.php" method="POST" name="form1"> 
  30. 用户名:<input type="text" name="username" size="20"/> 
  31. 密码:<input type="password" name="password" size="20"> 
  32. <input type="submit" value="登录" name="Submit"/> 
  33. <input type="button" onclick="javascript:location.href='index.php'" value="放弃"/> 
  34. </form> 
  35. </td> 
  36. </tr> 
  37. </table> 
  38. </body> 
  39. </html> 

删除留言的delete.php

  1. <?php 
  2. session_start(); 
  3. header('content-type:text/html;charset=utf-8'); 
  4. $con=mysql_connect('localhost','root','root') or die('链接数据库失败!'); 
  5. mysql_query('set names utf8'); 
  6. mysql_select_db('GuestBook'); 
  7. if(!$_SESSION['login']){ 
  8.  echo "<script>alert('权限不足!');location.href='index.php';</script>"; 
  9.  exit(); 
  10. } 
  11. if(isset($_GET['id'])&&$_GET['id']!=""){ 
  12.  $delRevertSql="delete from revert where post_id=".$_GET['id']; 
  13.  mysql_query($delRevertSql); 
  14.   
  15.  $delGuestSql="delete from guest where id = (select guest_id from post where id=".$_GET['id'].")"; 
  16.  mysql_query($delGuestSql); 
  17.   
  18.  $delPostSql="delete from post where id=".$_GET['id']; 
  19.  mysql_query($delPostSql); 
  20.   
  21.  if(mysql_error()==""){ 
  22.   echo "<script>alert('删除成功!');location.href='index.php';</script>"; 
  23.  } 
  24. } 
  25. ?> 

回复留言的revert.php文件

  1. <?php 
  2. session_start(); 
  3. $con=mysql_connect('localhost','root','root') or die('链接数据库失败!'); 
  4. mysql_query('set names utf8'); 
  5. mysql_select_db('GuestBook'); 
  6. if(!$_SESSION['login']){ 
  7.  echo "<script>alert('没有登录不能回复!');location.href='index.php';</script>"; 
  8.  exit(); 
  9. } 
  10. if($_POST['Submit']){ 
  11.  if(!get_magic_quotes_gpc()){ 
  12.   foreach ($_POST as $items){ 
  13.    $items = addslashes($items); 
  14.   } 
  15.  } 
  16.  if(strlen($_POST['revert'])>400){ 
  17.   echo "<script>alert('回复内容过长!');history.go(-1);</script>"; 
  18.   exit(); 
  19.  } 
  20.  $post_id = $_POST['post_id']; 
  21.  $revert = $_POST['revert']; 
  22.  $insertRevertSql = "insert into revert (post_id,revert,revert_time) value('$post_id','$revert','$time')"; 
  23.  if(mysql_query($insertRevertSql)){ 
  24.   echo "<script>alert('回复成功');location.href='index.php';</script>"; 
  25.   exit(); 
  26.  } 
  27.  else { 
  28.   echo "<script>alert('回复失败!');history.go(-1);</script>"; 
  29.  } 
  30. } 
  31. ?> 
  32. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  33. <html xmlns="http://www.w3.org/1999/xhtml"> 
  34. <head> 
  35. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  36. <title>无标题文档</title> 
  37. </head> 
  38. <body> 
  39. <table> 
  40. <tr> 
  41. <td> 
  42. <form action="revert.php" method="POST" name="form1"> 
  43. 回复内容:<textarea name="revert" cols="30" rows="5" id="revert"></textarea> 
  44. <input type="hidden" name="post_id" value="<?php echo $_GET['id']?> "size="20"> 
  45. <input type="submit" value="回 复" name="Submit"/> 
  46. <input type="button" onclick="javascript:history.go(-1);" value="放弃"/> 
  47. </form> 
  48. </td> 
  49. </tr> 
  50. </table> 
  51. </body> 
  52. </html>

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论