discuz simulates login to implement automatic top post php program_PHP tutorial

WBOY
Release: 2016-07-13 17:07:52
Original
1050 people have browsed it

To simulate login in PHP, we need to use the curl_init function. Next, I will introduce how to use curl to simulate post login to the discuz forum and implement the automatic post function.

In fact, simulating login is just a matter of getting the corresponding parameters, then simulating sending, and bringing the obtained COOKIE into the next step
In order to prevent flooding, the discuzx series has been using the formhash() function:

1. Let’s take a look at the formhash function:

The code is as follows Copy code
 代码如下 复制代码

function formhash($specialadd = '') {
    global $_G;
    $hashadd = defined('IN_ADMINCP') ? 'Only For Discuz! Admin Control Panel' : '';
    return          substr(md5(substr($_G['timestamp'],0,-7).$_G['username'].$_G['uid'].$_G['authkey'].$hashadd.$specialadd), 8, 8);
}

function formhash($specialadd = '') {

global $_G;
$hashadd = defined('IN_ADMINCP') ? 'Only For Discuz! Admin Control Panel' : '';
return      substr(md5(substr($_G['timestamp'],0,-7).$_G['username'].$_G['uid'].$_G['authkey'].$hashadd.$specialadd ), 8, 8);
}


Note: Generation method:
1. The intercepted timestamp
2. Username
3. User ID 4. authkey

5. hashadd(fixed value) 6. specialadd(fixed value) Because there is a user name and user id, the values ​​generated before logging in and after logging in are different, that is: you have to get fromhash twice before logging in and after logging in;

So what is authkey?

 代码如下 复制代码
 if(empty($this->var['cookie']['saltkey'])) {
 $this->var['cookie']['saltkey'] = random(8);//这一步不要去管
 dsetcookie('saltkey', $this->var['cookie']['saltkey'], 86400 * 30, 1, 1);
}
$this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey']);
2. About authkey



//Code location:/source/class/discuz/discuz_application.php

The code is as follows Copy code
if(empty($this->var ['cookie']['saltkey'])) {

$this->var['cookie']['saltkey'] = random(8);//Don't worry about this step dsetcookie('saltkey', $this->var['cookie']['saltkey'], 86400 * 30, 1, 1); }

$this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey ']);


You can see here: the authkey is generated based on the authkey in the configuration file and the saltkey in the cookie
In fact, you can see it here. As long as the value of $this->var['cookie']['saltkey'] of the website is always stored in the cookie, it can be guaranteed that the value generated by formhash will always be the same ( It’s always relative) One more thing to say here:
 代码如下 复制代码
$this->var['config']['cookie']['cookiepre'] = $this->var['config']['cookie']['cookiepre'].substr(md5($this->;var['config']['cookie']['cookiepath'].'|'.$this->;var['config']['cookie']['cookiedomain']), 0, 4).'_';
3 cookie series The prefix of discuz’s cookie is randomly generated Code location:/source/class/discuz/discuz_application.php


As long as you log in to the website and look at the cookie settings, it will be fine. The prefix hi will indeed remain unchanged. Of course, it will definitely change if the configuration file is changed
How should I write it: Let me talk about my idea of ​​implementation
1. After logging in, visit the website and grab the saltkey (cookie) and formhash (value) returned by the web page (saltkey must be brought below, and the context must be consistent)
2. Construct the login content and then simulate post submission (be sure to bring the cookie and formhash value captured in the first step)
3. If the login is successful, then obtain the formhash of a page and the set cookie (the formhash obtained this time is what you can always use)
4. Construct posts and top posts and submit them to the page (cookies and formhash are still important)
About php using crul to simulate post part of the code

$ch = curl_init($url); //Initialization curl_setopt($ch, CURLOPT_HEADER, 1); //Does not return the header part
The code is as follows
 代码如下 复制代码

$ch = curl_init($url); //初始化
    curl_setopt($ch, CURLOPT_HEADER, 1); //不返回header部分
    curl_setopt($ch, CURLOPT_POST, 1);//是否
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
    curl_setopt($ch,CURLOPT_REFERER,$refer);
    curl_setopt($ch, CURLOPT_COOKIE,  $tocookies); //存储cookies
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);

Copy code

curl_setopt($ch, CURLOPT_POST, 1);//Whether curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Return a string instead of outputting it directly

curl_setopt($ch,CURLOPT_REFERER,$refer);

curl_setopt($ch, CURLOPT_COOKIE, $tocookies); //Storage cookies The code will not be shared, friends who are capable can write it themselves; this thing can easily cause confusion
http://www.bkjia.com/PHPjc/629893.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/629893.htmlTechArticleTo simulate login in PHP, we need to use the curl_init function. Below I will introduce how to use curl to simulate post login to the discuz forum and realize automatic Like post function. In fact, simulated login is just that, nothing...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template