PHP Cookie won't Set?

I have written the below code but for some reason it will not set the cookie at all when i place it in the head of my site?
[php]<?php $value = $_GET['r']; setcookie ('r', $value, time()+60 * 60 * 24 * 30);?>[/php]
The code needs to get what ever value ‘r’ holds from the url and save the value in the cookie named ‘r’.

What am i doing wrong?

I’m no Cookie expert, I always want to say that. :smiley: However, maybe the following can clear this up:

[php]<?php
$value = filter_input(INPUT_GET, ‘r’, FILTER_SANITIZE_SPECIAL_CHARS);
setcookie( “TestCookie”, $value, time()+(606024*30) );

echo ‘

’;
print_r($_COOKIE);
echo ‘
’;

echo $_COOKIE[‘TestCookie’] . “
\n”;
?>

Untitled Document Item [/php]

Thanks for the help,
i tried your code but i wasn’t able to get it to save a cookie :’(
The page displays
[php]’; print_r($_COOKIE); echo ‘’; echo $_COOKIE[‘TestCookie’] . "
\n"; ?> Item[/php]
when visited.
any ideas why it won’t work?

If you are seeing this on the actual page in the browser then you have a typo in your script. Probably missed out the > from the first ‘

’;

Red :wink:

I used
[php]<?php
$value = filter_input(INPUT_GET, ‘r’, FILTER_SANITIZE_SPECIAL_CHARS);
setcookie( “TestCookie”, $value, time()+(606024*30) );
echo ‘

’;
print_r($_COOKIE);
echo ‘
’;
echo $_COOKIE[‘TestCookie’] . “
\n”;
?> Untitled Document Item [/php] It looks like the
 are there, can anyone see anything wrong with this?

I stand corrected, no typo there ???

As a sidenote, I just tested the code you posted below, it works fine. (see screenshot below)

I would suggest this is a php version thing, do you know what version you are using?
If not put this in a php page a browse to it in your browser.
[php]<?php phpinfo(); ?>[/php]

Just this code alone, nothing else - and remember to delete it when finished.
Red :wink:


k.JPG

Thanks for confirming that it’s working, I didn’t know which PHP Version i was using but i did what you suggested and i’m on PHP Version 5.4.26 Would this be causing the problem?
Thanks for your help ;D

Actually, no. I kinda thought you were going to report back with version 4 or something very old like that.

I got it working, i had some white space in the code ???
Thanks alot for everyones help ;D

Well done, glad you figured it out :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service