2013-07-31 51 views
0

现状:PHP卷曲cookie_session和搬迁

当进入网站时,它设置会话cookie,并重定向到本身。

问题:

当使用curl与FOLLOW_RELOCATIONCOOKIE_SESSION,它不重定向后适用的cookie,并以最大的限度重定向错误达到结束。

代码:

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_COOKIESESSION, 1); 
curl_setopt($ch, CURLOPT_INTERFACE, $ip); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 

详细输出:

* About to connect() to www.thesite.org port 80 (#0) 
* Trying x.x.x.254... * Name 'x.x.29.61' family 2 resolved to 'x.x.29.61' family 2 
* Local port: 0 
* connected 
* Connected to www.thesite.org (x.x.x.254) port 80 (#0) 
> GET/HTTP/1.1 
Host: www.thesite.org 
Accept: */* 

< HTTP/1.1 302 Moved Temporarily 
< Date: Wed, 31 Jul 2013 15:14:05 GMT 
< Server: Apache 
< cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
< pragma: no-cache 
< expires: Mon, 17 Jul 1978 05:00:00 GMT 
< Last-Modified: Wed, 31 Jul 2013 15:14:05 GMT 
< Set-Cookie: JSESSIONID=0A2488FF811CCDAB6A6C95C12AA3F4F8.DB4B9335DF549E1FCF56CF5BD8; Path=/ 
< Location: http://www.thesite.org/ 
< Content-Length: 0 
< Vary: Accept-Encoding 
< Content-Type: text/html 
< 
* Connection #0 to host www.thesite.org left intact 
* Issue another request to this URL: 'http://www.thesite.org/' 
* Re-using existing connection! (#0) with host www.thesite.org 
* Connected to www.thesite.org (x.x.x.254) port 80 (#0) 
> GET/HTTP/1.1 
Host: www.thesite.org 
Accept: */* 

< HTTP/1.1 302 Moved Temporarily 
< Date: Wed, 31 Jul 2013 15:14:06 GMT 
< Server: Apache 
< cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
< pragma: no-cache 
< expires: Mon, 17 Jul 1978 05:00:00 GMT 
< Last-Modified: Wed, 31 Jul 2013 15:14:06 GMT 
< Set-Cookie: JSESSIONID=5A80785B002D8C37F73F798B75A090B4.47EF37A843F03A8253F26BC644; Path=/ 
< Location: http://www.thesite.org/ 
< Content-Length: 0 
< Vary: Accept-Encoding 
< Content-Type: text/html 
< 
* Connection #0 to host www.thesite.org left intact 
* Issue another request to this URL: 'http://www.thesite.org/' 
* Re-using existing connection! (#0) with host www.thesite.org 
* Connected to www.thesite.org (x.x.x.254) port 80 (#0) 
> GET/HTTP/1.1 
Host: www.thesite.org 
Accept: */* 

etc... 

的问题是:

我在做什么错?如何使它工作?当然,我可以连接两次并手动设置,但这不是我想要做的,这就是为什么我使用CURL而不是file_get_contents()

+1

你用什么代码来设置卷曲处理程序? –

+0

已编辑的问题,上面的代码。 –

回答

0

您的代码未显示您使用cookie文件在页面加载之间存储信息。

添加以下行,它应该更好地工作。

$cookie_file='cookies.txt'; //Change this to wherever you want. 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
+0

是的,我不想使用文件来存储cookie。没有办法做一个单一的请求没有存储任何东西? 'curl_setopt($ ch,CURL_COOKIEJAR,'/ dev/null');'没有帮助。 –

+0

哦,它应该是'CURLOPT_COOKIEJAR',现在它可以工作 –