设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 118|回复: 0

Discuz! X2.5 初级发帖源码

[复制链接]
发表于 2013-11-1 14:27:18 | 显示全部楼层 |阅读模式
Detail:

  1. <?php
  2. if($_POST["submit"] == "提交"){
  3.     $discuz_url = 'http://localhost/comsenz/discuz/upload/';//论坛地址

  4.     $login_url = $discuz_url.'member.php?mod=logging&action=login';//登录页地址

  5.     $post_fields = array();
  6.     //以下两项不需要修改
  7.     $post_fields['loginfield'] = 'username';
  8.     $post_fields['loginsubmit'] = 'true';

  9.     //用户名和密码,必须填写
  10.     $post_fields['username'] = $_POST["username"];
  11.     $post_fields['password'] = $_POST["password"];

  12.     //安全提问
  13.     $post_fields['questionid'] = 0;
  14.     $post_fields['answer'] = '';

  15. //@todo验证码
  16.     $post_fields['seccodeverify'] = '';

  17.     //获取表单FORMHASH
  18.     $ch = curl_init($login_url);
  19.     curl_setopt($ch, CURLOPT_HEADER, 0);
  20.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21.     $contents = curl_exec($ch);
  22.     curl_close($ch);

  23.     preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);

  24.     if(!empty($matches)) {
  25.         $formhash = $matches[1];
  26.     } else {
  27.         die('Not found the loginhash.');
  28.     }

  29.     //POST数据,获取COOKIE,cookie文件放在网站的temp目录下
  30.     $cookie_file = tempnam('./temp','cookie');//建立一个临时档,返回临时档名

  31.     $ch = curl_init($login_url);

  32.     curl_setopt($ch, CURLOPT_HEADER, 0);
  33.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34.     curl_setopt($ch, CURLOPT_POST, 1);
  35.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  36.     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);

  37.     curl_exec($ch);
  38.     curl_close($ch);

  39.     //取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID
  40.     $send_url = $discuz_url."forum.php?mod=post&action=newthread&fid=".$_POST["fid"];

  41.     $ch = curl_init($send_url);

  42.     curl_setopt($ch, CURLOPT_HEADER, 0);
  43.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  44.     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

  45.     $contents = curl_exec($ch);
  46.     curl_close($ch);

  47.     //这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
  48.     preg_match('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
  49.    
  50. #echo $contents;exit;

  51. if(!empty($matches)) {
  52.         $formhash = $matches[1];
  53.     } else {
  54.         die('Not found the posthash.');
  55.     }

  56.     $post_data = array();

  57.     //帖子标题
  58.     $post_data['subject'] = $_POST["subject"];

  59.     //帖子内容
  60.     $post_data['message'] = $_POST["message"];

  61.     $post_data['topicsubmit'] = "yes";
  62.     $post_data['extra'] = '';

  63.     //帖子标签
  64.     $post_data['tags'] = 'test';

  65.     //帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
  66.     $post_data['formhash']=$formhash;

  67.     $ch = curl_init($send_url);

  68.     curl_setopt($ch, CURLOPT_REFERER, $send_url);       //伪装REFERER
  69.     curl_setopt($ch, CURLOPT_HEADER, 0);
  70.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  71.     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  72.     curl_setopt($ch, CURLOPT_POST, 1);
  73.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

  74.     $contents = curl_exec($ch);
  75.     curl_close($ch);

  76.     //清理cookie文件
  77.     unlink($cookie_file);
  78.     echo "<script>alert('发帖成功!'); window.location='dz.php';</script>";

  79. } else {
  80. ?>
  81.    <form id="dz_post" name="dz_post" method="post" action="">      
  82.             用户名:<input type="text" name="username"  /><br />
  83.             密码:<input type="text" name="password"  /><br />
  84.             版块ID:<input type="text" name="fid"  /><br />
  85.             标题:<input type="text" name="subject"  /><br />
  86.             内容:<input type="text" name="message"  /><br />
  87.             <input type="submit" name="submit" value="提交" />
  88.         </form>  
  89. <?
  90. }
  91. ?>
复制代码
织梦的大白兔
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 22:29 , Processed in 0.013979 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表