【发布时间】:2014-09-11 18:38:40
【问题描述】:
我正在使用Webbots, Spiders, and Screen Scrapers, 2nd Edition 学习 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);
当我使用网络浏览器登录测试网站时,我收到消息“您的登录还可以再使用 3600 秒”,每当我重新加载页面时,我都会看到时间下降(这是完全正常的行为,因为服务器识别会话编号并处理与之相关的所有内容,包括之前的时间)。现在,当我运行示例代码(如上所列)时,每次运行脚本时,身份验证值都会不断变化。我的猜测是因为 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 指令。但这里是这样想的:即使我注释了阻止脚本更改 cookie 的那一行,脚本仍然从服务器获得“相同”的响应(登录在 3600 - 3599 秒内仍然有效)。这可以通过某种方式解决吗?
【问题讨论】:
-
它真的在cookies.txt里面写了什么吗?
-
@LauriOrgla 是的
www.WebbotsSpidersScreenScrapers.com FALSE /cookie_authentication/ FALSE 0 authenticate 1410458823。如果未注释 cookiejar 指令,则每当我运行脚本时,最后一个值都会更改。