你的位置:| 首页网站建设PHP留言簿开发实例代码(附源文件)→文章正文  
文章导航


PHP留言簿开发实例代码(附源文件)


数据库:

比较有代表的php+mysql留言簿数据库结构:(库名:lyb)


表一: admin
字段: id(int11) name(varchvr) password(varchvr)

表二: lo
字段: id(int11) username(varchvr) sex(varchvr) qq(varchvr) email(varchvr) info(text) ip(varchvr) submit_time(datetime)

1.admin.php文件

<?php
require_once('conn.php');
?>
<style type="text/css">
<!--
.STYLE1 {
color: #FFFFFF;
font-size: 24px;
}
-->
</style>

<table width="400" border="0" align="center" bgcolor="#D7E7FF">
<form method="post" action="adminpost.php">
<tr>
<td colspan="2" align="center" bgcolor="#003366"><span class="STYLE1">管理员登录</span></td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="right"><a href="index.php">查看留言</a> <a href="post.php">写留言</a></td>
</tr>
<tr>
<td width="292" align="right"><img src="img/user.gif" width="20" height="20" align="absbottom" />姓名 :</td>
<td width="398"><input name="uname" type="text" class="y" id="uname" size="20" /></td>
</tr>
<tr>
<td align="right"><img src="img/password.gif" width="20" height="20" align="absbottom" />密码 :</td>
<td><input name="upass" type="password" class="y" id="upass" size="21" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="cen">
<input name="submit" type="submit" value="管理员登录" />
</span></td>
</tr>
</form>
</table>

2. adminexit.php文件

//adminexit.php(退出管理页)

<?php
session_start();
$_SESSION["key"] = 0;//使SESSION不为1,0为游客,1为管理员

header('location:index.php');
?>

3.adminpost.php文件

//adminpost.php(管理验证页)

<?php
session_start();
require_once('conn.php');

$name = $_POST['name'];
$password = $_POST['password'];

$sql = "select * from admin where name='".$name."'";
$result = mysql_query($sql);
$num = mysql_num_rows($result);

if (strrpos($name,"<")!==false || strrpos($name,">")!==false||strrpos($name,"@")!==false||strrpos($name,"\"")!==false||strrpos($name,"'")!==false||strrpos($name,"_")!==false)
{
echo "<script>alert('不能有特殊字符!');location.href='admin.php';</script>";
}

if($num)
{//如果用户存在,就检查密码是否正确
$rs = mysql_fetch_array($result);
if($rs[2]!=$password)
{
echo "<script>alert('密码不正确,请确认后输入!');location.href='admin.php';</script>";
}
else
{//用户名,密码都正确,注册SESSION变量,然后跳转到首页
$_SESSION["key"]=1;
echo "<script>alert('登录成功!');location.href='index.php';</script>";
}
}
else
{//如果没有这个用户
echo "<script>alert('没有这个用户,请确认后输入!');location.href='admin.php';</script>";
}
?>

4.conn.php文件

//conn.php(连接数据库文件)

<?php
mysql_connect("localhost","root","");//连接数据库
mysql_select_db("lyb");//选择数据库
?>

5.delete.php文件

//delete.php(删除留言页)

<?php
session_start();
require_once('conn.php');

$id=$_GET['id'];

if($_SESSION["key"]==1)
{
$sql = "delete from lo where id=".$id;
mysql_query($sql);
echo "<script>location.href='index.php'</script>";
}
else
{
header('location:index.php');
}
?>

6.footer.php

//footer.php(底部公共文件)


<?php
$counterFile="conter.xml";
function displayCounter($counterFile){
$fp = fopen($counterFile,"rw");
$num = fgets($fp,5);
$num += 1;
print "<div id=\"footer\">哈能留言簿 Version 1.0 您是第 "."$num"." 位用户</div>";
exec("rm -rf $counterFile");
exec("echo $num > $counterFile");
}
if(!file_exists($counterFile)){
exec("echo 0 > $counterFile");
}
displayCounter($counterFile);
?>

</body>
</html>

 

7.header.php文件

//header.php(头部公用文件)

