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

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 Web page, and then fill in and then post out, although it looks very friendly, but since the simulation login, after the log on the thing is not necessarily a short time to complete, So this is subject to PHP's maximum execution time limit, and some operations may not have enough permissions.

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 to fill out the verification code To the Code.txt file, after 20 seconds of hibernation is completed, 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:

/** * Analog Login *//Initialize variable $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); Obtain a cookie and store $contents = curl_exec ($curl); Curl_close ($curl); echo "Cookie gets completed, retrieving verification code ... \ n";//Check out Captcha $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, 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 fetch verification code ... \ n"; $code = file_get_contents ("Code.txt"); echo "Verification code successfully removed: $code \ n"; echo "Preparing to impersonate login ... \ n"; $post = "USERNAME=MABEN&AMp;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 the package itself to determine if (Substr_count ($result, "login succeeded") {echo "Login succeeded \ n";} else{echo "Login failed \ n"; exit;//ok, start doing what you want to do ....

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