How does PHP simulate landing using curl?

Source: Internet
Author: User
How does PHP simulate landing using curl?

$ch = Curl_init (), $url = "$data = Array (' Mail ' = * * * * * * * * * * * * *); foreach ($data as $key + = $value {$postfields. = UrlEncode ($key). '=' . UrlEncode ($value). ' & ';} $postfields = RTrim ($postfields, ' & '); $headers = Array (' accept:*/* ', ' accept-encoding:gzip, deflate ', ' accep t-language:zh-cn,zh;q=0.8 ', ' connection:keep-alive ', ' content-length:49 ', ' Content-type:application/x-www-form-ur lencoded; Charset=utf-8 ', ' cookie:mp_18fe57584af9659dea732cf41c1c0416_mixpanel=%7b%22distinct_id%22%3a%20% 22153c6c3ec0c91-04fd9c038-12771e2d-1fa400-153c6c3ec0d18a%22%2c%22%24 initial_referrer%22%3a%20%22%24direct%22%  2c%22%24initial_referring_domain%22%3a%20%22%24direct%22%7d;  Phpsessid=web2~dom8lkdgosec57oljs98g2m8k0;  _gat=1;  hm_lvt_e23800c454aa573c0ccb16b52665ac26=1463986883,1464937399,1465290769,1465713371;  hm_lpvt_e23800c454aa573c0ccb16b52665ac26=1465717067; _ga=ga1.2.1469164019.1455850659 ', ' Host:segmentfault.com ', ' origin:https://segmEntfault.com ', ' referer:https://segmentfault.com/', ' user-agent:mozilla/5.0 (X11; Linux i686 (x86_64)) applewebkit/537.36 (khtml, like Gecko) chrome/48.0.2564.97 safari/537.36 ', ' x-requested-with:xml HttpRequest ', curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_ Returntransfer, True); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_postfields, $ Postfields) curl_setopt ($ch, Curlopt_encoding, ""); $result = Curl_exec ($ch); Curl_close ($ch); Var_dump ($result);

Returned the result saying that my user or password was wrong? The account password is correct.

Analytical

This question is very good, but it is a pity that everyone's reply is not discussed, the most up-to-the-top ticket to say let the next grab bag tool, it is ridiculous ah, chrome F12 directly can see the account password is clear text sent why still want to grab bag? The other main HTTP headers are copied from chrome.
According to the reason I judge your problem is sent too many HTTP headers, which content-length is obviously problematic, this represents the length of the content, you capture the package is 49, but the next time to change the account password can be really not necessarily. For example, if the account password is too long, it may cause truncation, then in any case will prompt the password is wrong (because only a portion of the password is sent past).

Scheme

In fact, in order to explore this interesting problem, I am doing an interesting experiment.
Here is the simplest scripting language of the AJAX model in node. JS to reconstruct the operation.

Analysis

Let's go to the landing page-the source page to look roughly, where

<script crossorigin src= "Https://dfnjy7g2qaazm.cloudfront.net/v-575e20ec/user/script/login.min.js" ></ Script>


This cross-domain request loading JS script, to see the name should be related to the landing, we use this side to try to access, the results do not want to think, a messy.
According to the naming convention, we guess that the name before compression may be called login.js, we look at him removed no, we try to access dfnjy7g2qaazm.cloudfront.net/v-575e20ec/user/script/ Login.js, hmm, we're still here, so let's look down here:

$ ("form[action= '/api/user/login ')"). Submit (function () {  var data, url;  url = '/api/user/login ';  Data = $ (this). Serialize ();  $.post (URL, data, function (d) {    if (!d.status) {      return location.href = D.data;    }  });  return false;});

The code is very simple, we know the request results in the status of 0 o'clock for login success, and we also know that the background execution Landing request page is/api/user/login, that is, Https://segmentfault.com/api/user/login, Let's visit, uh, 404. This shows that the server validates the input and determines that our request does not conform to the normal logic. Below we start to forge the request header.

Request Header

We use chrome-like modern browser, normal access to Segmentfault.com/user/login, press F12, select the network panel to start monitoring requests, and then we feel free to fill in the account password, click Login.
This time there will be a message, we extracted the request header as follows

post/api/user/login?_=93e1b923149fb56c4fd329fe95ea4001 http/1.1host:segmentfault.comconnection: Keep-alivecontent-length:46pragma:no-cachecache-control:no-cacheaccept: */*origin:https:// segmentfault.comx-requested-with:xmlhttprequestuser-agent:xxxxcontent-type:application/x-www-form-urlencoded; Charset=utf-8dnt:1referer:https://segmentfault.com/accept-encoding:gzip, deflate, braccept-language:en-us,en;q= 0.8,zh-cn;q=0.6,zh;q=0.4cookie:phpsessid=web5~to8l5ovmt9t3jkb84aevuqf151; hm_lvt_e23800c454aa573c0ccb16b52665ac26=1465799317; hm_lpvt_e23800c454aa573c0ccb16b52665ac26=1465799317; _ga=ga1.2.915515414.1465799317; _gat=1

We only need to send these requests to the server, theoretically there is no problem, and no more 404.
In this data, some do not need to send, some must be sent.
We can test each of them.

Debugging

Here we use Nodejs to simply write the parameters verified by the service side of the section Code test.
The tedious test is to constantly prune requests to see if the server will return 404.
The process does not repeat, the result is:

The _ in querystring must correspond to the PHPSESSID in the cookie.

The value of X-requested-with requires an AJAX request flag, which is XMLHttpRequest

The value of the Referer

It seems that their service is still quite strict.

Source

var superagent = require (' superagent '); Superagent.post (' https://segmentfault.com/api/user/login?_= 7ef046ad4f224034d7b51655238bd870 ')    . Set (' Referer ', ' Https://segmentfault.com/user/login ')    . Set (' X-requested-with ', ' XMLHttpRequest ')    . Set (' Cookie ', ' phpsessid=web1~395mahoqliohh5kclv894ibpr3; _gat=1;     _ga=ga1.2.1234754628.1465797373; hm_lvt_e23800c454aa573c0ccb16b52665ac26=1465797373;     hm_lpvt_e23800c454aa573c0ccb16b52665ac26=1465797538 ')    . Send ({        mail: "xxxxxx",        Password: "xxxx"    } )    . Type (' form ')    . End (function (err, res) {        if (err | |!res.ok) {            console.log (err.status);        } else {            console.log (' Yay got ' + json.stringify (res.body));        }    });
  • 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.