<p> 数据库结构:(库名:lyb)</p><p> 表一: admin</p><p> 字段: id(int11) name(varchvr) password(varchvr)</p><p> 表二: lo</p><p> 字段: id(int11) username(varchvr) sex(varchvr) qq(varchvr) email(varchvr) info(text) ip(varchvr) submit_time(datetime)</p><p> 1 conn.php(连接数据库文件)</p><code><?php<br />mysql_connect("localhost","root","");//连接数据库<br />mysql_select_db("lyb");//选择数据库<br />?></code><p> 2 header.php(公用头部文件)</p><code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><br /><title>PHP和MYSQL联合打造简单留言本程序</title><br /></head><br /><link href="css.css" rel="stylesheet" type="text/css" /><br /><body><br /><div id="head"><br /> <div id="head_l"><br /> <ul><br /> <li><a href="index.php">偶要看留言</a></li><br /> <li><a href="post.php">偶要发表</a></li><br /> <?php<br /> session_start();<br /> if($_SESSION["key"]==1){//如果获取的SESSION为1则显示管理项<br /> ?><br /> <li><a href="adminexit.php">退出管理</a></li><br /> <?<br /> }<br /> else<br /> {<br /> ?><br /> <li><a href="admin.php">偶要管理</a></li><br /> <?}?><br /> </ul><br /> </div><br /> <div id="head_r">PHP和MYSQL联合打造简单留言本程序</div><br /></div></code><p> 3 footer.php(公用底部文件)</p>
<p> </p>
<code><?php<br />$counterFile="conter.xml";<br />function displayCounter($counterFile){<br />$fp = fopen($counterFile,"rw");<br />$num = fgets($fp,5);<br />$num += 1;<br />print "<div id="footer">简单留言板 您是第 "."$num"." 位无聊的银</div>";<br />exec("rm -rf $counterFile");<br />exec("echo $num > $counterFile");<br />}<br />if(!file_exists($counterFile)){<br />exec("echo 0 > $counterFile");<br />}<br />displayCounter($counterFile);<br />?><br /></body><br /></html></code><p> 4 index.php(首页)</p><code><?php<br />require_once("conn.php");<br />require_once("header.php");<br />session_start();<br />//分页代码开始<br />$pagesize = 10;//设置每页显示条数<br />$rs = mysql_query("select count(*) from lo");//取得记录总数,计算总页数用<br />$myrow = mysql_fetch_array($rs);<br />$numrows = $myrow[0];//计算总记录<br />$pages = intval($numrows/$pagesize);<br />if($numrows%$pagesize)$pages++;//设置页数<br />if(isset($_GET['page']))<br /> {<br /> $page = intval($_GET['page']);<br /> }<br /> else<br /> {<br /> $page = 1;//设为第一页<br /> }<br />$offset = $pagesize*($page-1);//计算记录偏移量<br />//分页代码结束<br />$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格式化日期格式<br />$result = mysql_query($sql);<br />$num = mysql_num_rows($result);<br />if($num>0){<br /> while($row = mysql_fetch_array($result))<br /> {<br /> //echo print_r($row);<br /> if($row[2]=="男")//这个使性别改成你想要的名称^_^<br /> {<br /> $sex = "帅锅";<br /> }<br /> else<br /> {<br /> $sex = "美女";<br /> }<br />?><br /><div id="show"><br /> <p class="num">第 [<?= $row[0] ?>] 条留言<p><br /> <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.webjx.com&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><br /> <p class="blue">留言内容:</p><br /> <div id="show_info"><?= nl2br(htmlspecialchars($row[5])) ?></div><br /></div><br /><?php<br /> }<br />}<br />else<br /> {<br /> echo "<div id="show">无数据......</div>";<br /> }<br />?><br /><div id="show_page"><br /><p><br /><?php<br />$first=1;<br />$prev=$page-1;<br />$next=$page+1;<br />$last=$pages;<br />if($page==1&&$pages>1)<br />{<br /> echo "首页 | ";<br /> echo "上一页 | ";<br /> echo "<a href="index.php?page=".$next."">下一页</a> | ";<br /> echo "<a href="index.php?page=".$last."">尾页</a> | ";<br />}<br />elseif($page>=1&&$page!=$pages&&$num>0)<br />{<br /> echo "<a href="index.php?page=".$first."">首页</a> | ";<br /> echo "<a href="index.php?page=".$prev."">上一页</a> | ";<br /> echo "<a href="index.php?page=".$next."">下一页</a> | ";<br /> echo "<a href="index.php?page=".$last."">尾页</a> | ";<br />}<br />elseif($page==$pages&&$page!=1)<br />{<br /> echo "<a href="index.php?page=".$first."">首页</a> | ";<br /> echo "<a href="index.php?page=".$prev."">上一页</a> | ";<br /> echo "下一页 | ";<br /> echo "尾页 | ";<br />}<br />elseif($page==$pages)<br />{<br /> echo "首页 | ";<br /> echo "上一页 | ";<br /> echo "下一页 | ";<br /> echo "尾页 | "; <br />}<br />else<br />{<br /> echo "首页 | ";<br /> echo "上一页 | ";<br /> echo "下一页 | ";<br /> echo "尾页 | ";<br />}<br />?><br />共 <span><?= $pages ?></span> 页 | 当前第 <span><?= $page ?></span> 页 | 共 <span><?=$numrows ?></span> 条留言</p><br /></div><br /><?php<br />mysql_close();<br />?><br /><?php<br />require_once("footer.php");<br />?></code><p> 5 post.php(提交留言页面)</p>
<p> </p>
<code><?<br />require_once("header.php");<br />?><br /><div id="input"><br /><form method="post" action="input.php" name="form1"><br /> <h1>提交留言</h1><br /> <p>姓名 :<input type="text" name="name" size="20" class="y" /> </p><br /> <p>性别 :<input name="sex" type="radio" value="男" checked/> 帅锅 <input type="radio" name="sex" value="女"/> 美女</p><br /> <p>Q Q :<input type="text" name="qq" class="y" /> (可选填)</p><br /> <p>Email:<input type="text" name="email" class="y" /> (可选填)</p><br /> <p>留言内容:</p><br /> <p><textarea name="info" rows="5" cols="40"></textarea></p><br /> <p class="cen"><br /> <input type="submit" value="偶填好了" /><br /> <input type="reset" value="偶要重写"><br /> </p><br /> <p class="cen1">留言板 Version 1.0</p><br /></form><br /></div><br /></body><br /></html></code><p> 6 input.php(插入留言)</p><code><?php<br />require_once("conn.php");<br />$username = $_POST['name'];<br />$sex = $_POST['sex'];<br />$qq = $_POST['qq'];<br />$email = $_POST['email'];<br />$info = $_POST['info'];<br />if (strrpos($username,"<")!==false || strrpos($username,">")!==false||strrpos($username,"@")!==false||strrpos($username,""")!==false||strrpos($username,"'")!==false||strrpos($username,"_")!==false)<br />{<br /> echo "<script>alert('名称不能有特殊字符!');location.href='post.php';</script>";<br /> exit();<br />}<br />if (!ereg("^[0-9]{0,}$",$qq))//用正则检查QQ格式<br />{<br /> echo "<script>alert('OICQ信息有错误!必须是数字!');location.href='post.php';</script>";<br /> exit();<br />}<br />if($email)<br />{//如果填写了邮箱就用正则检查邮箱格式<br /> if (!ereg("^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$",$email))<br /> {<br /> echo "<script>alert('email格式不正确!');location.href='post.php';</script>";<br /> exit();<br /> }<br />}<br />if(!$username)<br />{<br /> echo "<script>alert('名称不能为空哦!');location.href='post.php';</script>";<br /> exit();<br />}<br />elseif(!$info)<br />{<br /> echo "<script>alert('留言不能为空哦!');location.href='post.php';</script>";<br /> exit();<br />}<br />else<br />{<br /> $ip = getenv('REMOTE_ADDR');//获取客户端IP地址<br /> $sql = "insert into lo (username,sex,qq,email,info,ip,submit_time) values ('$username','$sex','$qq','$email','$info','$ip',NOW())";<br /> $result = mysql_query($sql);<br /> mysql_close();<br /> echo "<script>alert('提交成功!返回首页');location.href='index.php';</script>";<br />}<br />?></code><p> 7 update.php(修改留言页)</p>
<p> </p>
<code><?php<br />session_start();<br />require_once('conn.php');<br />require_once('header.php');<br />if($_SESSION["key"]==1)<br />{<br />$id = $_GET['id'];<br />$sql = "select * from lo where id= ".$id;<br />$result = mysql_query($sql);<br />$row = mysql_fetch_array($result);<br />?><br /><div id="update"><br /> <form action="updatepost.php?wuleying&id=<?= $row[0] ?>" method="post"><br /> <h1>修改留言</h1><br /> <p>姓名:<input type="text" value="<?= $row[1]?>" name="name" class="y"></input></p><br /> <p>留言:</p><br /> <p><textarea name="info" rows="5" cols="35"><?= $row[5] ?></textarea></p><br /> <p class="cen"><input type="submit" value="偶要修改" /></p><br /> <p class="cen1">留言板 Version 1.0</p><br /> </form><br /><div><br /><?php<br />}<br />else<br />{<br />header('location:index.php');<br />}<br />?></code><p> 8 updatepost(修改提交页)</p><code><?php<br />require_once('conn.php');<br />$username = $_POST['name'];<br />$info = $_POST['info'];<br />$id = $_GET['id'];<br />//echo $id;<br />$sql = "update lo set username= '".$username."',info='".$info."' where id=".$id;<br />mysql_query($sql);<br />echo "<script>alert('修改成功!');location.href='index.php';</script>";<br />?></code><p> 9 delete.php(删除留言页)</p>
<p> </p>
<code><?php<br />session_start();<br />require_once('conn.php');<br />$id=$_GET['id'];<br />if($_SESSION["key"]==1)<br />{<br />$sql = "delete from lo where id=".$id;<br />mysql_query($sql);<br />echo "<script>location.href='index.php'</script>";<br />}<br />else<br />{<br />header('location:index.php');<br />}<br />?></code><p> 10 admin.php(管理登录页)</p><code><?php<br />require_once('conn.php');<br />require_once('header.php');<br />?><br /><div id="admin"><br /> <form method="post" action="adminpost.php"><br /> <h1>管理员登录</h1><br /> <p>姓名 : <input type="text" name="name" size="20" class="y" /> </p><br /> <p>密码 : <input type="password" name="password" size="20" class="y" /> </p><br /> <p class="cen"><input type="submit" value="管理员登录" /></p><br /> <p class="cen1">留言板 Version 1.0</p><br /> </form><br /></div></code><p> 11 adminpost.php(管理验证页)</p><code><?php<br />session_start();<br />require_once('conn.php');<br />$name = $_POST['name'];<br />$password = $_POST['password'];<br />$sql = "select * from admin where name='".$name."'";<br />$result = mysql_query($sql);<br />$num = mysql_num_rows($result);<br />if (strrpos($name,"<")!==false || strrpos($name,">")!==false||strrpos($name,"@")!==false||strrpos($name,""")!==false||strrpos($name,"'")!==false||strrpos($name,"_")!==false)<br />{<br /> echo "<script>alert('不能有特殊字符!');location.href='admin.php';</script>";<br />}<br />if($num)<br />{//如果用户存在,就检查密码是否正确<br /> $rs = mysql_fetch_array($result);<br /> if($rs[2]!=$password)<br /> {<br /> echo "<script>alert('密码不正确,请确认后输入!');location.href='admin.php';</script>";<br /> }<br /> else<br /> {//用户名,密码都正确,注册SESSION变量,然后跳转到首页<br /> $_SESSION["key"]=1;<br /> echo "<script>alert('登录成功!');location.href='index.php';</script>";<br /> }<br />}<br />else<br />{//如果没有这个用户<br /> echo "<script>alert('没有这个用户,请确认后输入!');location.href='admin.php';</script>";<br />}<br />?></code><p> 12 adminexit.php(退出管理页)</p></p><code><?php<br />session_start();<br />$_SESSION["key"] = 0;//使SESSION不为1,0为游客,1为管理员<br />header('location:index.php');<br />?></code><p> 只是些基本的东东,像MD5加密,UBB,表情图片,添加管理员账号,回复留言(-_!!)等我都没加上去,有兴趣的同学可以完善下。</p></p>