PHP 附录:留言板源代码

PHP 留言板源代码

提示

下面是本教程 PHP 留言板源代码,是为演示 PHP 留言板系统制作过程的。若您需要经过 CSS 修饰并能直接使用的源代码,请 点此下载 留言板源代码压缩包,使用其中的完善版。

conn.php

<?php
/*****************************
*数据库连接
*****************************/
$conn = @mysql_connect("localhost","root","root123");
if (!$conn){
    die("连接数据库失败:" . mysql_error());
}
mysql_select_db("test", $conn);
//字符转换,读库
mysql_query("set character set 'gbk'");
//写库
mysql_query("set names 'gbk'");
?>

config.php

<?php
$pagesize = 3;		//每页显示的留言数
$gb_password = 123456;	// 留言本管理密码,在不做数据库验证时使用
?>

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>请您留言</title>
<script language="JavaScript">
function InputCheck(form1)
{
  if (form1.nickname.value == "")
  {
    alert("请输入您的昵称。");
    form1.nickname.focus();
    return (false);
  }
  if (form1.content.value == "")
  {
    alert("留言内容不可为空。");
    form1.content.focus();
    return (false);
  }
}
</script>
</head>
<body>
<h3>留言列表</h3>
<?php
/*****************************
*index.php 主页面文件
*****************************/
// 引用相关文件
require("./conn.php");
require("./config.php");

// 确定当前页数 $p 参数
$p = $_GET['p']?$_GET['p']:1;
// 数据指针
$offset = ($p-1)*$pagesize;

$query_sql = "SELECT * FROM guestbook ORDER BY id DESC LIMIT  $offset , $pagesize";
$result = mysql_query($query_sql);
// 如果出现错误并退出
if(!$result) exit('查询数据错误:'.mysql_error());

// 循环输出
while($gb_array = mysql_fetch_array($result)){
    $content = nl2br($gb_array['content']);
    echo $gb_array['nickname'],'&nbsp;';
    echo '发表于:'.date("Y-m-d H:i", $gb_array['createtime']).'<br />';
    echo '内容:',nl2br($gb_array['content']),'<br /><br />';
    
    if(!empty($gb_array['replytime'])) {
        echo '----------------------------<br />';
        echo '管理员回复于:',date("Y-m-d H:i", $gb_array['replytime']),'<br />';
        echo nl2br($gb_array['reply']),'<br /><br />';
    }
    echo '<hr />';
}

//计算留言页数
$count_result = mysql_query("SELECT count(*) FROM guestbook");
$count_array = mysql_fetch_array($count_result);
$pagenum=ceil($count_array['count(*)']/$pagesize);
echo '共 ',$count_array['count(*)'],' 条留言';
if ($pagenum > 1) {
    for($i=1;$i<=$pagenum;$i++) {
        if($i==$p) {
            echo '&nbsp;[',$i,']';
        } else {
            echo '&nbsp;<a href="index.php?p=',$i,'">'.$i.'</a>';
        }
    }
}
?>
<div class="form">
<form id="form1" name="form1" method="post" action="submiting.php" onSubmit="return InputCheck(this)">
<h3>发表留言</h3>
<p>
<label for="title">昵&nbsp;&nbsp;&nbsp;&nbsp;称:</label>
<input id="nickname" name="nickname" type="text" /><span>(必须填写,不超过16个字符串)</span>
</p>
<p>
<label for="title">电子邮件:</label>
<input id="email" name="email" type="text" /><span>(非必须,不超过60个字符串)</span>
</p>
<p>
<label for="title">留言内容:</label>
<textarea id="content" name="content" cols="50" rows="8"></textarea>
</p>
<input type="submit" name="submit" value="  确 定  " />
</form>
</div>
</body>
</html>

submiting.php

<?php
// 禁止非 POST 方式访问
if(!isset($_POST['submit'])){
    exit('非法访问!');
}
// 表单信息处理
if(get_magic_quotes_gpc()){
    $nickname = htmlspecialchars(trim($_POST['nickname']));
    $email = htmlspecialchars(trim($_POST['email']));
    $content = htmlspecialchars(trim($_POST['content']));
} else {
    $nickname = addslashes(htmlspecialchars(trim($_POST['nickname'])));
    $email = addslashes(htmlspecialchars(trim($_POST['email'])));
    $content = addslashes(htmlspecialchars(trim($_POST['content'])));
}
if(strlen($nickname)>16){
    exit('错误:昵称不得超过16个字符串 [ <a href="javascript:history.back()">返 回</a> ]');
}
if(strlen($nickname)>60){
    exit('错误:邮箱不得超过60个字符串 [ <a href="javascript:history.back()">返 回</a> ]');
}

// 数据写入库表
require("./conn.php");
$createtime = time();
$insert_sql = "INSERT INTO guestbook(nickname,email,content,createtime)VALUES";
$insert_sql .= "('$nickname','$email','$content',$createtime)";

if(mysql_query($insert_sql)){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Refresh" content="2;url=index.php">
<title>留言成功</title>
</head>
<body>
<div class="refresh">
<p>留言成功!非常感谢您的留言。<br />请稍后,页面正在返回...</p>
</div>
</body>
</html>
<?php
} else {
    echo '留言失败:',mysql_error(),'[ <a href="javascript:history.back()">返 回</a> ]';
}
?>

