PHP留言板

来源:互联网 发布:南京java培训 知乎 编辑:程序博客网 时间:2024/05/18 09:02

简单的PHP留言板制作。

一、功能:用户查看留言、发表留言,管理员登录、回复、删除留言、编辑回复、删除回复、分页

二、PHP文件制作:
1、conn.php 数据库连接文件

2、reportMessage 发表留言页面

2、messageBoard.PHP   留言板主页面

3、login.php 管理员登录页面

三、数据库messageboard

1、留言表message

字段:uid(主键id),username(用户名),title(留言标题),content(留言内容),reply(管理员回复),messageTime(留言时间)

2、管理员表admin

字段:aid(主键id),username(用户名),password(密码)

四、其他注意事项

1、留言内容过滤HTML字符,转换回车和空格

2、防刷新重复提交

五、创建数据库

drop database if exists messageboard;create database messageboard character set utf8 collate utf8_general_ci;use messageboard;DROP TABLE IF EXISTS `message`;CREATE TABLE `message` (  `mid` int(11) NOT NULL  auto_increment,  `username` char(20) NOT NULL,`title` VARCHAR(50) NOT NULL,  `content` text NOT NULL,  `reply` text,  `messagetime` int(11) NOT NULL,  PRIMARY KEY  (`uid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `admin`;CREATE TABLE `admin` (  `aid` int(11) NOT NULL  auto_increment,  `username` char(20) NOT NULL,  `password` char(50) NOT NULL,  PRIMARY KEY  (`aid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;-- 预设管理员,密码为admininsert into admin values(null,'admin','21232f297a57a5a743894a0e4a801fc3 ');


六、页面代码

1、样式表文件 main.css

@CHARSET "UTF-8";table.mTable{width:600px;margin:0 auto;text-align:left;    font-family: verdana,arial,sans-serif;    font-size:12px;    color:#333333;    border-width: 1px;    border-color: #666666;    border-collapse: collapse;}table.mTable th {    border-width: 1px;    padding: 8px;    border-style: solid;    border-color: #666666;    background-color: #dedede;}table.mTable td {    border-width: 1px;    padding: 8px;    border-style: solid;    border-color: #666666;    background-color: #ffffff;}table.lTable{width:300px;}.btnTd{text-align:center;}.mainLeftTd{width:120px;}.manageMsg{float:right;}.manageMsg a{text-decoration:none;}replyTd{color:009900;background-color:#0FF;}


2、数据库连接文件 conn.php


<?php
/*
 * Created on 2014-11-1
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
//连接数据库
$conn = @ mysql_connect("localhost:3306", 'root', 'root') or die("数据库服务器连接失败!");
mysql_select_db("messageboard") or die("数据库连接失败");
mysql_set_charset("utf8"); //设置当前连接的默认字符集。
?>

3、发表留言文件 reportMessage.php

<?php/* * Created on 2014-11-1 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */require_once("conn.php");if($_POST and array_key_exists("username",$_POST)){$sql = "insert into message (username,title,content,messagetime) values('$_POST[username]','$_POST[title]','$_POST[content]',".time().")";$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");echo "<script>alert('恭喜你,留言成功!');window.location.href='messageBoard.php';</script>";}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>发表留言</title><link rel="stylesheet" href="main.css" type="text/css" /><script type="text/javascript">function check(){if(document.mForm.username.value == ""){alert("用户名不能为空!");return false;}else if(document.mForm.title.value == ""){alert("留言标题不能为空!");return false;}else if(document.mForm.content.value == ""){alert("留言内容不能为空!");return false;}}</script></head><body><table class="mTable"><caption>发表留言</caption><form name="mForm" action="" method="post" onSubmit="return check();"><tr><td>用户名:</td><td><input type="text" name="username" value="" /></td></tr><tr><td>留言标题:</td><td><input type="text" name="title" value="" size="50" /></td></tr><tr><td>留言内容:</td><td><textarea name="content" cols="50" rows="8"></textarea></td></tr><tr><td colspan="2" class="btnTd"><input type="submit" name="mBtn" value="发 表" /> <input type="button" name="returnBtn" value="返 回" onclick="javascript:window.location.href='messageBoard.php';" /></td></tr></form></table></body></html>

4、留言板主页面 messageBoard.php

<?php/* * Created on 2014-11-1 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates *///一、功能:用户查看留言、发表留言,管理员登录、回复、删除留言、编辑自己回复的内容require_once("conn.php");//总记录数$totalRecord = count(getData("select * from message"));//当前页if (isset($_GET["page"]) and (int) $_GET["page"] >= 1) {$currentPage = $_GET["page"] ? (int) $_GET["page"] : 1;} else {$currentPage = 1;}//每页记录数$pageSize = 3;//总页数$totalPage = ceil($totalRecord / $pageSize);//组装获取当前页记录的SQL语句$sql = "select * from message limit " . ($currentPage -1) * $pageSize . "," . $pageSize;//存储当前页记录的数组$rows = getData($sql);//当前页的路径=相对根目录路径+文件名$url = $_SERVER['PHP_SELF'];//判断管理员是否已登录$isLogin = false;if(isset($_COOKIE["username"])) $isLogin = true;if($isLogin){//回复留言if(isset($_POST["reply"])){$sql = "update message set reply='".$_POST["reply"]."' where mid=".(int)$_POST["mid"];$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");echo "<script>alert('回复成功!');window.location.href='messageBoard.php';</script>";}//删除留言if(isset($_GET["act"]) and ($_GET["act"] == "del")){$sql = "delete from message where mid=".(int)$_GET["mid"];$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");echo "<script>alert('删除成功!');window.location.href='messageBoard.php';</script>";}//删除回复if(isset($_GET["act"]) and ($_GET["act"] == "delReply")){$sql = "update message set reply='' where mid=".(int)$_GET["mid"];$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");echo "<script>alert('删除回复成功!');window.location.href='messageBoard.php';</script>";}}//获取数据库中的数据function getData($sql) {$result = @ mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");$rows = array ();while ($row = mysql_fetch_array($result)) {$rows[] = $row;}return $rows;}//过滤HTML标记,转换空格和回车function changeStr($str){$str = htmlspecialchars($str);$str = str_replace("\n","<br />",str_replace(" "," ",$str));return $str;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>PHP留言板</title><link rel="stylesheet" href="main.css" type="text/css" /><script type="text/javascript">function check(o){if(o.reply.value == ""){alert("回复内容不能为空!");return false;}}function showAddReply(addReplyTrId){var allAddReplyTrArr = getClass("tr","addReplyTr");for(var i=0;i<allAddReplyTrArr.length;i++){allAddReplyTrArr[i].style.display = "none";}document.getElementById(addReplyTrId).style.display = "table-row";var allEditReplyTrArr = getClass("tr","editReplyTr");//所有的编辑回复窗口要隐藏for(var i=0;i<allEditReplyTrArr.length;i++){allEditReplyTrArr[i].style.display = "none";}var allReplyTrArr = getClass("tr","replyTr");//所有有内容的回复窗口要显示for(var i=0;i<allReplyTrArr.length;i++){allReplyTrArr[i].style.display = "table-row";}}function showEditReply(editReplyTrId,replyTrId){var allEditReplyTrArr = getClass("tr","editReplyTr");for(var i=0;i<allEditReplyTrArr.length;i++){allEditReplyTrArr[i].style.display = "none";}document.getElementById(editReplyTrId).style.display = "table-row";var allReplyTrArr = getClass("tr","replyTr");for(var i=0;i<allReplyTrArr.length;i++){allReplyTrArr[i].style.display = "table-row";}document.getElementById(replyTrId).style.display = "none";var allAddReplyTrArr = getClass("tr","addReplyTr");//所有的回复窗口也要隐藏for(var i=0;i<allAddReplyTrArr.length;i++){allAddReplyTrArr[i].style.display = "none";}}//这个方法是用来替代document.getElementsByClassName,因为该死的IE不支持document.getElementsByClassName,所以写了这样一个方法来替代。function getClass(tagname,className) { //tagname指元素,className指class的值if (document.getElementsByClassName) {      //判断浏览器是否支持getElementsByClassName,如果支持就直接的用return document.getElementsByClassName(className);}else{//当浏览器不支持getElementsByClassName的时候用下面的方法var tagname = document.getElementsByTagName(tagname);  //获取指定元素var tagnameAll = [];     //这个数组用于存储所有符合条件的元素for (var i = 0; i < tagname.length; i++) {    //遍历获得的元素if (tagname[i].className == className) {     //如果获得的元素中的class的值等于指定的类名,就赋值给tagnameAlltagnameAll[tagnameAll.length] = tagname[i];}}return tagnameAll;}}//删除留言时的判断function confirmDel(mid,url,act){if(confirm("确定要删除吗?")){window.location.href = url+"?mid="+mid+"&act="+act;}}</script></head><body><table class="mTable"><tr><td colspan="2"><a href="reportMessage.php">我要留言</a> <?if(!$isLogin):?><a href="login.php">管理留言</a><?endif;?></td></tr><? foreach($rows as $row):?><tr><td class="mainLeftTd">用户名:<?=$row["username"]?></td><td>留言标题:<?=$row["title"]?><?if($isLogin):?><span class="manageMsg"><?if($row["reply"]==""):?><a href="#<?="replyTr".$row["mid"]?>" onclick="showAddReply('<?="addReplyTr".$row["mid"]?>');">回复</a><?endif;?>  <a href="#" onclick="confirmDel(<?=$row["mid"]?>,'<?=$url?>','del');">删除</a></span><?endif;?></td></tr><tr><td>留言内容:</td><td><?=changeStr($row["content"])?></td></tr><?if($row["reply"]!=""):?><tr class="replyTr" id="replyTr<?=$row["mid"]?>"><td>管理员回复:</td><td>RE  <span class="replyContent"><?=$row["username"]?>:<br /><?=changeStr($row["reply"])?></span><?if($isLogin):?><span class="manageMsg"><a href="#<?="editReplyTr".$row["mid"]?>" onclick="showEditReply('<?="editReplyTr".$row["mid"]?>','<?="replyTr".$row["mid"]?>');">编辑</a>  <a href="#" onclick="confirmDel(<?=$row["mid"]?>,'<?=$url?>','delReply');">删除</a></span><?endif;?></td></tr><tr class="editReplyTr" id="editReplyTr<?=$row["mid"]?>" style="display:none;"><form name="editForm<?=$row["mid"]?>" action="" method="post" onSubmit="return check(this);"><td>管理员编辑回复:</td><td><textarea name="reply" cols="40" rows="7"><?=changeStr($row["reply"])?></textarea> <input type="hidden" name="mid" value="<?=$row["mid"]?>" /><input type="submit" name="replyBtn" value="确认编辑" /></td></form></tr><?else:?><tr class="addReplyTr" id="addReplyTr<?=$row["mid"]?>" style="display:none;"><form name="addForm<?=$row["mid"]?>" action="" method="post" onSubmit="return check(this);"><td>管理员回复:</td><td><textarea name="reply" cols="40" rows="7"></textarea> <input type="hidden" name="mid" value="<?=$row["mid"]?>" /><input type="submit" name="replyBtn" value="确认回复" /></td></form></tr><?endif;?><tr class="separatorTr"><td colspan="2"></td></tr><? endforeach;?><!-- 分页 --><?if($totalRecord>$pageSize):?><tr><td colspan="3"><? if ($currentPage == 1):?>首页 上一页 <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a><?elseif($currentPage == $totalPage): ?><a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> 下一页 尾页<?else:?><a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a><?endif;?></td></tr><? endif; ?></table></body></html>

5、管理员登录页面 login.php

<?php/* * Created on 2014-11-1 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ require_once("conn.php");if(isset($_COOKIE["username"])){echo "<script>window.location.href='messageBoard.php';</script>";}else{if($_POST and array_key_exists("username",$_POST)){$sql = "select * from admin where username='".$_POST["username"]."' and password='".md5($_POST["password"])."'";$result = mysql_query($sql);if(mysql_num_rows($result) != 0){setCookie("username",$_POST["username"]);}else{echo "<script>alert('用户名或密码错误!请重新输入。');window.location.href='login.php';</script>";}}}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>管理员登录</title><link rel="stylesheet" href="main.css" type="text/css" /><script type="text/javascript">function check(){if(document.mForm.username.value == ""){alert("用户名不能为空!");return false;}else if(document.mForm.password.value == ""){alert("密码不能为空!");return false;}}</script></head><body><table class="mTable lTable"><caption>管理员登录</caption><form name="mForm" action="" method="post" onSubmit="return check();"><tr><td>用户名:</td><td><input type="text" name="username" value="" /></td></tr><tr><td>密码:</td><td><input type="password" name="password" value="" /></td></tr><tr><td colspan="2" class="btnTd"><input type="submit" name="mBtn" value="登 录" /> <input type="button" name="returnBtn" value="返 回" onclick="javascript:window.location.href='messageBoard.php';" /></td></tr></form></table></body></html>

完工!^_^


1 0
原创粉丝点击