所以我有hostgator來(lái)主持domain.com和hostinger來(lái)主持test.com。在這些網(wǎng)站中,我運(yùn)行著完全相同的腳本。該腳本的作用是,它登錄到外部網(wǎng)站example.com,獲取登錄 cookie 并存儲(chǔ)它。這個(gè)餅干可以用幾個(gè)月!然后轉(zhuǎn)到example.com/need-to-be-logged-in-to-view-this-page并獲取其內(nèi)容。但是,我面臨一個(gè)問(wèn)題。我在兩家托管公司上都有完全相同的代碼。它在hostgator上完美運(yùn)行。但是,在托管商上,它登錄并獲取 cookie,但 cookie 僅在登錄時(shí)起作用一次。然后我在 hostgator 上生成了 cookie 并將該 cookie 復(fù)制到托管商,它運(yùn)行良好。所以我想知道,cURL 中是否有某種設(shè)置或主機(jī)可能已啟用而主機(jī)可能未啟用的某些默認(rèn)設(shè)置,這使得主機(jī)上生成的 cookie 僅在登錄時(shí)持續(xù)那一秒鐘。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);}所以我嘗試$headerLiner從get_login_cookie()函數(shù)打印。它們非常不同。Hostinger 和 hostgator 如何使用完全相同的代碼沒(méi)有相同的響應(yīng)。當(dāng)我打印出來(lái)時(shí)在主機(jī)上 $headerLinerHTTP/2 201 server: nginx/1.15.6date: Sun, 21 Jul 2019 23:59:04 GMTcontent-type: application/json; charset=UTF-8content-length: 479set-cookie: coolCookie=THIS_IS_THE_COOKIE; Domain=.example.com; Path=/; Expires=Sun, 28-Jul-19 23:59:04 UTC; HttpOnlyexpires: Thu, 19 Nov 1981 08:52:00 GMTcache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0pragma: no-cachevary: Accept,Cookie,Authorization,X-EXAMPLE-Saucelocation: https://api.example.com/login/8gybiuf8gybundfguino
在 cURL 中發(fā)送 post 請(qǐng)求以獲取 cookie 在一個(gè)網(wǎng)絡(luò)托管上有效
料青山看我應(yīng)如是
2021-10-22 14:17:45