因为论坛本身的设置是如果该论坛完全开放的话,游客可以发帖子。
<?php if(!isset($_SESSION["username"])){ $_SESSION["username"]="Guest"; } ?>
那么该论坛的页面应该相应的有添加帖子的按钮:
<?php $F=$_GET["F"]; $result=mysql_fetch_array(mysql_query("select isguest from forums where ID='$F'")); $isguest=$result["isguest"]; //从数据库中提取该论坛版块是否完全开放,如果完全开放游客就可以发帖子了,否则只有注册用户才可以 if($isguest==0){ if($_SESSION["username"] && $_SESSION["islogined"]) echo"<a href=addnew.php?F=$F><img src=\"images/add.gif\" /></a>"; }else{ echo"<a href=addnew.php?F=$F><img src=\"images/add.gif\" /></a>"; } ?>
addnew.php?F=N 这里传递函数。表明添加新帖子要添加到论坛版块。 添加页面如图所示:
 点击放大
这里的表单里会有一个隐藏字段,记录论坛版面的ID号。
处理表单非常简单,就是INSERT来实现:
<?php require_once "global.php"; require_once "conn.php"; $F=$_POST["F"]; $title=$_POST["title"]; $content=$_POST["Content"]; $author=$_POST["username"]; $face=$_POST["face"]; $result=mysql_fetch_array(mysql_query("select isguest from forums where ID='$F'")); $isguest=$result["isguest"]; //下面肯定要再验证一下该论坛版块用户是否可以发帖 if($isguest==0){ if(empty($_SESSION["username"])||empty($_SESSION["islogined"])){ echo "<script>alert('您尚未登陆,请先登陆!');location='login.php';</script>"; exit(); } } $sql="insert into topic (title,author,last_post_author,last_post_time,no_of_hit,no_of_reply, locked,face,topic,good,forum_id) values ('$title','$author','$author',now(),0,0,0,'$face',0,0,'$F')"; mysql_query($sql); $topicID=mysql_insert_id(); //这里:mysql_insert_id()函数可以取出刚才插入操作成功后的ID值; //插入到主题表是第一步 $sql="insert into thread (topicID,face,title,author,post_time,subject) values ('$topicID','$face','$title','$author',now(),'$content')"; mysql_query($sql); //插入到帖子表是第二步 if($author!="Guest"){ $sql="update member set no_of_post = no_of_post + 1 where username='$author'"; mysql_query($sql); //如果不是游客的话就更新用户表。其实这里用户表中开始就应该存在一个管理员和一个游客的信息。游客发帖子默认的值为空,这样即使更新也更新不了。 } ?>
现在我们来看thread.php。这个页面和forums.php有很相似的地方,包括分页。只是这里提取了thread表中的所有数据。
 如下:
<?php $sql="select A.ID,A.title,A.author,A.post_time,A.subject,A.face,B.groupID,B.email, B.headimg,B.homepage,B.qq,B.MSN,B.jointime,B.no_of_post,B.sign from thread A,member B where A.topicID=$T and A.author = B.username order by A.post_time asc limit $p_start,$tread_list_rows"; $result=mysql_query($sql); //这里的SQL语句是查询2个表,MEMBER和THREAD表。 $sqlno="select * from thread where topicID='$T'"; $number=mysql_num_rows(mysql_query($sqlno)); while($row=mysql_fetch_array($result)){ //这里是HTML代码 } ?> 果当前用户有权限发表帖子,那么每页下面将出现快速回复的表单。
 点击放大
