PHP uses curl to implement a mock login to a website with a CAPTCHA

Source: Internet
Author: User

* Demo Login

//Initialize Variables
$cookie _file ="Tmp.cookie";
$login _url ="";
$verify _code_url ="";

Echo"getting 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"the cookie gets completed and the verification code is being taken ... \ 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 is removed and is hibernating, please fill in code.txt and save \ n in 20 seconds";
//stop running for 20 seconds
Sleep -);

Echo"hibernate complete, start to fetch verification code ... \ n";
$code = file_get_contents ("Code.txt");
Echo"Verification code successfully removed: $code \ n";
Echo"Preparing for impersonation 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 successful \ n";
Echo"Login failed \ n";

PHP uses curl to implement a mock login to a website with a CAPTCHA

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: 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.