程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php簡易留言板程序代碼

php簡易留言板程序代碼

編輯:關於PHP編程

這是一個最基礎的留言板程序了,但是己經有了留言板程序基本功能,很適合於php初學者用用,學習用啊,當然也可以用於企業網站也是很不錯的哦。

 

 代碼如下 復制代碼

<?php
session_start();
$con=mysql_connect('localhost','root','root') or die('鏈接數據庫失敗!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

$pagesize = 10;//每一頁顯示多少留言記錄
if(isset($_GET['page'])&&$_GET['page']!='') $page=$_GET['page'];
else $page=0;

$sql = "SELECT a . * , b.name, b.email, b.qq, c.revert_time, c.revert
  FROM post a
  LEFT JOIN revert c ON ( a.id = c.post_id ) , guest b
  WHERE a.guest_id = b.id
  ORDER BY a.id DESC";
$numRecord = mysql_num_rows(mysql_query($sql));
$totalpage = ceil($numRecord/$pagesize);

$recordSql = $sql. " LIMIT ".$page*$pagesize.",".$pagesize;
$result = mysql_query($recordSql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPiask簡易留言板</title>
<style type="text/css">
<!--
body {
 margin-left: 0px;
 margin-top: 0px;
}
a:link {
 text-decoration: none;
 color: #FF6600;
}
a:visited {
 text-decoration: none;
}
a:hover {
 text-decoration: underline;
}
a:active {
 text-decoration: none;
}
.STYLE1 {
 color: #FFFFFF;
 font-weight: bold;
 font-size: 16px;
}
td{
font-size:12px;
}
.tdhx {
 font-style: italic;
 line-height: 1.5;
 text-decoration: underline;
}
-->
</style>
<script language="javascript">
function checkInput(){
 var Email = document.getElementById('email');
 var QQ = document.getElementById('qq');
 var name = document.getElementById('name');
 var post = document.getElementById('post');
 //驗證用戶名:不能超過10個字符(5個漢字),不能輸入非法字符,不能為空
 nameValue = name.value.replace(/s+/g,"");
 var SPECIAL_STR = "~!%^&*();"?><[]{}\|,:/=+—";
 var nameflag=true;
 for(i=0;i<nameValue.lenght;i++){
  if (SPECIAL_STR.indexOf(nameValue.charAt(i)) !=-1)
  nameflag=false;
 }
 if(nameValue==''){
  alert('請填寫用戶名稱!'); 
  return false;
 }
 if(nameValue.length>10){
  alert('用戶名稱最多10個字符(5個漢字)!');
  return false;
 }
 
 if(nameflag===false){
  alert('用戶名稱不能包含非法字符請更改!');
  return false;
 }
 //驗證QQ號碼
 var par =/^[1-9]d{4,12}$/;
 if(QQ.value!=''&&!par.test(QQ.value)){
  alert('請輸入正確的QQ號碼');
  return false;
 }
 //驗證Email地址
 var emailpar = /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/;
 if(Email.value!=''&&!emailpar.test(Email.value)){
  alert('請輸入正確的郵箱地址!');
  return false;
 }
 if(QQ.value==''&&Email.value==''){
  alert('郵箱和QQ必選其一');
  return false;  
 }
 if(post.value==""){
  alert('請輸入留言內容!');
  return false;   
 }
 if(post.value.length>400){
  alert('留言內容太長!');
  return false;   
 }

}
</script>
</head>

<body>
<table width="800" border="0" align="center">

  <tr>
    <td height="80" bgcolor="#003366"><span class="STYLE1"> 簡易留言板教程(<a href="http://www.phpiask.com">PHP iask</a>)</span></td>
  </tr>
  <tr>
    <td height="5" bgcolor="#efefef"></td>
  </tr>
</table>
<table width="800" border="0" align="center" bgcolor="#fefefe">
<?php
while($rs=mysql_fetch_object($result)){
?>
  <tr>
    <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>
  </tr>
  <?php
  if(isset($_SESSION['login'])&&$_SESSION['login']){
  ?>
    <tr>
    <td class="tdhx"><a href="revert.php?id=<?php echo $rs->id?>">回復</a> | <a href="delete.php?id=<?php echo $rs->id?>">刪除</a></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td>留言內容:<?php echo nl2br(htmlspecialchars($rs->post))?><br/>
    <font color="Red">
    回復內容:<?php echo nl2br(htmlspecialchars($rs->revert))?>[<?php if($rs->revert_time!="") echo date("Y-m-d H:i:s",$rs->revert_time+8*3600)?> ]
    </font>
   
    </td>
  </tr>
  <tr><td height="3px"  bgcolor="##FF6600"></td></tr>
<?php
}
?>
</table>
<table width="800" border="0" align="center" bgcolor="#B1C3D9">
  <tr>
    <td >
<?php
if($page>0) echo "<a href='index.php?page=".($page-1)."'>上一頁|</a>" ;
if($page<$totalpage-1) echo "<a href='index.php?page=".($page+1)."'>下一頁</a>" ;
?></td>
  </tr>
</table><form action="post.php" method="post" id="postForm" name="postForm">
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#efefef">
 
  <tr>
    <td width="117" bgcolor="#FFFFFF">姓名:</td>
    <td width="673" bgcolor="#FFFFFF"><label>
      <input type="text" name="name" id="name" />
    </label></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">Email:</td>
    <td bgcolor="#FFFFFF"><label>
      <input type="text" name="email" id="email" />
    </label></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">QQ:</td>
    <td bgcolor="#FFFFFF"><label>
      <input type="text" name="qq" id="qq"/>
    </label></td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#FFFFFF">留言內容:</td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#FFFFFF"><label>
      <textarea name="post" id="post" cols="40" rows="5"></textarea>
    </label></td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#FFFFFF"><label>
      <input type="submit" name="Submit" value="提交" onclick="return checkInput();"/>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="reset" name="Submit2" value="重置" />
    </label><a href="login.php">管理員登錄</a></td>
  </tr>
</table></form>
</body>
</html>

post.php文件

<?php
header('content-type:text/html;charset=utf-8');
//如果PHP設置的自動轉義函數未開啟,就轉義這些值
if(!get_magic_quotes_gpc()){
 foreach ($_POST as &$items){
  $items = addslashes($items);
 }
}

$name = $_POST['name'];
$qq = $_POST['qq'];
$email = $_POST['email'];
$post = $_POST['post'];

if($name==""||strlen($name)>10){
 echo <<<tem
 <script language="javascript">
 alert('請輸入正確的有戶名');
 history.go(-1);
 </script>
tem;
exit();
}
if($qq==""&&$email==""){
 echo <<<tem
 <script>
 alert('Email和QQ必須輸入一個!');
 history.go(-1);
 </script>
tem;
exit();
}
if($qq!=""&&(!is_numeric($qq)||$qq>9999999999||$qq<=9999)){
 echo <<<tem
 <script>
 alert("請輸入正確的QQ號碼");
 history.go(-1);
 </script>
tem;
exit();
}
if($email!=""&&(!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$email)||strlen($email)>60)){
 echo <<<tem
 <script>
 alert("請輸入正確的Email");
 history.go(-1);
 </script>
tem;
exit();
}
if(strlen($post)>400){
 echo <<<tem
 <script>
 alert("輸入的留言內容太長!");
 history.go(-1);
 </script>
tem;
exit();
}

//鏈接數據庫
$con=mysql_connect('localhost','root','root') or die('鏈接數據庫失敗!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

//把客戶信息插入guest表
$insertSql="insert into guest (name,qq,email) values ('$name','$qq','$email')";
if(mysql_query($insertSql)){
 $guestid = mysql_insert_id();
}
else{
 echo $insertSql;
 echo mysql_error();
 echo "數據插入失敗!";
 exit();
}

//把以上插入取得的客戶id和留言信息插入到post表中
$post_time = time();
$insertPostSql = "insert into post(guest_id,post,post_time) values('$guestid','$post','$post_time')";
if(mysql_query($insertPostSql)){
 echo <<<tem
 <script>
 alert("留言成功");
 location.href="index.php";
 </script>
tem;
}
else{
 echo <<<tem
 <script>
 alert("留言失敗");
 location.href="index.php";
 </script>
tem;
}
?>

下面為後台管理管理的頁面 login.php登錄先

 

 代碼如下 復制代碼 <?php
session_start();
if(isset($_POST['Submit'])){
 if(!get_magic_quotes_gpc()){
  foreach ($_POST as &$items){
   $items = addslashes($items);
  }
 }
 if($_POST['username']=='phpiask'&&md5($_POST['password'])=='6dc88b87062a5de19895e952fa290dad'){
  $_SESSION['login']=true;
  echo "<script>alert('管理員登錄成功');location.href='index.php';</script>";
  exit();
 }
 else {
  echo "<script>alert('登錄失敗!');</script>";
 }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
<table>
<tr>
<td>
<form action="login.php" method="POST" name="form1">
用戶名:<input type="text" name="username" size="20"/>
密碼:<input type="password" name="password" size="20">
<input type="submit" value="登錄" name="Submit"/>
<input type="button" onclick="javascript:location.href='index.php'" value="放棄"/>
</form>
</td>
</tr>
</table>
</body>
</html>

刪除留言的delete.php

 代碼如下 復制代碼

<?php
session_start();
header('content-type:text/html;charset=utf-8');
$con=mysql_connect('localhost','root','root') or die('鏈接數據庫失敗!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

if(!$_SESSION['login']){
 echo "<script>alert('權限不足!');location.href='index.php';</script>";
 exit();
}

if(isset($_GET['id'])&&$_GET['id']!=""){
 $delRevertSql="delete from revert where post_id=".$_GET['id'];
 mysql_query($delRevertSql);
 
 $delGuestSql="delete from guest where id = (select guest_id from post where id=".$_GET['id'].")";
 mysql_query($delGuestSql);
 
 $delPostSql="delete from post where id=".$_GET['id'];
 mysql_query($delPostSql);
 
 if(mysql_error()==""){
  echo "<script>alert('刪除成功!');location.href='index.php';</script>";
 }
}
?>

回復留言的revert.php文件

 代碼如下 復制代碼

<?php
session_start();
$con=mysql_connect('localhost','root','root') or die('鏈接數據庫失敗!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

if(!$_SESSION['login']){
 echo "<script>alert('沒有登錄不能回復!');location.href='index.php';</script>";
 exit();
}
if($_POST['Submit']){
 if(!get_magic_quotes_gpc()){
  foreach ($_POST as $items){
   $items = addslashes($items);
  }
 }
 if(strlen($_POST['revert'])>400){
  echo "<script>alert('回復內容過長!');history.go(-1);</script>";
  exit();
 }
 $post_id = $_POST['post_id'];
 $revert = $_POST['revert'];
 $insertRevertSql = "insert into revert (post_id,revert,revert_time) value('$post_id','$revert','$time')";
 if(mysql_query($insertRevertSql)){
  echo "<script>alert('回復成功');location.href='index.php';</script>";
  exit();
 }
 else {
  echo "<script>alert('回復失敗!');history.go(-1);</script>";
 }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
<table>
<tr>
<td>
<form action="revert.php" method="POST" name="form1">
回復內容:<textarea name="revert" cols="30" rows="5" id="revert"></textarea>
<input type="hidden" name="post_id" value="<?php echo $_GET['id']?> "size="20">
<input type="submit" value="回 復" name="Submit"/>
<input type="button" onclick="javascript:history.go(-1);" value="放棄"/>
</form>
</td>
</tr>
</table>
</body>
</html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved