PHP Tutorial Curl implements the method of simulating login with CAPTCHA website

Source: Internet
Author: User

Many of the online simulation login programs, mostly through the service program Apache and other operations, get to the verification code displayed on the page, and then fill in and then post out

This article provides a program example, the idea is to obtain a verification code after the verification code stored as a picture, and then the program sleeps 20 seconds, after 20 seconds by the user manually view the picture, and the verification code to fill in the Code.txt file, after 20 seconds of hibernation, the program will read the Code.txt verification code, so that with the verification code for the login operation. The specific code is as follows:

/**

* Demo Login

*/

Initialize variables

$cookie _file = "Tmp.cookie";

$login _url = "http://xxx.com/logon.php";

$verify _code_url = "http://xxx.com/verifyCode.php";

echo "Acquiring cookie...\n";

$CURLJ = Curl_init ();

$timeout = 5;

curl_setopt ($curl, Curlopt_url, $login _url);

curl_setopt ($curl, Curlopt_returntransfer, 1);

curl_setopt ($curl, Curlopt_connecttimeout, $timeout);

curl_setopt ($curl, Curlopt_cookiejar, $cookie _file); Get Cookies and store

$contents = curl_exec ($curl);

Curl_close ($curl);

echo "Cookie is complete, retrieving verification code ... \ n";

Remove Verification Code

$curl = Curl_init ();

curl_setopt ($curl, Curlopt_url, $verify _code_url);

curl_setopt ($curl, Curlopt_cookiefile, $cookie _file);

curl_setopt ($curl, Curlopt_header, 0);

curl_setopt ($curl, Curlopt_returntransfer, 1);

$img = curl_exec ($curl);

Curl_close ($curl);

$fp = fopen ("Verifycode.jpg", "w");

Fwrite ($fp, $img);

Fclose ($FP);

echo "Verification code check out completed, is sleeping, 20 seconds please fill in the Verification code code.txt and save \ n";

Stop running for 20 seconds

Sleep (20);

echo "Hibernate complete, start to fetch verification code ... \ n";

$code = file_get_contents ("Code.txt");

echo "Verification code successfully removed: $code \ n";

echo "Preparing to impersonate a login ... \ n";

$post = "Username=maben&pwd=hahahaha&verifycode= $code";

$curl = Curl_init ();

curl_setopt ($curl, Curlopt_url, $url);

curl_setopt ($curl, Curlopt_header, false);

curl_setopt ($curl, curlopt_returntransfer,1);

curl_setopt ($curl, Curlopt_postfields, $post);

curl_setopt ($curl, Curlopt_cookiefile, $cookie _file);

$result =curl_exec ($curl);

Curl_close ($curl);

This piece is based on the data on the Web site captured by your own bag to make judgments.

if (Substr_count ($result, "login Successful")) {

echo "Login succeeded \ n";

}else{

echo "Login failed \ n";

Exit

}

OK, start doing what you want to do .....



PHP Tutorial Curl implements the method of simulating login with CAPTCHA website

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.