0

I want to put some Date() value in Cookies which I would like to use the same data in other pages later. I do something wrong and I do not understand what is a problem?

<?php
$value = date('d-m-Y H:i:s');
setcookie("DATENEW", $value, time()+3600*24);
echo $HTTP_COOKIE_VARS["DATENEW"];
?>

I would much appreciate you suggestions and guidance! Thank you in advance!

phihag
  • 278,196
  • 72
  • 453
  • 469
Sergiu Costas
  • 530
  • 4
  • 8
  • 26
  • Well... what *is* the problem? Your code doesn't show one. – deceze Nov 21 '12 at 10:11
  • 1
    duplicate of http://stackoverflow.com/questions/12529469/php-working-of-cookies – deceze Nov 21 '12 at 10:17
  • Please note that `$HTTP_COOKIE_VARS` is [deprecated](http://www.php.net/manual/en/reserved.variables.cookies.php) since PHP/4.1.0. – Álvaro González Nov 21 '12 at 10:17
  • 1
    @Sergiu Be aware that the answer you accepted does not really solve your core problem. Clear your cookies and try again. Then read Carlos' answer or the beforelinked duplicate again. – deceze Nov 21 '12 at 11:12

3 Answers3

1

You cannot read a cookie in the same page request that the cookie is set. The $_COOKIE super global array (use this instead of the deprecated $HTTP_COOKIE_VARS) contains the cookies that the client has sent you. And in this page load you just sent the cookie to the client, so it will be in the next page load by the client that he/she will sent you the cookie and you will be able to read it.

Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57
1
<?php
$name   = 'cookieName';
$value  = date('d-m-Y H:i:s');
$expire = time() + 60 * 60 * 24 * 30; //cookie expires within 30 days    
// Set the cookie
setcookie( $name, $value, $expire );

// Get cookie value
if( isset($_COOKIE['cookieName']) ) {
    echo $_COOKIE['cookieName'];
}
?>

I didn't test it..

Erik-Jan
  • 194
  • 8
0

First $HTTP_COOKIE_VARS is deprecated use $_COOKIE instead.

<?php
$value = date('d-m-Y H:i:s');
setcookie("DATENEW", $value, time()+3600*24);
echo $_COOKIE["DATENEW"];
?>

Output: 21-11-2012 10:17:45

this is working for me.

René Höhle
  • 26,716
  • 22
  • 73
  • 82