Php CURL simulates login to the Forum and collects data instances

Source: Internet
Author: User
Tags curl hash website server

To simulate browser access to a website, you should first learn how the browser sends http packets and what content the website server returns to the browser. I recommend installing an httpwatch software developed by a foreign user. It is best to build a cracked version. Otherwise, some functions cannot be used. After the software is installed, it is embedded in IE. Start Record, enter the URL in the address bar, and press Enter. It will scan all the communications between the browser and the server for you to see at a glance. This document does not describe the use of this software.

Simulating browser login to application development, the most important thing is to break through login verification. CURL not only supports http, but also https. The difference lies in an additional layer of SSL encrypted transmission. To log on to the https website, php must support openssl. Let's take an example for analysis.

The code is as follows: Copy code

<? Php
$ Discuz_url = 'http: // 127.0.0.1/discuz/'; // forum address
$ Login_url = $ discuz_url. 'Logging. php? Action = login '; // logon page address

$ Post_fields = array ();
// The following two items do not need to be modified
$ Post_fields ['loginfield'] = 'username ';
$ Post_fields ['loginsubmit '] = 'true ';
// Username and password, required
$ Post_fields ['username'] = 'tiancei ';
$ Post_fields ['password'] = '000000 ';
// Security question
$ Post_fields ['questionid'] = 0;
$ Post_fields ['answer'] = '';
// @ Todo verification code
$ Post_fields ['seccodeverify '] = '';

// Obtain the FORMHASH form
$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );
Preg_match ('/<inputs * type = "hidden" s * name = "formhash" s * value = "(.*?) "S */>/I ', $ contents, $ matches );
If (! Empty ($ matches )){
$ Formhash = $ matches [1];
} Else {
Die ('not found the forumhash .');
}

// POST the data to obtain the COOKIE. The cookie file is stored in the temp directory of the website.
$ Cookie_file = tempnam ('./temp', 'cooker ');

$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file );
Curl_exec ($ ch );
Curl_close ($ ch );

// Take the key cookie file to simulate post with the cookie file. fid is the topic ID of the Forum.
$ Send_url = $ discuz_url. "post. php? Action = newthread & fid = 2 ";


$ Ch = curl_init ($ send_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );

// The hash code here is not the same as the hash code in the login window. The hidden here has an id attribute.
Preg_match ('/<inputs * type = "hidden" s * name = "formhash" s * id = "formhash" s * value = "(.*?) "S */>/I ', $ contents, $ matches );
If (! Empty ($ matches )){
$ Formhash = $ matches [1];
} Else {
Die ('not found the forumhash .');
}


$ Post_data = array ();
// Post title
$ Post_data ['subobject'] = 'test2 ';
// Post content
$ Post_data ['message'] = 'test2 ';
$ Post_data ['topicsubmit '] = "yes ";
$ Post_data ['Extra '] = '';
// Post tag
$ Post_data ['tags'] = 'test ';
// The Post hash code, which is critical! If this hash code is missing, discuz will warn you of incorrect webpage
$ Post_data ['formhash'] = $ formhash;


$ Ch = curl_init ($ send_url );
Curl_setopt ($ ch, CURLOPT_REFERER, $ send_url); // disguise REFERER
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );

// Clear cookie files
Unlink ($ cookie_file );
?>

CURL enables simulated website login

The code is as follows: Copy code

<? Php $ cookie_file = tempnam ('./temp', 'cooke'); $ login_url = '/bbs/logging. php? Action = login & amp; loginsubmit = yes '; $ post_fields = 'username = user name & password = user password & referer = index. php & formhash = 24eca8af & loginfield = username & questionid = 0 & loginsubmit = logon '; $ ch = curl_init ($ login_url); curl_setopt ($ ch, CURLOPT_HEADER, 0 ); curl_setopt ($ ch, batch, 1); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields); curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); curl_exe C ($ ch); curl_close ($ ch); $ url = '/bbs'; $ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_HEADER, 0 ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file); $ contents = curl_exec ($ ch); echo $ contents; curl_close ($ ch);?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.