<!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=gb2312" />
<title>哈能留言簿 Version 1.0</title>
</head>
<link href="css.css" rel="stylesheet" type="text/css" />
<body>
<div id="head">
<div id="head_l">
<ul>
<li><a href="index.php">查看留言</a></li>
<li><a href="post.php">写留言</a></li>
<?php
session_start();
if($_SESSION["key"]==1){//如果获取的SESSION为1则显示管理项
?>
<li><a href="adminexit.php">退出管理</a></li>
<?
}
else
{
?>
<li><a href="admin.php">管理</a></li>
<?}?>
</ul>
</div>
<div id="head_r">哈能留言簿 Version 1.0</div>
</div>

8.index.php文件

//index.php(首页)


<?php
require_once("conn.php");
require_once("header.php");
session_start();

//分页代码开始
$pagesize = 10;//设置每页显示条数
$rs = mysql_query("select count(*) from lo");//取得记录总数,计算总页数用
$myrow = mysql_fetch_array($rs);
$numrows = $myrow[0];//计算总记录

$pages = intval($numrows/$pagesize);
if($numrows%$pagesize)$pages++;//设置页数
if(isset($_GET['page']))
{
$page = intval($_GET['page']);
}
else
{
$page = 1;//设为第一页
}
$offset = $pagesize*($page-1);//计算记录偏移量
//分页代码结束


$sql = "select id,username,sex,qq,email,info,ip,DATE_FORMAT(submit_time, '%Y年%m月%d日 %T' ) from lo order by id desc limit $offset,$pagesize";//用到了DATE-FORMAT格式化日期格式
$result = mysql_query($sql);
$num = mysql_num_rows($result);

if($num>0){
while($row = mysql_fetch_array($result))
{
//echo print_r($row);
if($row[2]=="男")//这个使性别改成你想要的名称^_^
{
$sex = "帅哥";
}
else
{
$sex = "美女";
}
?>

<div id="show">
<p class="num">第 [<?= $row[0] ?>] 条留言<p>
<p class="unline">留言人:<span class="blue"><?= $row[1]?></span> 性别:<?= $sex ?> 留言时间:<?= $row[7] ?> <? if($row[3]) {?> <a href="http://wpa.qq.com/msgrd?V=1&Uin=<?= $row[3] ?>&Site=http://www.loai.cn&Menu=yes"><img src="img/qq.gif" alt="<?= $row[3]?>" /></a><? } ?><? if($row[4]){ ?> <a href="mailto:<?= $row[4] ?>"><img src="img/email.gif" alt="<?= $row[4]?>" /></a> <? }?><? if($_SESSION["key"]==1){ ?> IP:<?= $row[6] ?> <a href="update.php?wuleying&id=<?= $row[0]?>">更改</a> <a href="delete.php?wuleying&id=<?= $row[0]?>">删除</a><?}?></a></p>
<p class="blue">留言内容:</p>
<div id="show_info"><?= nl2br(htmlspecialchars($row[5])) ?></div>
</div>
<?php
}
}
else
{
echo "<div id=\"show\">无数据......</div>";
}
?>

<div id="show_page">
<p>
<?php
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
if($page==1&&$pages>1)
{
echo "首页 | ";
echo "上一页 | ";
echo "<a href=\"index.php?page=".$next."\">下一页</a> | ";
echo "<a href=\"index.php?page=".$last."\">尾页</a> | ";
}
elseif($page>=1&&$page!=$pages&&$num>0)
{
echo "<a href=\"index.php?page=".$first."\">首页</a> | ";
echo "<a href=\"index.php?page=".$prev."\">上一页</a> | ";
echo "<a href=\"index.php?page=".$next."\">下一页</a> | ";
echo "<a href=\"index.php?page=".$last."\">尾页</a> | ";
}
elseif($page==$pages&&$page!=1)
{
echo "<a href=\"index.php?page=".$first."\">首页</a> | ";
echo "<a href=\"index.php?page=".$prev."\">上一页</a> | ";
echo "下一页 | ";
echo "尾页 | ";
}
elseif($page==$pages)
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
else
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
?>
共 <span><?= $pages ?></span> 页 | 当前第 <span><?= $page ?></span> 页 | 共 <span><?=$numrows ?></span> 条留言</p>
</div>


