Ask a curl to log on to the website

Source: Internet
Author: User
Tags set cookie urlencode webp
Ask you the great God, I use Curl login a website When login failed, can trouble everybody pointing.

First of all, the application background:
Http://my.tri.co.id/login/init,
This site is a telephone inquiry inquiry website, used to query the current phone card traffic, is the Indonesian website.

I want to use PHP to do a login agent, automatic query and summary.

This site has a verification code when logging in, that is, the server through the session to establish the user and identification code one by one correspondence.

I used curl to implement the same raw data as the browser post, but I still failed the login.

Browser Raw stream:

721 Bytes sent to 180.214.234.59:80

Post/login/submit http/1.1
Host:my.tri.co.id
Connection:keep-alive
Cache-control:max-age=0
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin:http://my.tri.co.id
Upgrade-insecure-requests:1
user-agent:mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/47.0.2526.111 safari/537.36
content-type:application/x-www-form-urlencoded
Referer:http://my.tri.co.id/login/submit
Accept-encoding:gzip, deflate
accept-language:en-us,en;q=0.8
cookie:bigipserversc_frontend_pool=123211018.37151.0000; Jsessionid=bad8502fe89e3929e468fceb255dfa77
content-length:68

modelname=login&msisdn=089506850651&password=indonesia&checknum=6998

Curl simulates the raw stream:

721 Bytes sent to 180.214.234.59:80

Post/login/submit http/1.1
Host:my.tri.co.id
cookie:bigipserversc_frontend_pool=123211018.37407.0000; jsessionid=ee68c4c157664fb0e964ffb1a950968a
Connection:keep-alive
Cache-control:max-age=0
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin:http://my.tri.co.id
Upgrade-insecure-requests:1
user-agent:mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/47.0.2526.111 safari/537.36
content-type:application/x-www-form-urlencoded
Referer:http://my.tri.co.id/login/submit
Accept-encoding:gzip, deflate
accept-language:en-us,en;q=0.8
content-length:68

modelname=login&msisdn=089506850651&password=indonesia&checknum=4608


But the returned data is different, that is, my program returned to the page is not a successful landing page.

Browser return:

http/1.1 302 Moved temporarily
server:apache-coyote/1.1
set-cookie:jsessionid=5830130390f707d68d4e465557b9f054; path=/
Location:http://my.tri.co.id/
Content-language:en-us
Date:tue, 14:13:19 GMT
content-length:0


Curl returns:

http/1.1 OK
server:apache-coyote/1.1
Content-type:text/html;charset=utf-8
Content-language:en-us
Vary:accept-encoding
Date:tue, 16:09:49 GMT
content-length:5569


My program is the following implementation login.php:
==========================================================
 * @link http://hightman.cn * @copyright Copyright (c) Twomice Studio. *//Set cookie file $cookie _file = dirname (__file__). ' \cookie.txt '; Echo $cookie _file. "
