(四)网站首页显示与用户留言实现过程
该网站的首页设计较为简单,主要向用户显示留言内容和必需的登录、注册、注销、查看主页按钮,游客模式下只能查看留言内容,登录后可以查看个人主页和更多信息,例如评论等。(如何进行判断是否登录以后会提到)
1.首页设计
<script language="JavaScript">
window.history.forward(1);
window.focus();
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>首页</title>
</head>
<body background="bg3.jpg" style="background-repeat:no-repeat">
<center><table style="border:dotted">
<caption>社区首页 <br/> <br/></caption>
<tr><th>用户</th><th>标题</th><th> 发表时间</th></tr>
<?php
@mysql_connect("localhost","root","tiancy"); //连接数据库
mysql_select_db("tiancyDb"); //选择数据库
mysql_query("set names 'utf-8'"); //设定字符集
$id=1;
$q = "select * from messagelist order by id";//设置查询指令
$result = mysql_query($q);//执行查询
while($row = mysql_fetch_assoc($result)){
echo "<tr><td>".$row["username"]."</td><td>".$row["title"]."</td><td>".$row["lastdate"]."</td><td> <a href=\"view.php?id=".$row['id']."\">查看</a></td></tr>";
}
?>
</table><br />
<a href="OwnPage.php">我的主页</a>
<a href="homepage.php">写观点</a>
<a href="login.php">登录</a>
<a href="register.php">注册</a>
<a href="logout.php">注销</a>
</center>
</body>
</html>
界面如下图:
2.用户留言实现
用户留言其实就是将用户的留言存进数据库中,留言界面如下
用户留言的后端实现:
$q = "insert into messagelist(username,title,MSG,lastdate) values('".$_POST["username"]."','".trim($_POST["title"])."','".$_POST["MSG"]."',now())";//设置执行的SQL指令
$result = mysql_query($q);//执行SQL指令
if($result && mysql_affected_rows()>0)
{
echo"<script>alert('留言成功');location.href='firstpage.php';</script>";
}
else{
echo"<script>alert('留言失败,请重试!');location.href='firstpage.php';</script>";
}
以上就是首页的设计和用户留言的实现,这些仅仅完成了网站的基本功能,并没有考虑用户留言安全性的防范,后续会进行安全性的防范,例如session的管理等等。。