我正在使用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秒#34;每当我重新加载页面时,我都会看到时间停止(这是一个完全正常的行为,因为服务器识别会话号并处理随之而来的所有内容,包括之前的时间)。现在,当我运行示例代码(上面列出)时,每次运行脚本时,authenticate值都会不断变化。我的猜测是因为curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
指令。但是这里有一个想法:即使我评论那条顺便阻止脚本更改cookie的行,脚本仍然会得到"相同的"来自服务器的响应(登录仍然适用于3600 - 3599秒)。这可以通过某种方式解决吗?
答案 0 :(得分:0)
您的问题是,您没有使用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