"; $usr _form_name = ' usr '; $pwd _form_name = ' pwd '; $check _num_form_name = ' check_num '; $post _url = ' http://my.tri.co.id/login/submit '; $USR = $_post[$usr _form_name]; $pwd = $_post[$pwd _form_name]; $check _num = $_post[$check _num_form_name]; Echo $usr. " " . $pwd. "". $check _num; $tri _usr_form_name = ' msisdn '; $tri _pwd_form_name = ' password '; $tri _check_num_form_name = ' checknum '; $tri _mode_form_name = ' modelname '; $fields = Array ($tri _mode_form_name = urlencode (' login '), $tri _usr_form_name = Urlenco De ($USR), $tri _pwd_form_name = UrlEncode ($pwd), $tri _check_num_form_name = UrlEncode ($ Check_num)); Url-ify the data for the POST $fields _string = '; foreach ($fields as $key = + $value) {$fields _string. = $key. ' = '. $value. ' & '; The print $fields _string. "
"; $fields _string = substr ($fields _string, 0, strlen ($fields _string)-1); RTrim ($fields _string, "&"); Print $fields _string. "
"; Open connection $ch = Curl_init (); $header [] = "connection:keep-alive"; $header [] = "cache-control:max-age=0"; $header [] = "accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; $header [] = "origin:http://my.tri.co.id"; $header [] = "upgrade-insecure-requests:1"; $header [] = "user-agent:mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/47.0.2526.111 safari/537.36 "; $header [] = "content-type:application/x-www-form-urlencoded"; $header [] = "Referer:http://my.tri.co.id/login/submit"; $header [] = "accept-encoding:gzip, deflate"; $header [] = "accept-language:en-us,en;q=0.8"; curl_setopt ($ch, Curlopt_httpheader, $header); Set the URL, number of post VARs, post data curl_setopt ($ch, Curlopt_url, $post _url); curl_setopt ($ch, Curlopt_referer, ' http://my.tri.co.id '); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_customrequest, "POST"); curl_setopt ($ch, Curlopt_postfields, $fields_string); curl_setopt ($ch, Curlopt_ssl_verifypeer, false); curl_setopt ($ch, curlopt_ssl_verifyhost, 0); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_cookiejar, $cookie _file); curl_setopt ($ch, Curlopt_cookiefile, $cookie _file); Using the cookies obtained above curl_setopt ($ch, curlopt_followlocation, 1); Use automatic jump curl_setopt ($ch, Curlopt_encoding, ""); Execute post $result = curl_exec ($ch); if (! $result) {$error = Curl_error (); Echo $error. "
"; } else {print "Result length:". Strlen ($result). "
"; } $rinfo =curl_getinfo ($ch); Curl_close ($ch); Print $result;

========================================================

The idea here is that I first used (index.php, the rendering of the login interface) Curl to implement a cookie and a login ID image acquisition, and then submit it to my own login.php for processing. login.php the source code as shown above, complete the login.

But do not know what causes the failure.

Sincerely ask you, thank you!


Reply to discussion (solution)

Access the form page first, get cookie variables Jsessionid and Bigipserversc_frontend_pool
Re-access the checksum URL (with the cookie variable) to get the cookie variable
There is manual intervention, so cookies must be stored as files and prevent sharing conflicts (preferably with user ID as file name)
Submit form data (with check code) to the form target page (with cookie variable)

curl_setopt ($ch, Curlopt_postfields, $fields _string);
$fields _string can be an associative array and does not require you to bother assembling. If you must want to use a string, you can also use the Http_build_query function

Access the form page first, get cookie variables Jsessionid and Bigipserversc_frontend_pool
Re-access the checksum URL (with the cookie variable) to get the cookie variable
There is manual intervention, so cookies must be stored as files and prevent sharing conflicts (preferably with user ID as file name)
Submit form data (with check code) to the form target page (with cookie variable)

curl_setopt ($ch, Curlopt_postfields, $fields _string);
$fields _string can be an associative array and does not require you to bother assembling. If you must want to use a string, you can also use the Http_build_query function



The cookie has been taken in the process of the previous page. It is used in the login.php. I also looked at the cookie data, the session ID data is the same. In addition, from the raw stream data, all the data is the same.

But logging in is not right. I wonder what you think of this phenomenon?

Access the form page first, get cookie variables Jsessionid and Bigipserversc_frontend_pool
Re-access the checksum URL (with the cookie variable) to get the cookie variable
There is manual intervention, so cookies must be stored as files and prevent sharing conflicts (preferably with user ID as file name)
Submit form data (with check code) to the form target page (with cookie variable)

curl_setopt ($ch, Curlopt_postfields, $fields _string);
$fields _string can be an associative array and does not require you to bother assembling. If you must want to use a string, you can also use the Http_build_query function



My process is your idea.

  * @link http://hightman.cn * @copyright Copyright (c) Twomice Studio. */header (' content-type:text/html; Charset=utf-8 ');//Prevent the generated page garbled $title = ' Technology Co., Ltd. '; $temp _file = ' template.html '; $dest _ File = ' login.html ';//Set Cookie File$cookie_file = DirName (__file__). ' \cookie.txt '; Echo $cookie _file, php_eol;//$http->setcookiepath ($cookie _file);//Simple load response contents$ Link_header = ' http://my.tri.co.id '; $response _len = 0; $loop _times = 0; $login _url = $link _header. '/login/init ';//first obtain cookies and save $ch = Curl_init ($login _url); Initialize curl_setopt ($ch, Curlopt_header, 0); does not return header section curl_setopt ($ch, Curlopt_returntransfer, true); Returns a string rather than a direct output curl_setopt ($ch, Curlopt_cookiejar, $cookie _file); Storage cookiescurl_setopt ($ch, Curlopt_cookiefile, $cookie _file), $response = Curl_exec ($ch); Curl_close ($ch);//echo Number_format (strlen ($response)). ' bytes ', Php_eol;//echo $response; $img _link_header = '/verifycode;jsessionid '; $img _link_tail = '? sessionkey=logincn &null '; $header _pos = Strpos ($resPonse, $img _link_header); $tail _pos = Strpos ($response, $img _link_tail, $header _pos) + strlen ($img _link_tail);//echo ' Header pos: '. $header _pos, Php_eol;//echo ' tail pos: '. $tail _pos, php_eol; $img _link = $link _header. substr ($response, $header _pos, $tail _pos-$header _pos); $jsession _id_header = ' jsessionid= '; $session _key_header = '? SessionKey '; $session _id_pos = Strpos ($img _link, $jsession _id_header); $session _key_pos = Strpos ($img _link, $session _ Key_header); $session _id = substr ($img _link, $session _id_pos + strlen ($jsession _id_header), $session _key_pos-$session _id_pos-strlen ($jsession _id_header)); Echo $img _link, Php_eol;echo $session _id, php_eol; $fp = fopen ($temp _file, "R"); Read-only open template $str = Fread ($fp, FileSize ($temp _file));//read content in Template fclose ($fp); $str = Str_replace ("{penglig_site_title}", $ Title, $str);//replace content $str = Str_replace ("{Img_url}", $img _link, $STR);//Replace the contents of the Echo $str;


From the code can be seen, I also first open the first page, set a cookie, and get a link to the CAPTCHA image and display on their own page, manually identify and input, and then click on their own submission data post to login.php, that is, 1 floor code inside.

Then you can follow it and see what's wrong with that link.

Then you can follow it and see what's wrong with that link.



Can you tell me how to debug tracking? I've never done this before. Experience is 0.
Thank you
  • 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.