【问题标题】:Why PHP/Curl doesn't keep the cookie data for the session为什么 PHP/Curl 不保留会话的 cookie 数据
【发布时间】: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 指令,则每当我运行脚本时,最后一个值都会更改。

标签: php session curl cookies


【解决方案1】:

您的问题是您没有使用 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

【讨论】:

  • 它似乎没有什么不同。但是,看起来只要脚本正在运行(有或没有 COOKIESESSION),脚本就会使用正常行为(我刚刚添加了一个sleep() 指令并在此之后调用$page = curl_exec($ch); echo $page; ,新的响应肯定表明时间已经过去. 但我想要的是即使我停止脚本并重新运行它也能正常工作。
  • 其实你必须做一个检查,如果检查cookie文件是否存在。如果你再次运行你的脚本,你不应该创建一个新的 cookie。因为新 cookie = 新会话。
  • 正如我在原始问题上所说,评论 COOKIEJAR 指令(重新运行)不会使会话保持不变。我还尝试评论 COOKIEJAR 指令并将其替换为 COOKIESESSION 指令。到目前为止没有任何变化:如果脚本停止运行并且我再次运行它,它仍然会给我 3600 秒。添加if (!file_exists("cookies.txt")){} 也没有什么不同(即使我确实理解它的需要)。
猜你喜欢
  • 1970-01-01
  • 2017-04-11
  • 2013-10-18
  • 1970-01-01
  • 2015-06-24
  • 2012-02-26
  • 1970-01-01
  • 2011-10-11
  • 2014-03-24
相关资源
最近更新 更多