[php] 我的微型论坛的简单教程[已完成]


当前第2页 返回上一页

上面的代码应该很简单,相信大家一看就会懂。值的一提的是,我用SESSION["groupID"]来判断是否管理员,如果是,则出现”论坛管理“的连接。

退出的页面loginout.php,逻辑很简单,把所有SESSION释放掉就OK了,PHP中释放全部SESSION的方法是:

<?php
SESSION_DESTROY();//或者使用session_unregister()来注销SESSION
?>

然后登陆其实也很简单了(你明白了注册步骤,登陆就应该很容易了)。部分代码:

<?php
session_start();require_once("conn.php"); //先接收用户登陆表单传来的值,这里略去$sql="select * from member where username='$username'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);    //得到记录的数量
if($num){  //如果用户存在,则检查密码是否正确
    $rs=mysql_fetch_array($result);
    if($rs["password"]!=md5($password)){
        echo"<script>alert('密码不正确,请返回检查!');location.;</script>";
    }else{//用户名、密码都正确,注册SESSION变量,然后跳转到首页
        $_SESSION["username"]=$username;
        $_SESSION["groupID"]=$rs["groupID"];
        $_SESSION["islogined"]="OK";
        echo"<script>alert('登陆成功!');location.;</script>";
    }
}else{//如果没有这个用户
    echo"<script>alert('没有这个用户,请返回检查!');location.;</script>";
}
?>

今天实现了添加论坛版块,用户注册、登陆的功能了,也知道了SESSION的基本用法。

我们在第一章里已经讲了首页如何显示论坛版块,当然,在第二章节里我们论坛添加了排序功能,那么SQL语句当然也应该改成$sql="select * from forums order by forum_list",默认为ASC,顺序排列数据。
现在我们来讨论显示分论坛显示主题的情况。我命名为forums.php.
利用F作为参数,具体来显示某个论坛的帖子。

现在我们进入“测试版块”来显示本论坛下的所有主题。显示结果如图:

具体显示主题列表要解决的主要问提就PHP的分页。PHP分页我们可以使用LIMIT来实现。具体用法:"select clumn field1,...,filedN from table_name limit start_num,end_num";
limit作用是限定结果行数。这里可以提取指定的条数,也可以从查询结果中取出中间值。举个例子来说明:
$sql="select * from [topic] limit 5";
作用是取出topic表中顺序前5条数据;
$sql="select * from [topic] limit 9,10";
提取10条信息,从第9条开始提取。

我们forums.php首先接收F的值:$F=$_GET["F"];注意这里是GET
首先取得当前论坛:

<?php
require_once("conn.php");
require_once("global.php");$F=$_GET["F"];
$sql="select forum_name from forums where ID='$F'";
$result=mysql_query($sql);
$rs=mysql_fetch_array($result);
$forum_name=$rs["forum_name"];echo "当前论坛为:<a href=\"index.php\">$gb_name</a>-->>$forum_name";
?>

我们这里的分页当然使用"select clumn field1,...,filedN from table_name limit start_num,end_num";这种方式。
end_num当然就是我们global.php我们定义的论坛常用的变量$list_rows,而start_num如何计算呢?依靠的是当前页面intpage,每页显示条数$list_rows和总条数来结算的。代码如下:

<?php
//接上面代码
$result=mysql_query("select count(ID) from topic where forum_id='$F'");
$num=mysql_result($result,0);  //获得总条数$intpage=$_GET["intpage"];
if($intpage) $intpage=1;
$start_num=($intpage-1)*$list_rows;  //获取limit开始位置$sql="select ID,title,author,last_post_author as la,last_post_time as lt,no_of_hit,no_of_reply,locked,face,topic,good from topic where forum_id='$F' order by topic desc,last_post_time desc limit $start_num,$list_rows";
$result=mysql_query($sql);//这里当然就是表格的HTML代码啦,自己写吧if($num>0){
    while($rs=mysql_fetch_array($result)){
        $FID=$row["ID"];
        $author=$row["author"];
        $title=$row["title"];
        $no_of_reply=$row["no_of_reply"];
        $no_of_hit=$row["no_of_hit"];
        $lt=$row["lt"];
        $la=$row["la"];
        $topic=$row["topic"];
        $good=$row["good"];
        $locked=$row["locked"];
        $face=$row["face"];
?>
  <tr>
    <td><?php echo "<img src=\"$face\">" ?></td>
    <td><?php echo "<div class=\"bold\"><a class=\"forum\" href=\"thread.php?T=".$FID."&F=".$F."\">".$title."</a></div>" ?></td>
    <td><?php echo $author ?></td>
    <td><?php echo $no_of_reply." / ".$no_of_hit ?></td>
    <td><div class="lastupdate"><?php echo $lt." By ".$la ?></div></td>
  </tr>
<?php
    }
