Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 312
Barry Kooij Avatar asked Oct 11 '25 18:10

Barry Kooij


2 Answers

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.

like image 65
Gung Foo Avatar answered Oct 14 '25 07:10

Gung Foo


The Google Analytics cookie is set by javasvascript and not in the response of the http request. It's impossible with HTTP_Request, it's impossible with PHP...

You need to have an javascript intepreter to get the cookie, or some headless browser (maybe Zombie.js?)

like image 38
Tobias P. Avatar answered Oct 14 '25 09:10

Tobias P.