<?php if($isguest==0){ if($_SESSION["username"] && $_SESSION["islogined"]){ //这里是快速回复的HTML表单 } }else{ //这里是快速回复的HTML表单 } //思路也很简单,如果完全开放,自然而然就出现回复表单,允许回复;否则如果用户登陆,就出现回复表单。 ?>
回复表单里要有主题帖子的ID编号。
回复的代码同样是插入,然后更新相关表。
<?php require_once "global.php"; require_once "conn.php"; $F=$_POST["F"]; $T=$_POST["T"]; $title=$_POST["title"]; $content=$_POST["Content"]; $author=$_POST["username"]; $face=$_POST["face"];$result=mysql_fetch_array($db->db_query("select isguest from forums where ID='$F'")); $isguest=$result["isguest"];
if($isguest==0){ if(empty($_SESSION["username"])||empty($_SESSION["islogined"])){ echo "<script>alert('您尚未登陆,请先登陆!');location='login.php';</script>"; exit(); } }
$sql="insert into thread (topicID,face,title,author,post_time,subject) values ('$T','$face','$title','$author',now(),'$content')"; $db->mysql_query($sql); //插入表,同时记录TOPIC的主键 $sql="update topic set last_post_author ='$author',last_post_time=now(),no_of_reply = no_of_reply + 1 where ID = '$T'"; $db->mysql_query($sql); //更新主题表,最后回复人,最后更新时间 $sql="update forums set last_post_author='$author',last_post_time=now() where ID='$F'"; $db->mysql_query($sql); //更新论坛版块的信息,最后回复,最后更新时间 if($author!="Guest"){ $sql="update member set no_of_post = no_of_post + 1 where username='$author'"; mysql_query($sql); //更新发帖人的发帖数量 } ?>
编辑帖子,同样要求权限。必须登陆;用户必须是帖子的作者;管理员可以管理所有的帖子

<?php if($_SESSION["groupID"]=="2"){ echo" <a href=\"editor.php?F=$F&T=$T&ID=$ID\" class=\"forum\">编辑</a>"; }elseif($_SESSION["username"] && $_SESSION["islogined"]){ if($_SESSION["username"]==$author) echo " <a href=\"editor.php?F=$F&T=$T&ID=$ID\" class=\"forum\">编辑</a>"; } ?>
 点击放大
<?php require_once "global.php"; require_once "conn.php"; $F=$_GET["F"]; $T=$_GET["T"]; $ID=$_GET["ID"]; if(empty($F)||empty($T)||empty($ID)) echo "<script>history.back;</script>"; $sql="select A.author,A.title,A.face,A.subject,B.title as topictitle from thread A,topic B where A.ID='$ID' and A.topicID=B.ID"; $rs=mysql_fetch_array($db->db_query($sql)); $rename=$rs["author"]; $title=$rs["title"]; $face=$rs["face"]; $topictitle=$rs["topictitle"]; $resubject=$rs["subject"]; if($_SESSION["groupID"]!="2"){ if(($_SESSION["username"]!=$rename)||empty($_SESSION["islogined"])) echo "<script>history.go(-1);</script>"; } $sresult=mysql_fetch_array(mysql_query("select forum_name,isguest from forums where ID='$F'")); $forum_name=$sresult["forum_name"]; //这里检查当前用户是否有编辑帖子的权限,并且按照ID号提取出该帖子的所有内容 ?>
处理编辑的帖子就是更新原先数据。这里不做多说了。
////////////////////////////////////////////////// 下面到了个人资料管理。control.php,管理“我的资料”


这里比较简单,也不再写了。
正如5do8所说的,最好要把常用的程序(如连接数据库)写成类,容易管理,而且速度和性能上也有提高。我是个菜鸟,对于类不甚了解,想了解这个东西的朋友请参照这里: http://www.phpchina.com/bbs/viewthread.php?tid=13765&highlight=
这里是按照我的理解来写的这个论坛的连接数据库类:dbclass.php
class Eastsin { function db_connect($db_host_ip,$db_login_name,$db_login_password){ mysql_connect($db_host_ip,$db_login_name,$db_login_password); } function db_select($db_name){ mysql_select_db($db_name); } function db_query($sql){ return mysql_query($sql); } function db_fetch_array($result){ return mysql_fetch_array($result); } function db_result($query,$row){ return mysql_result($query,$row); } function db_rows($query){ return mysql_num_rows($query); } function db_ID(){ return mysql_insert_id(); } function db_close(){ mysql_close(); } }
在使用的时候:
$db=new Eastsin; //初始化一个类,并把这个对象赋给变量$db $db->db_selsct($dbname); //访问类的方法,类中定义的函数即为类的方法。 $sql="...."; $db->db_query($sql); /* 上面两句等同于: $sql="...."; mysql_query($sql); 类中其他方法的使用同上; */
我的论坛还在完善中,还有关于安全性、容错处理等我也再学习中。一个小论坛从思路上讲还是比较简单的,但是真正做起来还是要费些力气和脑筋的。 (源文件请大家允许我稍后发布)
这样,这个小教程算是简单的完成了。作者水平有限,也没有写过教程的经验,所以里面的不足之出大家多包涵指点。在此谢谢大家!! 我的 QQ:278502721; MSN:fengyuedao#hotmail.com或eastsin.com#hotmail.com; E-mail:numsix#163.com 以上将#换成@ 希望得到您的指导。
经典论坛讨论: http://bbs.blueidea.com/thread-2703251-1-1.html
本文链接:http://www.blueidea.com/tech/program/2006/4352.asp
出处:蓝色理想
责任编辑:moby
上一页 我的微型论坛的简单教程 [8] 下一页
◎进入论坛网络编程版块参加讨论
|