Hi All,

I've this problem with cookies. I am lost as I can't understand why this code does not work as expected.

Please help!!

$rq =$_REQUEST['r']  ;
$value = $_REQUEST['v'];
setcookie("XYZ",$value, time()+60*60*24*30) ;
echo "Value is ".$value; 
if ($rq =='showdata'){
    print_r($_COOKIE);
    echo "Cookie is".$_COOKIE['XYZ'];
    exit;
}

Output: when passing values r=showdata&v=test and pressing F5 repeatedly.

Value is testArray
(
[XYZ] =>
[PHPSESSID] => 060ee31ae35b8fb1262d0660bd925746
)
Cookie is

Member Avatar for diafol

USe $_GET instead of $_REQUEST

You can't show the cookie on the same page as when you set it. You need another call to the server for this. However, I think it should show on second load.
//edit

Yes tried it and it loaded on second load, giving an error first time around.

Search the cookies in the browser to see if it's stored where you think. The path could be wrong.

Thanks Ardav for your response.

You'll notice that the array output in the browser does show the cookie name. However, it doesn't show the value that was set.

Member Avatar for diafol

No I know. That's why I asked you to check the cookie path in the browser storage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.