PHP uses curl to implement a mock login to a website with a captcha, Curl captcha _php Tutorial

Source: Internet
Author: User

PHP uses curl to perform a mock login to a website with a verification code, curl Verification Code


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

/** * Impersonation 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";//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 out and sleeping, please fill in the Verification code 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 to impersonate 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 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 CURL Post Simulation User login hopefully can give specific code and explain the code briefly

You can see if there are other HTTP headers without impersonation, such as whether Referer and user-agent can emulate the value of the browser, and a complete request is similar to this:
Get/home/pack/data/content?id=31,2399,13,30&asyn=1&t=0.03439752989200834&_req_seqid= 0xa982225f0637c78a http/1.1
Accept: */*
Accept-language:zh-cn
referer:www.baidu.com/
X-requested-with:xmlhttprequest
Accept-encoding:gzip, deflate
user-agent:mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0; BTRS123401; infopath.2;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729;. net4.0c; MS-RTC LM 8)
Host:www.baidu.com
Connection:keep-alive
Cookie:xcxxxxx

How does curl implement analog login PHP

$url = "localhost/post_output.php";
$post _data = Array (
"foo" = "Bar",
"Query" = "Nettuts",
"Action" = "Submit"
);
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
We're in post data Oh!
curl_setopt ($ch, Curlopt_post, 1);
Add the post variable to the
curl_setopt ($ch, Curlopt_postfields, $post _data);
$output = curl_exec ($ch);
Curl_close ($ch);
Echo $output;
Results




http://www.bkjia.com/PHPjc/846186.html www.bkjia.com true http://www.bkjia.com/PHPjc/846186.html techarticle PHP Using Curl implementation of the site with a verification code to simulate the method of login, Curl verification code on the network many of the simulation login programs, mostly through the service program Apache and other operations, get ...

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