tags:

views:

238

answers:

3

I'm not sure if I'm asking this properly.

I have two PHP pages located on the same server. The first PHP page sets a cookie with an expiration and the second one checks to see if that cookie was set. if it is set, it returns "on". If it isn't set, it returns "off".

If I just run the pages like
"www.example.com/set_cookie.php"
AND
"www.example.com/is_cookie_set.php"
I get an "on" from is_cookie_set.php.

Heres the problem, on the set_cookie.php file I have a function called is_set. This function executes the following cURL and returns the contents ("on" or "off"). Unfortunately, the contents are always returned as "off". however, if I check the file manually ("www.example.com/is_cookie_set.php") I can see that the cookie was set.

Heres the function :

<?php
function is_set()
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://example.com/is_cookie_set.php');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec ($ch);

curl_close ($ch);

echo $contents;
}
?>

Please note, I'm not using cURL to GET or SET cookies, only to check a page that checks if the cookie was set.

I've looked into CURLOPT_COOKIEJAR, and CURLOPT_COOKIEFILE, but I believe those are for setting cookies via cURL and I don't want to do this.

A: 

I believe you are making a confusion. When you are using curl, PHP will go to the trouble of acting like a client (like a browser maybe), and make that request for you. That is, the cookies that curl checks for have nothing to do with the cookies in your current browser. I think.

nc3b
A: 

I'm not entirely sure what you are trying to do here but you are aware, as nc3b already states, that in your is_set() function, it's PHP acting as the client and not your browser, right? That means that your cookie test will always fail (= return with no cookies).

Cookies are stored by the client and sent along with every request to the server.

If you want to find out in PHP whether a cookie has been set - of course, you need to be on the same domain as the cookie for that - you can use plain if (isset($_COOKIE["cookiename"])).

Maybe you are trying to build a solution to query for a cookie on a remote host. For that, see this SO question:

Pekka
A: 

Curl acts like your browser as a http client. If configured they both recceive and store cookies, but they are in no way related.

Curl doesn't use the browser cookies. If you want to use your browser cookies, you have to use the --cookie option switch. See the manpage for details: http://curl.haxx.se/docs/manpage.html
For example Firefox stores them in a file called cookies.txt. Under linux its located under ~/.mozilla/firefox/$profilefolder/cookies.txt

Hint: If you use Firefox >= 3.0 the cookies are stored in a sqlite database. If you want to use them with curl, you have to extract a cookies.txt file by yourself. Here are some examples how to do that:

echox