login.php

<?php
/*****************************
*login.php 登录验证处理文件
*****************************/
session_start();
if($_POST){
    require("./conn.php");
    $username = $_POST['username'];
    $password = MD5(trim($_POST['password']));
    $check_result = mysql_query("SELECT uid FROM user WHERE username = '$username' AND 
password = '$password'");
    if(mysql_fetch_array($check_result)){
        session_register("username");
        header("Location: http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\'
)."/admin.php");
        exit;
    } else {
        echo '密码错误!';
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>留言管理登录</title>
<script language="JavaScript">
<!--

function InputCheck(form1)
{
  if (form1.password.value == "")
  {
    alert("请输入密码。");
    form1.password.focus();
    return (false);
  }
}
//--!>
</script>
</head>
<body>
<h3>请输入管理密码</h3>
<div class="form">
<form id="form1" name="form1" method="post" action="login.php" onSubmit="return InputCheck(this)">
<p>
<input type="hidden" name="username" value="admin" />
<label for="password">密 码:</label>
<input id="password" name="password" type="password" />
</p>
<input type="submit" name="submit" value=" 确 定 " />
</form>
</div>

admin.php

<?php
/*****************************
* admin.php 后台管理主页面文件
*****************************/
session_start();
// 未登录则重定向到登陆页面
if(!isset($_SESSION['username'])){
    header("Location: http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\'
)."/login.php");
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="style/style.css" />
<title>留言管理</title>
</head>
<body>
<?php
require("./conn.php");
require("./config.php");

$p = $_GET['p']?$_GET['p']:1;
$offset = ($p-1)*$pagesize;
$query_sql = "SELECT * FROM guestbook ORDER BY id DESC LIMIT  $offset , $pagesize";
$result = mysql_query($query_sql);
if(!$result) exit('查询数据错误:'.mysql_error());

// 循环输出当前页显示数据
while($gb_array = mysql_fetch_array($result)){
    echo $gb_array['nickname'],'&nbsp;';
    echo '发表于:',date("Y-m-d H:i:s", $gb_array['createtime']);
    echo ' ID号:',$gb_array['id'],'<br />';
    echo '内容:',nl2br($gb_array['content']),'<br />';
    
?>
<div id="reply">
<form id="form1" name="form1" method="post" action="reply.php">
<p><label for="reply">回复本条留言:</label></p>
<textarea id="reply" name="reply" cols="40" rows="5"><?=$gb_array['reply']?></textarea>
<p>
<input name="id" type="hidden" value="<?=$gb_array['id']?>" />
<input type="submit" name="submit" value="回复留言" />
<a href="reply.php?action=delete&id=<?=$gb_array['id']?>">删除留言</a>
</p>
</form>
</div>
<?
    echo "<hr />";
}
//计算留言页数
$count_result = mysql_query("SELECT count(*) FROM guestbook");
$count_array = mysql_fetch_array($count_result);
$pagenum=ceil($count_array['count(*)']/$pagesize);
echo '共 ',$count_array['count(*)'],' 条留言';
if ($pagenum > 1) {
    for($i=1;$i<=$pagenum;$i++) {
        if($i==$p) {
            echo '&nbsp;[',$i,']';
        } else {
            echo '&nbsp;<a href="admin.php?p=',$i,'">'.$i.'</a>';
        }
    }
}
?>
</body>
</html>

reply.php

<?php
session_start();
if(!isset($_SESSION['username'])){
    header("Location: http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\'
)."/login.php");
    exit;
}
require("./conn.php");
if($_POST){
    if(get_magic_quotes_gpc()){
        $reply = htmlspecialchars(trim($_POST['reply']));
    } else {
        $reply = addslashes(htmlspecialchars(trim($_POST['reply'])));
    }
    // 回复为空时,将回复时间置为空
    $replytime = $reply?time():'NULL';
    $update_sql = "UPDATE guestbook SET reply = '$reply',replytime = $replytime WHERE id = $_POST[id]";
    if(mysql_query($update_sql)){
        exit('<script language="javascript">alert("回复成功!");self.location="admin.php";</script>');
    } else {
        exit('留言失败:'.mysql_error().'[ <a href="javascript:history.back()">返 回</a> ]');
    }
}

// 删除留言
if($_GET['action'] == 'delete'){
    $delete_sql = "DELETE FROM guestbook WHERE id = $_GET[id]";
    if(mysql_query($delete_sql)){
        exit('<script language="javascript">alert("删除成功!");self.location = "admin.php";</script>');
    } else {
        exit('留言失败:'.mysql_error().'[ <a href="javascript:history.back()">返 回</a> ]');
    }
}
?>

本章节内容共分 8 部分:

  1. 1. PHP 留言板制作详细教程(附源代码下载)
  2. 2. PHP 留言板教程数据库表设计
  3. 3. PHP 留言板留言信息读取展示
  4. 4. PHP 留言板留言表单及留言处理
  5. 5. PHP 留言板后台管理登陆
  6. 6. PHP 留言板系统后台管理
  7. 7. PHP 留言板后台管理回复及删除留言处理
  8. 8. PHP 附录:留言板源代码

评论于2016-12-18日起不再显示。

5idev.com(我爱开发网) — 提供最好的 XHTML教程DIV+CSS教程JavaScript教程PHP教程ThinkPHP教程