Forum Moderators: coopster

Message Too Old, No Replies

curl and cookie problem

         

omoutop

8:55 am on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello all and thanks for any tip/advice.

I have the following code:

function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

$myurl = $_SERVER['DOCUMENT_ROOT'].'/somepage.php?var1=aaa&var2=bbb';
$fetched_data = get_data($myurl);

The target page ($myurl) checks for the existence of a cookie with this:
if (isset($_COOKIE['mycookie']))
{
// show [dataset 1]
}
else
{
// show [dataset 2]
}

The problem is that my $fetched_data always return the [dataset 2] data, even if cookie exists.

Is there a way to fix this? Should i modify anything in my curl()?
Take note that i know all the data that are stored in the $_COOKIE

jatar_k

12:43 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you dump $_COOKIE does the index you're using exist?

maybe try using a comparison for a specific value

if ($_COOKIE['somevar'] == 'somevalue')

if it's always dropping into the else then your test is failing so even though the cookie is actually set you may be testing the wrong thing

omoutop

1:12 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



that was my first thought Jatar

both pages are owned by me (in 2 different boxes)
so i am pretty sure that what i ask, actually exists

In main page (the curl one) i always get the no-cookie results.
In target page if i do print_r($_COOKIE) i get all values correct.

Thats why i am wondering if this is some curl() issue

andrewsmd

1:38 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try just doing a var_dump($_COOKIE['somevar']); To see if it is anything. The other two posts are correct though. If it is always going to the else, then it probably is nothing. But it could be something like "" or 0.

jatar_k

3:00 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so the script you are curling to isn't able to see the cookies

do you have to do something differently to show it cookies?

doesn't curl have to use the cookiejar or something (i forget, been a while) to store cookies? I am guessing you need to add in something to make the cookie available.

apologies I was thinking about it backwards

i didn't read all of this
[curl.haxx.se...]

The simplest way to send a few cookies to the server when getting a page with
curl is to add them on the command line like:

curl -b "name=Daniel" www.cookiesite.com

omoutop

8:30 am on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ok problem solved

i just recode target page using heredoc syntax (dont know why first code didnt work but now it behaves like it should have been)

And for the curl part:
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);