Forum Moderators: coopster

Message Too Old, No Replies

Problem setting Cookie

Cookie keeps resetting

         

lubeguy

10:58 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



I have cookies set with the following code:

if(!isset($HTTP_COOKIE_VARS['cart_id'])) {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 14400, '/');
} else {
$cart_id = $HTTP_COOKIE_VARS['cart_id'];
}

The cookie sets correctly, however, it does not stick, it keeps getting re-created. This code worked at one time, but I had to re-install Apache and PHP. Ever since the reinstall it doesn't work. I am thinking I don't have something enabled in one of the two if not both. Can anyone help?

RonPK

11:45 am on Oct 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess your new install doesn't use $HTTP_COOKIE_VARS anymore. You probably should use $_COOKIE now. Run a simple test by looking at the output of this script:


<?php
echo '<pre>$HTTP_COOKIE_VARS: ';
print_r($HTTP_COOKIE_VARS);
echo '$_COOKIE: ';
print_r($_COOKIE);
echo '</pre>';
?>

lubeguy

8:32 pm on Oct 10, 2005 (gmt 0)

10+ Year Member



I get the following:

$HTTP_COOKIE_VARS

Notice: undefined variable: HTTP_COOKIE_VARS in(Location of the file I put this code into)

$_Cookie: Array
(
)

How can I set up my server to recognize $HTTP_COOKIE_VARS?

RonPK

9:02 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$HTTP_COOKIE_VARS is history, deprecated, gone. Just like $HTTP_GET_VARS and $HTTP_POST_VARS. These variables have been replaced by $_COOKIE, $_GET and $_POST. They're called superglobals and were introduced when PHP decided switch off register_globals by default.

As a very dirty workaround you could do something like this:
<?php
$HTTP_COOKIE_VARS = $_COOKIE;
// rest of script;

But if you do some reading on register_globals you may decide that that is not a decent approach, regarding security.

lubeguy

11:37 pm on Oct 10, 2005 (gmt 0)

10+ Year Member



Thanks, I just realized when I re-installed everything I installed php 5, and I was using php 4 before. I didn's realize those variables weren't around anymore. Again, thanks for the help.