Search code examples
phpjavascriptcookies

Get cookies of external website with PHP


I'm trying to get all cookies of a website into an array in PHP. The goal of the script is to enter a website url and let the script check what cookies are set.

I've tried using HTTP_Request like this:

$req = new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->sendRequest();
$cookies = $req->getResponseCookies();

But this is only returning the server-side set cookies (like a session ID). I would also like to recieve cookies that are set with javascript, like the Google Analytics cookies.

I know this is possible because the website: http://www.cookie-checker.com/ is able to do so. To be sure the "cookie-checker" website is really parsing javascript set cookies and not just parsing known javascript source url, I've scanned a test website which wrote a dummy javascript cookie. They detected this cookie succesfully (incl. name, value and expiration date).

Any help would be very much appreciated!

Thanks in advance.

Barry


Solution

  • There is no way for a website to access cookies set by another site.

    Browsers (have to) make sure this cannot happen.

    What the site you mentioned does is make a request to the server and parse (also the javascript) its content.

    What you will need to do is make sure that your script parses all the javascript and keeps track of cookies set using it.