开发者

PHP cURL Location Change

I am using cURL to display the contents of a page behind a login system. I am able to successfully login and display the first page behind the login s开发者_JAVA百科ystem, but any subsequent pages are unable to be displayed.

My understanding of the problem is that cURL follows the headers that are provided after logging in. So, if the order is login.php -> home.php, and I want to go to account.php, I would need another header pointing to that page.

Is that correct? Can I use cURL to display the contents of other pages after logging in?


You probably need to save and transmit cookies. This can be done in PHP cURL like this:

$ch = curl_init();

// set your regular options
curl_setopt($ch, CURLOPT_URL, 'http://somedomain.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('user'=>'foo', 'pass'=>'bar'));

// set where cookies are saved
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');

// set where cookies are retrieved from when sent to the server
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');

// execute login
curl_exec($ch);

// do another request
curl_setopt($ch, CURLOPT_URL, 'http://somedomain.com/restricted_page.php');
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_exec($ch);


You need to handle cookies. They're needed for the session authentication.

Get the Cookie header from the first call and send it with all subsequent calls.


You must know how login pages (usually) work. When you log into a site the site will send you a session cookie that you will send again to the website everytime you request a new page. So if you want to emulate the login you must store cookies returned by the login and then include them in every other request to the site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