简体   繁体   English

为什么PHP / Curl不保留会话的Cookie数据

[英]Why PHP/Curl doesn't keep the cookie data for the session

I am learning PHP Curl with Webbots, Spiders, and Screen Scrapers, 2nd Edition . 我正在使用Webbots,Spiders和Screen Scrapers(第二版)学习PHP Curl。 The chapter about cookie authentication shows this code: 关于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);

when I get log in the test website using the web browser, I get the message "Your login is good for another 3600 seconds" and whenever I reload the page I see the time goes down (which is a completely normal behaviour since the server recognize the session number and handle everything that goes with it, including the time before). 当我使用Web浏览器登录测试网站时,会收到消息“您的登录时间又可以保持3600秒”,并且每当我重新加载页面时,我看到的时间就会减少(这是完全正常的行为,因为服务器可以识别会话号并处理所有相关事宜(包括之前的时间)。 Now when I run the example code (listed above), the authenticate value keeps changing every time I run the script. 现在,当我运行示例代码(上面列出)时,每次运行脚本时,authenticate值都会不断变化。 My guess is because of the curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 我的猜测是因为curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); instruction. 指令。 But here is the think: even if I comment that line which by the way prevent the script to change the cookie, The script still get the "same" response from the server (the login is still good for 3600 - 3599 secs). 但是这里有一个想法:即使我注释了这一行,但该行阻止了脚本更改cookie,该脚本仍会从服务器获得“相同”响应(登录仍然有效3600-3599秒)。 Can this be solved in some way? 能以某种方式解决吗?

Your problem, is that you are not using cookie sessions with curl. 您的问题是您没有使用带有curl的cookie会话。

Try setting cookie sessions like that: 尝试像这样设置cookie会话:

curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

Working example: 工作示例:

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));

Output: 输出:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM