cURL使用cookie做奇怪的事情

时间:2014-01-15 16:58:17

标签: php cookies curl

我正在尝试通过cURL管理外部网站上的帐户,但我使用的cURL代码确实很奇怪。我通过login.php登录并尝试通过account.php访问该帐户。

这是login.php中代码的一部分:

$cookieFile = $functions->random(10, 1); //function generates random file name
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://strona.com/login.php");
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($curl, CURLOPT_POSTFIELDS, "login=user&password=password");
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_HEADER, 1);        
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_COOKIEJAR,             $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt');
$getCookie = curl_exec($curl);
curl_close($curl);

$cookieContainer =         file_get_contents($_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt');
$cookieIdPos = strpos($cookieContainer, "JSESSIONID");
$setFinalCookie = "JSESSIONID=".substr($cookieContainer, ($cookieIdPos+11), (strlen($cookieContainer)-1));
$setExpireDate = (time() + 1500);

执行代码后,它会被正确创建和写入。这就是示例cookie的样子:

# Netscape HTTP Cookie File
# [url="http://curlm.haxx.se/rfc/cookie_spec.html"]http://curlm.haxx.se/rfc/cookie_spec.html[/url]
# This file was generated by libcurl! Edit at your own risk.

strona.com  FALSE   /   FALSE   1405347742  md5 1389795742074
strona.com  FALSE   /   FALSE   0   JSESSIONID  0F6BCB6D89F2DCD2B3B77AAA33AEA69A

登录时的示例标题如下:

HTTP/1.1 302 Found Date: Wed, 15 Jan 2014 14:37:09 GMT Server: Apache/2.2.14 (Ubuntu) Set-    Cookie: JSESSIONID=BDC17E49CAE16F876D22BE97C155E573; Path=/; HttpOnly Location: [url="http://strona.com/login.php;jsessionid=BDC17E49CAE16F876D22BE97C155E573"]http://strona.com/login.php;jsessionid=BDC...D22BE97C155E573[/url] Content-Language: en-US Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 20 Connection: close Content-Type: text/html HTTP/1.1 200 OK Date: Wed, 15 Jan 2014 14:37:09 GMT Server: Apache/2.2.14 (Ubuntu) Content-Language: en-US Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 21445 Connection: close Content-Type: text/html;charset=UTF-8

这一直在努力。现在,使用account.php代码:

$cookie = $functions->get_cookie(); //gets previously generated (not included in the code above) and saved cookie and cookie file name
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://strona.com/account.php");
curl_setopt($curl, CURLOPT_COOKIE, $cookie["cookie"]);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_COOKIEJAR,     $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookie["cookiefile"].'.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookie["cookiefile"].'.txt');
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$curlData = curl_exec ( $curl );
echo $curlData;

这应该显示正常的帐户页面,不应该吗?嗯,事实并非如此。网页回复“您尚未登录”并返回此Cookie:

# Netscape HTTP Cookie File
# [url="http://curlm.haxx.se/rfc/cookie_spec.html"]http://curlm.haxx.se/rfc/cookie_spec.html[/url]
# This file was generated by libcurl! Edit at your own risk.

strona.com  FALSE   /   FALSE   1405347742  md5 1389795742074

案例是:

  • 正确获取cookie,

  • 使用正确的chmod set

  • 将cookie正确保存在文件中
  • cookie正确保存在数据库中,

  • 正确地在$ cookie [“cookie”]中提取cookie,

然而,当用于访问要求我记录的页面时,它不起作用。

login.php成功登录我,account.php说我没有登录。

情况如何?

帮助,任何人!

1 个答案:

答案 0 :(得分:0)

从代码中删除这两行,以便它也可以从cookie文件加载会话Cookie。目前,它不允许您从cookie文件加载会话cookie。

curl_setopt($curl, CURLOPT_COOKIE, $cookie["cookie"]);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);