PHP uses curl to implement a method of simulating login with a Web site with Captcha _php tips

Source: Internet
Author: User
Tags curl time limit

Many of the online analog login procedures, mostly through the service program, such as Apache operation, get to the verification code displayed on the Web page, and then fill in the post out, so although it looks very friendly, but since the analog login, the things that do after login is not necessarily a short time to complete, So this is subject to PHP's maximum execution time limit, and some operations may have insufficient permissions.

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

/** * Analog Login///initialization variable $cookie _file = "Tmp.cookie";
$login _url = "http://xxx.com/logon.php";

$verify _code_url = "http://xxx.com/verifyCode.php";
echo "is 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);
Gets the cookie and stores the $contents = Curl_exec ($curl);

Curl_close ($curl);
echo "Cookie fetch complete, fetching captcha ... \ n";
Remove the 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 out, is sleeping, within 20 seconds, please fill in the Code.txt and save \ n";

Stop running for 20 seconds Sleep (20);
echo "Hibernate complete, start fetching captcha ... \ n";
$code = file_get_contents ("Code.txt");
echo "Verification code successfully removed: $code \ n"; ECHO is preparing the impersonationLogin ... \ 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 that you grabbed to get it. if (Substr_count ($result, "login succeeded") {echo "logon succeeded \ n";}
 else{echo "Login failed \ n";
Exit

 //ok, start doing what you want to do ...

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.