-1

Does anyone knows how I could get a list of cookies for an external URL using php?

I think this could be done with cURL?

When i'm using cookiejar or using get_headers, I only see one cookie (the PHPSESSID) for example. But when you open chrome console (F12) and go to cookie storage, you see a much bigger list. Also the google analytics cookies for example. I want to be able to display that list of cookies. So also 3rd party cookies..

Is there any way to retrieve those cookies? Maybe store them temporary or something?

user1596031
  • 31
  • 1
  • 7

1 Answers1

0

If I got you right & you mean getting cookies that a page sets when visiting it using cURL,
You can use CURLOPT_COOKIEJAR option of cURL for auto manage cookies with cURL (curl_setopt):

$ch = curl_init($url);  
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$cont = curl_exec ($ch);  
curl_close ($ch);  

Or search response header for Set-Cookie: line:
how to get the cookies from a php curl into a variable

Community
  • 1
  • 1
Ariyan
  • 14,760
  • 31
  • 112
  • 175
  • Hi, thanks for your reply! When i'm using cookiejar or using get_headers, I only see one cookie (the PHPSESSID) for example. But when you open chrome console (F12) and go to cookie storage, you see a much bigger list. Also the google analytics cookies for example. I want to be able to display that list of cookies. So also 3rd party cookies..Is that possible? – user1596031 Aug 13 '12 at 18:39
  • 1
    @user1596031 If you are not getting the other cookies, it's probably because they are not created by the page you load, but by other resources. For example google analytics cookies (utma, utmz, ...) will be create by an invisible image. – Tchoupi Aug 13 '12 at 18:49
  • Thanks! That's a good answer :-) Is there any way to retrieve that kind of cookies with php (or javascript) or any other language? – user1596031 Aug 13 '12 at 18:53
  • I think something like this is what i need, but then programmatically: https://chrome.google.com/webstore/detail/lopabhfecdfhgogdbojmaicoicjekelh – user1596031 Aug 13 '12 at 20:17
  • @user1596031: you need to visit the page and get its response header to get a cookie; so for getting other cookies that like Google Analytics , ... you need get the related page and save those cookies too. this is code and not a browser so you should read all of `iframe`,pages that are loaded by javascript(e.g. Google Analytics), images , ... by yourself in your code. Only loading the main page will not get you the whole thing. for specific pages you can find all materials separately and write its fetch code. but for a generic code you should write something like a HTML/JS/CSS Renderer Engine! – Ariyan Aug 14 '12 at 09:07
  • @user1596031: That is Chrome app. and reads its data from Chrome databases; it doesn't get cookies by itself. – Ariyan Aug 14 '12 at 09:09