简体   繁体   English

PHP使用Cookie登录

[英]PHP Using cookies to log in

I am trying to log into a page through curl. 我正在尝试通过curl登录页面。 Where a successful login redirects you to the actual site and you see the content there. 成功登录后,您会将您重定向到实际站点,并在那里看到内容。

Basically, there is are 2 urls, the first url is to post the login credentials to and the other url is where the content is visible after the login. 基本上,有2个URL,第一个URL是将登录凭据发布到的,而另一个URL是登录后可见内容的位置。

I managed to send a post request to the login url and it successfully creates a valid cookie too but I can't figure out how to use the cookie to see the content of the page from the second url. 我设法将登录请求发送到登录URL,它也成功创建了一个有效的cookie,但是我不知道如何使用cookie从第二个URL中查看页面的内容。

I am trying to do a normal curl request (without the POSTFIELDS in the code) with these two options to retrieve the content of page 2 but if you view the source for it, it just displays the html code to redirect to the login url. 我正在尝试使用这两个选项进行普通的curl请求(代码中没有POSTFIELDS )来检索第2页的内容,但是如果您查看它的源代码,它只会显示html代码以重定向到登录url。

curl_setopt($ch1, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch1, CURLOPT_COOKIEFILE, 'cookie.txt');

Any ideas on what I might be doing wrong? 关于我可能做错了什么的任何想法?

Try to add more parameters to your CURL request : 尝试向您的CURL请求添加更多参数:

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$string = curl_exec ($ch);  
curl_close ($ch);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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