Show_pages($num,$F,$intpage,$list_rows);
}else{
echo"本版块没有帖子";
}
?>

显示结果如下:

分页就是上面代码中体现出来的Show_pages($num,$F,$intpage,$list_rows)。效果就是从1到10,从11-20这样的分页。分页的思路就是这样:
我们得到了所需要的数据:总记录,当前显示页码,每页显示的条数,总记录和每页显示的条数可以得到总页码。如果总页码小于等于10,那么直接输出 1到总页码就可以了。如果大于10,那就根据当前页码显示具体的分页,比如,总页码13页,当前第11页,那么将按照11-13的显示方式输出。同时还会有上一页,下一页等辅助翻页按钮。图示如下:

代码如下:

Function Show_pages($number,$F,$intpage,$list_rows){
$pageno=ceil($number/$list_rows);
echo "\n<table width=90% border=0 align=center cellpadding=0 cellspacing=0 class=mrg-top>";
echo "  <tr>\n    <td align=right>\n  <table border=0 cellpadding=0 cellspacing=0>\n    <tr>";
echo "<td id=pagetop>第".$intpage."页/共".$pageno."页</td>";
if($pageno>1)
echo "\n    <td><div class=pagefirst><a href=?intpage=1&F=".$F.$IsT.">&lt;&lt;</a></div></td>";
if($intpage>1)
echo "\n    <td><div class=pagefirst><a href=?intpage=".($intpage-1)."&F=".$F.$IsT.">&lt;</a></div></td>";

if($pageno>10){
    $a=Floor($pageno/10);
    $b=Floor($intpage/10);
    $c=$b*10+1;
    $d=$b*10+10;
    $e=$intpage%10;
    $g=($b-1)*10+1;
    if($intpage>10){
        if($b<$a){
        for($i=$c;$i<=$d;$i++) echo "\n    <td><div class=pagefirst><a href=?intpage=".$i."&F=".$F.">".$i."</a></div></td>";
        }elseif($e==0){
        for($i=$g;$i<=$intpage;$i++) echo "\n    <td><div class=pagefirst><a href=?intpage=".$i."&F=".$F.">".$i."</a></div></td>";
        }else{
        for($i=$c;$i<=$pageno;$i++) echo "\n    <td><div class=pagefirst><a href=?intpage=".$i."&F=".$F.">".$i."</a></div></td>";
        }
    }else{
    for($i=1;$i<=10;$i++)
    echo "\n    <td><div class=pagefirst><a href=?intpage=".$i."&F=".$F.">".$i."</a></div></td>";
    }
}else{
    for($i=1;$i<=$pageno;$i++)
    echo "\n    <td><div class=pagefirst><a href=?intpage=".$i."&F=".$F.">".$i."</a></div></td>";
}if($pageno>$intpage)
echo "\n    <td><div class=pagefirst><a href=?intpage=".($intpage+1)."&F=".$F.">&gt;</a></div></td>";
if($pageno>1)
echo "\n    <td><div class=pagefirst><a href=?intpage=".$pageno."&F=".$F.">&gt;&gt;</a></div></td>";
echo "</td>\n  </tr>";
echo "\n</table></td></tr></table>";
}

 

我上面的代码自己也认为比较烂,有经验的大鸟不要笑我……^@^,帖个好些的分页代码。
分页这里主要利用的就是LIMIT,熟悉了它的用法,简单的分页程序就可以搞定了……

因为论坛本身的设置是如果该论坛完全开放的话,游客可以发帖子。

<?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"&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"editor.php?F=$F&T=$T&ID=$ID\" class=\"forum\">编辑</a>";
    }elseif($_SESSION["username"] && $_SESSION["islogined"]){
    if($_SESSION["username"]==$author) echo "&nbsp;&nbsp;&nbsp;&nbsp;<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
以上将#换成@
希望得到您的指导。

更多相关Discuz论坛的内容来自木庄网络博客


标签:Discuz论坛

返回前面的内容

相关阅读 >>

网站数据自动备份方法

设置.net1.1 .net2.0安全 防止iisspy 防止aspx木马等

iis下论坛静态化分析

360通用php防护代码(使用操作详解)

discuz!7.1、7.2 远程代码执行漏洞exp

mysql 中文乱码 解决方法集锦

phpcms 2008 整合ucenter的图文方法

discuz批量替换帖子内容的方法(使用sql更新数据库)

telnet 服务器的25端口不通(无法发送邮件)的解决办法

php版本的选择5.2.17 5.3.27 5.3.28 5.4 5.5兼容性问题分析

更多相关阅读请进入《Discuz论坛》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...