Jump to content

Can not echo or print_r $_COOKIE variables to safari , Firefox ok.


noyfb

Recommended Posts

Hi,

 

This is my first post, I am also a new PHP coder.

Took a while to find a PHP forum so I hope this one is healthy cause I'll be active for a very long time!

 

code:

setcookie("user", "AArrr", time()+604800);

$user = $HTTP_COOKIE_VARS["user"];

echo $user;

 

Tired just using $_COOKIE['user']; with no luck.

 

 

The cookie gets set in Safari, I see it and it's content when using Safari show cookies,  but when I try to print it's content to the browser, using the code above, I get ether "array()" or "    "blank data. It depends on the code used.

 

Firefox work 100%

 

I am sure I am not the only one that got this annoying glitch.

 

Tried to search google for "Safari #_COOKIE" but it's polluted with ignorant users trying to delete there cookies. 

 

 

Any help is appreciated.

The $_COOKIE variable is not available until the next time the browser requests a page. FF has a nasty habit of requesting pages twice to apply the default character encoding that has been set, so the code on your page has probably been requested twice in FF in which case, setcookie() has sent the cookie to the browser, and it has been sent back to the server, while better behaving browsers have only been sent the cookie and have not sent it back to the server yet.

Thank for the intel!

 

I also found that Safari dictates that the 4 first variables must be set to be able to read it's data.

 

setcookie( 'variable name', 'variable data', time, 'path')

Time can not be empty, use NULL or specify 0.

And like you said you have to reload the page to echo the data.

 

<?php

// SET COOKIE //

setcookie ('user_id','44', NULL , '/');

 

// Print Cookie

#1

$arr = $HTTP_COOKIE_VARS["user_id"];

echo $arr;

 

#2

echo $HTTP_COOKIE_VARS["user_id"];

 

#3

echo $_COOKIE['user_id'];

?>

 

I guess it's best to supply a variable for all cookie fields.

setcookie('variable name', 'variable data', time, 'path', 'domain', secure);

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.