<?php
mysql_close();
?>

<?php
require_once("footer.php");
?>

9.input.php文件

//input.php(插入留言)

<?php
require_once("conn.php");
$username = $_POST['name'];
$sex = $_POST['sex'];
$qq = $_POST['qq'];
$email = $_POST['email'];
$info = $_POST['info'];

if (strrpos($username,"<")!==false || strrpos($username,">")!==false||strrpos($username,"@")!==false||strrpos($username,"\"")!==false||strrpos($username,"'")!==false||strrpos($username,"_")!==false)
{
echo "<script>alert('名称不能有特殊字符!');location.href='post.php';</script>";
exit();
}

if (!ereg("^[0-9]{0,}$",$qq))//用正则检查QQ格式
{
echo "<script>alert('OICQ信息有错误!必须是数字!');location.href='post.php';</script>";
exit();
}

if($email)
{//如果填写了邮箱就用正则检查邮箱格式
if (!ereg("^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",$email))
{
echo "<script>alert('email格式不正确!');location.href='post.php';</script>";
exit();
}
}

if(!$username)
{
echo "<script>alert('名称不能为空哦!');location.href='post.php';</script>";
exit();
}
elseif(!$info)
{
echo "<script>alert('留言不能为空哦!');location.href='post.php';</script>";
exit();
}
else
{
$ip = getenv('REMOTE_ADDR');//获取客户端IP地址

$sql = "insert into lo (username,sex,qq,email,info,ip,submit_time) values ('$username','$sex','$qq','$email','$info','$ip',NOW())";

$result = mysql_query($sql);

mysql_close();

echo "<script>alert('提交成功!返回首页');location.href='index.php';</script>";
}
?>

10.post.php文件

//post.php(提交留言页面)


<?
require_once("header.php");
?>
<div id="input">
<form method="post" action="input.php" name="form1">
<h1>提交留言</h1>
<p>姓名 :<input type="text" name="name" size="20" class="y" /> </p>
<p>性别 :<input name="sex" type="radio" value="男" checked/> 帅哥 <input type="radio" name="sex" value="女"/> 美女</p>
<p>Q Q :<input type="text" name="qq" class="y" /> (可选填)</p>
<p>Email:<input type="text" name="email" class="y" /> (可选填)</p>
<p>留言内容:</p>
<p><textarea name="info" rows="5" cols="40"></textarea></p>
<p class="cen">
<input type="submit" value="提交留言" />
<input type="reset" value="重写">
</p>
<p class="cen1">哈能留言簿 Version 1.0</p>
</form>
</div>
</body>
</html>

 

11.update.php文件

//update.php(修改留言页)


<?php
session_start();
require_once('conn.php');
require_once('header.php');


if($_SESSION["key"]==1)
{
$id = $_GET['id'];
$sql = "select * from lo where id= ".$id;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

?>

<div id="update">
<form action="updatepost.php?wuleying&id=<?= $row[0] ?>" method="post">
<h1>修改留言</h1>
<p>姓名:<input type="text" value="<?= $row[1]?>" name="name" class="y"></input></p>
<p>留言:</p>
<p><textarea name="info" rows="5" cols="35"><?= $row[5] ?></textarea></p>
<p class="cen"><input type="submit" value="修改" /></p>
<p class="cen1">哈能留言簿 Version 1.0</p>
</form>

<div>


<?php
}
else
{
header('location:index.php');
}
?>

12.updatepost.php

//updatepost.php(修改提交页)

<?php
require_once('conn.php');

$username = $_POST['name'];
$info = $_POST['info'];
$id = $_GET['id'];
//echo $id;

$sql = "update lo set username= '".$username."',info='".$info."' where id=".$id;
mysql_query($sql);

echo "<script>alert('修改成功!');location.href='index.php';</script>";
?>

下载源文件: PHP留言簿开发实例代码源文件

  上一页 1 2 3 4 5 下一页