【发布时间】:2019-11-29 22:40:08
【问题描述】:
所以我有 hostgator 来托管 domain.com 和托管商来托管 test.com。在这些网站中,我运行着完全相同的脚本。该脚本所做的是,它登录到外部网站example.com,获取登录cookie 并将其存储。这个饼干可以用几个月!然后转到example.com/need-to-be-logged-in-to-view-this-page 并获取其内容。
但是,我遇到了一个问题。我在两家托管公司都有完全相同的代码。它在 hostgator 上完美运行。但是,在托管服务器上,它会登录并获取 cookie,但 cookie 仅在登录时有效。
然后我在 hostgator 上生成了 cookie 并将该 cookie 复制到托管程序,它运行良好。 所以我想知道,cURL 中是否有某种设置或一些默认设置,hostgator 可能已启用而托管商可能未启用,这使得托管商上生成的 cookie 在登录时仅持续一秒钟。
function get_login_cookie($ch, $headerLine) {
if (strncmp($headerLine, "set-cookie: coolCookie=", 20) == 0) {
$endPos = strpos($headerLine, ";");
$coolCookie = substr($headerLine, 20, ($endPos - 20));
file_put_contents("cookie.txt", $coolCookie);
}
return strlen($headerLine);
}
function login($username, $password) {
$fields = [
"username" => $username,
"password" => $password
];
$fields_string = http_build_query($fields);
$ch = curl_init("https://example.com/login");
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "get_login_cookie");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
}
所以我尝试从get_login_cookie() 函数打印$headerLiner。它们非常不同。 Hostinger 和 hostgator 为何没有完全相同的代码的相同响应。
当我打印出$headerLiner时在hostgator上
HTTP/2 201
server: nginx/1.15.6
date: Sun, 21 Jul 2019 23:59:04 GMT
content-type: application/json; charset=UTF-8
content-length: 479
set-cookie: coolCookie=THIS_IS_THE_COOKIE; Domain=.example.com; Path=/; Expires=Sun, 28-Jul-19 23:59:04 UTC; HttpOnly
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
pragma: no-cache
vary: Accept,Cookie,Authorization,X-EXAMPLE-Sauce
location: https://api.example.com/login/8gybiuf8gybundfguino
p3p: CP='TST'
set-cookie: browser_session=browser_gfdhfdguy46ytyrty/oMnIg; Domain=.example.com; Path=/; Expires=Sun, 21-Jul-69 23:59:04 UTC; HttpOnly
set-cookie: window_session=window_45gh56yjhttrghf4; Domain=.example.com; Path=/; HttpOnly
set-cookie: coolCookie=THIS_IS_THE_COOKIE; Domain=.example.com; Path=/; Expires=Sun, 28-Jul-19 23:59:04 UTC; HttpOnly
x-example-rnd: p3456ythth4567yt
当我在主机上打印$headerLine 时。 Hostinger 的回复缺失HTTP/2 201、browser session、window session 等
server: nginx/1.15.6
date: Sun, 21 Jul 2019 23:46:41 GMT
content-type: application/json; charset=UTF-8
content-length: 180
set-cookie: coolCookie=THIS_IS_THE_COOKIE; expires=Sun, 28-Jul-2019 23:46:41 GMT; Max-Age=604800; path=/; domain=.example.com; HttpOnly
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
pragma: no-cache
set-cookie: coolCookie=THIS_IS_THE_COOKIE; expires=Sun, 28-Jul-2019 23:46:41 GMT; Max-Age=604800; path=/; domain=.example.com; HttpOnly
vary: Accept,Cookie,Authorization,X-EXAMPLE-Sauce
x-example-rnd: OD4wQjIDT5ug0C
【问题讨论】:
-
这可能是由于 curl/php 版本。您可以将发布 URL 设置为 requestbin 并在此处发布结果吗? requestbin.com
-
@atymic 我检查了 curl 版本。 Hostgator 有 7.19.x,Hostinger 有 7.62.0。 Hostinger 有一个新版本,所以该脚本不应该也适用于 Hostinger 吗?
-
你能把每个服务器的请求的requestbin贴出来吗?这样我们就可以检查发送的请求有什么不同。
-
@atymic 不太确定如何使用它
-
将您的代码调用的 URL 更新为
https://en2ntkal5pml6.x.pipedream.net(requestbin),然后在两台服务器上触发您的代码。
标签: php curl post cookies http-headers