返回介绍

16.6.5 发帖功能讲解

发布于 2025-01-30 22:11:38 字数 5729 浏览 0 评论 0 收藏 0

发贴功能

模板所在位置: /theme/default/addc.html

PHP 页面 addc.php

操作流程

  1. 用户点击进入发贴页的时候,将所选版块的 ID 带上。URL 示例如下: http://bbs.com/addc.php?classid=5。我们就可以知道用户是向哪个版块发贴
  2. 判断用户是否存在
  3. 判断版块是否存在,如果不存在的话,插入的版块即非法。我们让用户发贴不能成功。
  4. 展示导航,版块名等基本信息,渲染进入模板
  5. 准本好要插入的基本信息,拼接 SQL 语句,并插入库
  6. 判断是否写入成功
  7. 成功给用户金币(积分),跳转至贴子页

1. 判断用户是否登陆,未登陆不能发贴

//包含公共组件
include './common/common.php';

//判断用户是否登录
if(!$_COOKIE['uid'])
{
  $notice = '抱歉,您尚未登录,没有权限在该版块发帖';
  include 'close.php';
  exit;
}

2.判断 ID 是否存在

  if(empty($_REQUEST['classid']) || !is_numeric($_REQUEST['classid']))
{
  $msg = '<font color=red><b>禁止非法操作</b></font>';
  $url = $_SERVER['HTTP_REFERER'];
  $style = 'alert_error';
  $toTime = 3000;
  include 'notice.php';
}
$classId = $_REQUEST['classid'];

3. 读取版块相关的基本信息

  $category = dbSelect('category','cid,classname,parentid','parentid<>0 and cid='.$classId.'','',1);
if($category){

  $smallName = $category[0]['classname'];
  $smallId = $category[0]['cid'];
  $parentCategory = dbSelect('category','cid,classname','cid='.$category[0]['parentid'].'','',1);
  if($parentCategory)
  {
    $bigName=$parentCategory[0]['classname'];
    $bigId=$parentCategory[0]['cid'];
  }else{
    $msg = '<font color=red><b>非法操作</b></font>';
    $url = $_SERVER['HTTP_REFERER'];
    $style = 'alert_error';
    $toTime = 3000;
    include 'notice.php';
    exit;
  }

}else{
  $msg = '<font color=red><b>非法操作</b></font>';
  $url = $_SERVER['HTTP_REFERER'];
  $style = 'alert_error';
  $toTime = 3000;
  include 'notice.php';
  exit;
}

4.准备发布内容

$authorid = $_COOKIE['uid'];    //发布人 ID
$title = strMagic($_POST['subject']);     //标题
$content = strMagic($_POST['content']);   //内容
$addtime = time();      //发表时间
$addip = ip2long($_SERVER['REMOTE_ADDR']);//发布人 IP
$classId = $_POST['classid'];     //类别 ID
$rate = $_POST['price'];      //帖子售价

$n = 'first, authorid, title, content, addtime, addip, classid, rate';

$v = '1, '.$authorid.', "'.$title.'", "'.$content.'", '.$addtime.', '.$addip.', '.$classId.', '.$rate.'';

$result = dbInsert('details', $n, $v);

整体代码展示

<?php
/**
* 发帖子
*/

include './common/common.php';

//判断用户是否登录
if(!$_COOKIE['uid'])
{
  $notice = '抱歉,您尚未登录,没有权限在该版块发帖';
  include 'close.php';
  exit;
}

//判断 ID 是否存在
if(empty($_REQUEST['classid']) || !is_numeric($_REQUEST['classid']))
{
  $msg = '<font color=red><b>禁止非法操作</b></font>';
  $url = $_SERVER['HTTP_REFERER'];
  $style = 'alert_error';
  $toTime = 3000;
  include 'notice.php';
}
$classId = $_REQUEST['classid'];

//读取导航索引
$category = dbSelect('category','cid,classname,parentid','parentid<>0 and cid='.$classId.'','',1);
if($category){

  $smallName = $category[0]['classname'];
  $smallId = $category[0]['cid'];
  $parentCategory = dbSelect('category','cid,classname','cid='.$category[0]['parentid'].'','',1);
  if($parentCategory)
  {
    $bigName=$parentCategory[0]['classname'];
    $bigId=$parentCategory[0]['cid'];
  }else{
    $msg = '<font color=red><b>非法操作</b></font>';
    $url = $_SERVER['HTTP_REFERER'];
    $style = 'alert_error';
    $toTime = 3000;
    include 'notice.php';
    exit;
  }

}else{
  $msg = '<font color=red><b>非法操作</b></font>';
  $url = $_SERVER['HTTP_REFERER'];
  $style = 'alert_error';
  $toTime = 3000;
  include 'notice.php';
  exit;
}

//发布帖子
if($_POST['topicsubmit'])
{
  $authorid = $_COOKIE['uid'];    //发布人 ID
  $title = strMagic($_POST['subject']);     //标题
  $content = strMagic($_POST['content']);   //内容
  $addtime = time();      //发表时间
  $addip = ip2long($_SERVER['REMOTE_ADDR']);//发布人 IP
  $classId = $_POST['classid'];     //类别 ID
  $rate = $_POST['price'];      //帖子售价

  $n = 'first, authorid, title, content, addtime, addip, classid, rate';
  $v = '1, '.$authorid.', "'.$title.'", "'.$content.'", '.$addtime.', '.$addip.', '.$classId.', '.$rate.'';
  $result = dbInsert('details', $n, $v);

  $insert_id = dbSelect('details','id','title="'.$title.'"','id desc',1);
  $insertId = $insert_id[0]['id'];
  if(!$result){

    $msg = '<font color=red><b>帖子发表失败,请联系管理员</b></font>';
    $url = $_SERVER['HTTP_REFERER'];
    $style = 'alert_error';
    $toTime = 3000;
    include 'notice.php';
    exit;

  }else{

    $money = REWARD_T;  //发帖赠送积分
    $result = dbUpdate('user', "grade=grade+{$money}", 'uid='.$_COOKIE['uid'].'');

    //更新版块表的主题数量[Motifcount](跟帖是回复数量[eplycount]) 和最后发表[lastpost]
    $last = $insertId.'+||+'.$title.'+||+'.$addtime.'+||+'.$_COOKIE['username'];
    $result = dbUpdate('category', 'motifcount=motifcount+1, lastpost="'.$last.'"', 'cid='.$classId.'');

    $msg = '<font color=red><b>帖子发表成功</b></font>';
    $url = 'detail.php?id='.$insertId;
    $style = 'alert_right';
    $toTime = 3000;
    include 'notice.php';

    $msg = '发帖赠送';
    include 'layer.php';
    exit;

  }

}

$OnMenu = dbSelect('category','cid,classname','cid='.$classId.' and ispass=1','orderby desc,cid desc');
if(!$OnMenu)
{
  $msg = '<font color=red><b>没有找到该版块</b></font>';
  $url = $_SERVER['HTTP_REFERER'];
  $style = 'alert_error';
  $toTime = 3000;
  include 'notice.php';
}else{
  $OnCid = $OnMenu[0]['cid'];
  $OnCname = $OnMenu[0]['classname'];
}

$title = '发表帖子'.$OnCname.' - '.WEB_NAME;
$menu = WEB_NAME;
include template("addc.html");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文