Example of php CURL login with verification code

Source: Internet
Author: User
Tags curl sleep trim


Example

Read the remote verification code and load it to this page using the framework. Then manually enter the verification code.

 

The code is as follows: Copy code

<? Php
/**
* @ Author Chase _ something
* @ Version $ id
*/
Define ('script _ root', dirname (_ FILE __).'/');
$ Act = trim ($ _ REQUEST ['AC']);
Switch ($ act)
{
Case 'login ':
// Obtain the verification code
$ Code = trim ($ _ REQUEST ['code']);
       
// $ LoginParams is The post parameter when curl simulates logon
$ LoginParams ['AC'] = 'login ';
$ LoginParams ['password'] = '000000 ';
$ LoginParams ['username'] = 'wlpadmin ';
$ LoginParams ['vcode'] = $ code;
       
// $ CookieFile is the cookie file name saved when the verification code is loaded.
$ CookieFile = SCRIPT_ROOT. 'Cookie. tmp ';
       
// $ TargetUrl curl: the submitted destination address www.111cn.net
$ TargetUrl = 'http: // local.xx.com/admin/login.php ';
       
// Reset parameters
$ Content = curlLogin ($ targetUrl, $ cookieFile, $ loginParams );
Echo $ content;
Break;
Case 'authcode ':
// The image Type of the Content-Type verification code
Header ('content-Type: image/png ');
ShowAuthcode ('http: // local.xx.com/admin/vcode.php? R = 0.03192671708666017 ');
Exit;
Break;
}

/**
* Simulate logon
* @ Param string $ address to which the url is submitted
* @ Param string $ cookieFile: the cookie-saving file
* @ Param string $ parameter to be post when loginParams is submitted
* @ Return string $ content returned content
*/
Function curlLogin ($ url, $ cookieFile, $ loginParams)
{
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookieFile); // send Cookie at the same time
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ loginParams); // submit the query information
$ Content = curl_exec ($ ch );
Curl_close ($ ch );
Return $ content;
}

/**
* Load the image verification code of the target website
* @ Param string $ authcode_url address of the verification code of the target website
*/
Function showAuthcode ($ authcode_url)
{
$ CookieFile = SCRIPT_ROOT. 'Cookie. tmp ';
$ Ch = curl_init ($ authcode_url );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookieFile); // Save the returned cookie information to the file
Curl_exec ($ ch );
Curl_close ($ ch );
}
?>
<Iframe src = "? Act = authcode "style = 'width: 100px; height: 40px 'frameborder = 0> </iframe>
<Form>
<Input type = "hidden" name = "act" value = "login">
<Input type = "text" name = "code"/>
<Input type = "submit" name = "submit">
</Form>

Example

Token verification code, and then carry the verification code to log on.

 

The code is as follows: Copy code

// Initialize the variable
$ Cookie_file = "tmp. cookie ";
$ Login_url = "http://xxx.com/logon.php ";
$ Verify_code_url = "http://xxx.com/verifyCode.php ";

Echo "obtaining 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 and store the COOKIE
$ Contents = curl_exec ($ curl );
Curl_close ($ curl );

Echo "the COOKIE is obtained. The verification code... n is being obtained ";
// Retrieve the verification code www.111cn.net
$ 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 "the verification code has been obtained and is waiting for a sleep. Please enter the verification code in code.txt within 20 seconds and save n ";
// Stops running for 20 seconds
Sleep (20 );

Echo "sleep complete, start to get the verification code... n ";
$ Code = file_get_contents ("code.txt ");
Echo "the verification code is successfully retrieved: $ coden ";
Echo "preparing to simulate logon... 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 part is determined based on the data on the website obtained through the packet capture.
If (substr_count ($ result, "logon successful ")){
Echo "logon successful n ";
} Else {
Echo "logon failed n ";
Exit;
}

// La, 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.