Search code examples
phpcurlcurl-multiredirectwithcookies

Simulating a cookie-enabled browser in PHP


How can I open a web-page and receive its cookies using PHP?

The motivation: I am trying to use feed43 to create an RSS feed from the non-RSS-enabled HighLearn website (remote learning website). I found the web-page that contains the feed contents I need to parse, however, it requires to login first. Luckily, logging in can be done via a GET request so it's as easy as fopen()ing "http://highlearn.website/login_page.asp?userID=foo&password=bar" for example. But I still need to get the cookies generated when I logged in, pass the cookies to the real client (using setcookie() maybe?) and then redirect.


Solution

  • For a server-side HTTP client you should use the cURL module. It will allow you to persist cookies across multiple requests. It also does some other neat things like bundling requests (curl_multi) and transparently handling redirects.

    When it comes to returning a session to your user, I don't think this is possible. You would need to be able to overwrite the cookies of other domains. This would cause massive security issues, so no browser would implement it.