1

I am trying to check if a cookie exists on a server with a specific url.

    $ch=curl_init();
//curl_setopt ($ch, CURLOPT_COOKIE, "mm=38533;" );
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_COOKIEFILE,'/tmp/cookies.txt');
curl_setopt($ch,CURLOPT_URL,'http://www.engageya.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
$response=curl_exec($ch);
// get cookie
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $response, $m);

curl_close($ch);

var_dump(parse_url($m[1]));
echo "<br/>".$response;

The problem is when I am logged in onto the target site, and I need to check if I am logged into the site, so I try to retrieve the cookie from the site..But i fail. I thought to use the curlopt_cookie option..but I thought if there is an easy way to return the cookie from the server and check if it exists .

In short the cookie exists when I surf to the page. But it doesnt exist when the request comes back and i print it

1
  • 1
    Cookies don't live on the server. They live on the client. Commented Feb 6, 2013 at 11:39

1 Answer 1

1

Seems that the target host isn't available. Maybe a typo in url or it is really not available. I added the following option to curl:

curl_setopt($ch, CURLOPT_VERBOSE, 1);

Now I can see the following error:

* getaddrinfo(3) failed for www.engagya.com:80
* Couldn't resolve host 'www.engagya.com'

Update: After fixing the typo in the url, I got the following example working:

<?php

$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL,'http://www.engageya.com/wordpress/api/?action=isloggedin&uid=22472');
curl_setopt($ch, CURLOPT_HEADER, 1);
$response=curl_exec($ch);
curl_close($ch);

// use preg_match to get the cookie as in your question:
preg_match('/^Set-Cookie:\s*([^;]*);/mi', $response, $m);
var_dump($m);
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, we'll see. step by step. :)
problem is the headers dont help, because the cookie is set once.. i need to see if the cookie exists on the server. it gets a cookie being set each time. But i dont want to get a response that a cookie is set. I want to check for pre-existing cookies..check perhaps the cookie on the server, if it is saved there.
What do you mean with: 'The cookie exists on the server'.. ?
when you do set_cookie.. where is the cookie saved? on the server , right? how do I retrieve it? when i am on that page , the cookie does exist..which means it is saved on the server.. i think the problem is with this...curl_setopt($ch,CURLOPT_COOKIEFILE,'/tmp/cookies.txt');.. i need to find where the cookie is on the server..am I right?
:) A cookie is in any case saved on the client. I think what you mean is a session that is saved on server and identified by a cookie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.