[英]Why PHP/Curl doesn't keep the cookie data for the session
我正在使用Webbots,Spiders和Screen Scrapers(第二版)學習PHP Curl。 關於cookie身份驗證的章節顯示了以下代碼:
# Define target page
$target = "http://www.WebbotsSpidersScreenScrapers.com/cookie_authentication/index.php";
# Define the login form data
$form_data="enter=Enter&username=webbot&password=sp1der3";
# Create the PHP/CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target); // Define target site
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return page in string
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // Tell PHP/CURL where to write cookies
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); // Tell PHP/CURL which cookies to send
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
# Execute the PHP/CURL session and echo the downloaded page
$page = curl_exec($ch);
echo $page;
# Close the PHP/CURL session
curl_close($ch);
當我使用Web瀏覽器登錄測試網站時,會收到消息“您的登錄時間又可以保持3600秒”,並且每當我重新加載頁面時,我看到的時間就會減少(這是完全正常的行為,因為服務器可以識別會話號並處理所有相關事宜(包括之前的時間)。 現在,當我運行示例代碼(上面列出)時,每次運行腳本時,authenticate值都會不斷變化。 我的猜測是因為curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
指令。 但是這里有一個想法:即使我注釋了這一行,但該行阻止了腳本更改cookie,該腳本仍會從服務器獲得“相同”響應(登錄仍然有效3600-3599秒)。 能以某種方式解決嗎?
您的問題是您沒有使用帶有curl的cookie會話。
嘗試像這樣設置cookie會話:
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
工作示例:
function getSessionTime($answer) {
$parts = explode('<font color="red">', $answer);
$bottomPart = $parts[1];
$parts = explode('</font>', $bottomPart);
$result = $parts[0];
return $result;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.WebbotsSpidersScreenScrapers.com/cookie_authentication/index.php');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "enter=Enter&username=webbot&password=sp1der3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts
echo getSessionTime(curl_exec($ch));
sleep(1);
echo getSessionTime(curl_exec($ch));
sleep(1);
echo getSessionTime(curl_exec($ch));
sleep(1);
echo getSessionTime(curl_exec($ch));
sleep(1);
echo getSessionTime(curl_exec($ch));
sleep(1);
echo getSessionTime(curl_exec($ch));
輸出:
<br>
Your login is good for another 3600 seconds
<br>
Your login is good for another 3599 seconds
<br>
Your login is good for another 3597 seconds
<br>
Your login is good for another 3596 seconds
<br>
Your login is good for another 3595 seconds
<br>
Your login is good for another 3594 seconds
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.