Curl Submit login information encountered verification code problem

Source: Internet
Author: User
Keywords Php
Verification code, is currently found URL, on the front page directly displayed, and then manually entered.
Submitting the login information on the Web page with Ajax can be returned correctly.
I will be the same combination of good information, into the background php file, with curl submitted, but each time the verification code error.
It feels like it was submitted in PHP and refreshed a verification code, how can this need to solve?

Borrowed ty0716 code to try it out, there are some problems, do not understand how to deal with.
Directly take the verification code address, no cookie is saved. Use the address of the landing page, there is no way to take verification code to identify.

Define (' Script_root ', DirName (__file__). '    /'), include "code.php", Function Send ($LOGINURL, $cookieFile) {//$name = $_post[' name '];    $idcard = $_post[' Idcard ');    $PW = $_post[' pw '];    $code = $_post[' code ');    $code = GetCode (' http://www.xxx.com/Num.jsp ');    Header ("Content-type:text/html;charset=utf-8");    $post _data = "". $code; Return post ($LOGINURL, $post _data, $cookieFile);}    function GetCode ($CAPTCHAURL, $cookieFile) {$captchaString = Get ($CAPTCHAURL, $cookieFile); $tempCaptchaFile = Script_root. Date (' Ymdhis ').    '. jpg ';    File_put_contents ($tempCaptchaFile, $captchaString);    $OCR = new Code ($TEMPCAPTCHAFILE);    $captcha = $OCR->getcode ();    Echo $captcha; return $captcha;}    function Get ($url, $cookie _file, $isCookiesSave = False) {//Initialize $curl = Curl_init ($url);    $header = Array (); $header [] = ' user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.90 safari/53    7.36 '; curl_setopt ($curl, CUrlopt_httpheader, $header);    Do not output header header information curl_setopt ($curl, Curlopt_header, 0);    if ($isCookiesSave) {curl_setopt ($curl, Curlopt_cookiejar, $cookie _file);    } else {curl_setopt ($curl, Curlopt_cookiefile, $cookie _file);    }//Save to string instead of output curl_setopt ($curl, Curlopt_returntransfer, 1);    Whether to crawl the page after the jump curl_setopt ($curl, curlopt_followlocation, 1);    $info = curl_exec ($curl);    Curl_close ($curl); return $info;}    Function post ($url, $data, $cookie _file) {//Initialize $curl = Curl_init ($url);    $header = Array (); $header [0] = ' user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.90 SAFARI/5    37.36 ';    curl_setopt ($curl, Curlopt_httpheader, $header);    Do not output header header information curl_setopt ($curl, Curlopt_header, 0);    Save to string instead of output curl_setopt ($curl, Curlopt_returntransfer, 1);    curl_setopt ($curl, Curlopt_cookiefile, $cookie _file);    POST Data curl_setopt ($curl, Curlopt_post, 1); Request Data Curl_setopt ($curl, Curlopt_postfields, $data);    Whether to crawl the page after the jump curl_setopt ($curl, curlopt_followlocation, 1);    $response = curl_exec ($curl);    Curl_close ($curl); return $response;}

Reply content:

Verification code, is currently found URL, on the front page directly displayed, and then manually entered.
Submitting the login information on the Web page with Ajax can be returned correctly.
I will be the same combination of good information, into the background php file, with curl submitted, but each time the verification code error.
It feels like it was submitted in PHP and refreshed a verification code, how can this need to solve?

Borrowed ty0716 code to try it out, there are some problems, do not understand how to deal with.
Directly take the verification code address, no cookie is saved. Use the address of the landing page, there is no way to take verification code to identify.

Define (' Script_root ', DirName (__file__). '    /'), include "code.php", Function Send ($LOGINURL, $cookieFile) {//$name = $_post[' name '];    $idcard = $_post[' Idcard ');    $PW = $_post[' pw '];    $code = $_post[' code ');    $code = GetCode (' http://www.xxx.com/Num.jsp ');    Header ("Content-type:text/html;charset=utf-8");    $post _data = "". $code; Return post ($LOGINURL, $post _data, $cookieFile);}    function GetCode ($CAPTCHAURL, $cookieFile) {$captchaString = Get ($CAPTCHAURL, $cookieFile); $tempCaptchaFile = Script_root. Date (' Ymdhis ').    '. jpg ';    File_put_contents ($tempCaptchaFile, $captchaString);    $OCR = new Code ($TEMPCAPTCHAFILE);    $captcha = $OCR->getcode ();    Echo $captcha; return $captcha;}    function Get ($url, $cookie _file, $isCookiesSave = False) {//Initialize $curl = Curl_init ($url);    $header = Array (); $header [] = ' user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.90 safari/53    7.36 '; curl_setopt ($curl, CUrlopt_httpheader, $header);    Do not output header header information curl_setopt ($curl, Curlopt_header, 0);    if ($isCookiesSave) {curl_setopt ($curl, Curlopt_cookiejar, $cookie _file);    } else {curl_setopt ($curl, Curlopt_cookiefile, $cookie _file);    }//Save to string instead of output curl_setopt ($curl, Curlopt_returntransfer, 1);    Whether to crawl the page after the jump curl_setopt ($curl, curlopt_followlocation, 1);    $info = curl_exec ($curl);    Curl_close ($curl); return $info;}    Function post ($url, $data, $cookie _file) {//Initialize $curl = Curl_init ($url);    $header = Array (); $header [0] = ' user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.90 SAFARI/5    37.36 ';    curl_setopt ($curl, Curlopt_httpheader, $header);    Do not output header header information curl_setopt ($curl, Curlopt_header, 0);    Save to string instead of output curl_setopt ($curl, Curlopt_returntransfer, 1);    curl_setopt ($curl, Curlopt_cookiefile, $cookie _file);    POST Data curl_setopt ($curl, Curlopt_post, 1); Request Data Curl_setopt ($curl, Curlopt_postfields, $data);    Whether to crawl the page after the jump curl_setopt ($curl, curlopt_followlocation, 1);    $response = curl_exec ($curl);    Curl_close ($curl); return $response;}

When you first get the page, save the cookie and post it with your login information, verification code, and the cookie attached to it. If you post directly, your verification code will always correspond to the last verification code.

Looked under, such a verification code can do a verification code identification, refer to my article: https://segmentfault.com/a/1190000004361370

You need to make sure that the session_id of the post is consistent with the session_id of the Verification code, otherwise the captcha is wrong.

The verification code is output by your program,

After using PHP to get the verification code output, you go again to call either an expired verification code, or it is not your session_id verification code

Locate the URL of the verification Code, request a verification code at the time of submission, listen to recognition, or manually enter

  • Related Article

    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.