PHP如何使用curl模拟登陆?
PHP如何使用curl模拟登陆?
$ch = curl_init();$url = " $data = array( 'mail' => *** 'password' => *** ); foreach ($data as $key => $value){ $postfields .= urlencode($key) . '=' . urlencode($value) . '&'; }$postfields = rtrim($postfields, '&');$headers = array( 'Accept:*/*', 'Accept-Encoding:gzip, deflate', 'Accept-Language:zh-CN,zh;q=0.8', 'Connection:keep-alive', 'Content-Length:49', 'Content-Type:application/x-www-form-urlencoded; 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:XMLHttpRequest', ); 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);
返回结果说我用户或密码错误? 账号密码是正确的。
解析
这个问题问的非常好,但可惜的是大家的回复都是纸上谈兵未经探讨,最前最高票的回答的竟然说让下抓包工具,简直可笑啊,chrome下F12直接就可以看到账号密码是明文发送的何必还要抓包?另外的题主的http头就是从chrome下复制的。
根据竟然我判断你的问题的原因是发送了过多的http头,其中Content-Length是明显有问题的,这个代表内容长度,你这次抓包是49,但下次换个账号密码可就真不一定了。比如,如果账号密码过长,可能就会导致截断,那么无论如何都会提示密码错误的(因为只发送了一部分的密码过去)。
方案
事实上为了探究这个有意思的问题,我专门动手做一个有意思的实验。
这里就用个最简单的脚本语言node.js中的ajax模型来重新构建操作过程。
分析
我们先去登陆页--源码页去大致看一下,其中
<script crossorigin src="https://dfnjy7g2qaazm.cloudfront.net/v-575e20ec/user/script/login.min.js"></script>
这个跨域请求加载js脚本,看名字应该是和登陆有关的,我们这边使用尝试访问下,结果不用想,一篇乱糟糟的。
根据命名规范,我们猜测压缩前的名字可能就是叫login.js,我们看下他删除了没有,我们尝试访问dfnjy7g2qaazm.cloudfront.net/v-575e20ec/user/script/login.js,嗯哼还在,那好我们往下看下这里:
$("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; });
代码非常简单,我们知道了请求结果中status为0时代表登陆成功,同时我们也知道了后台执行登陆请求页是/api/user/login,即https://segmentfault.com/api/user/login,我们访问一下,嗯404。这说明了服务端验证了输入,并判断我们的请求不符合正常逻辑。下面我们开始伪造请求头。
请求头
我们用类似chrome的现代化浏览器,正常访问segmentfault.com/user/login,按下F12,选择network面板开始监控请求,然后我们随意填写账号密码,点击登陆。
这个时候下面会有一条信息,我们提取其中的Request Header如下
POST /api/user/login?_=93e1b923149fb56c4fd329fe95ea4001 HTTP/1.1 Host: segmentfault.com Connection: keep-alive Content-Length: 46 Pragma: no-cache Cache-Control: no-cache Accept: */* Origin: https://segmentfault.com X-Requested-With: XMLHttpRequest User-Agent: xxxx Content-Type: application/x-www-form-urlencoded; charset=UTF-8 DNT: 1 Referer: https://segmentfault.com/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4 Cookie: PHPSESSID=web5~to8l5ovmt9t3jkb84aevuqf151; Hm_lvt_e23800c454aa573c0ccb16b52665ac26=1465799317; Hm_lpvt_e23800c454aa573c0ccb16b52665ac26=1465799317; _ga=GA1.2.915515414.1465799317; _gat=1
我们只需要同样发送这些请求到服务器上,理论上就不会有问题,同时也不会再404了。
这里面的数据中,有些不需要发的,有些是必须要发送的。
我们可以一一测试下。
调试
我们这里使用nodejs来简单写段代码测试下服务端所验证的参数。
枯燥的测试就是不断删减请求来看看服务端会不会返回404.
过程不再赘述,结果是:
querystring中的 _必须和Cookie中的PHPSESSID对应。
X-Requested-With的值需要带ajax请求标志,即XMLHttpRequest
Referer的值
看来他们服务端还是蛮严格的。
源码
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)); } });

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
