View Full Version : Having Cookie Problems...

06-17-2006, 03:02 AM
I had this working before... now it won't work! Anyways, basically what my code does is set a cookie with a persons name inside to be retreived later... anyways, here's a snippet of my code: (If you want the whole sha-bang, just say so)

$cookie_name = "name_cookie";
$value = "".$name."";
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie($cookie_name, $value, $inTwoMonths);

And to retreive said cookie:

if(isset($_COOKIE[name_cookie'])) {
$name_chat = $_COOKIE['name_cookie'];
echo "Name: <b>".$name_chat."</b><br>";
} else {
echo "You need to register before you can chat!</div>";

It worked before, but after I've reordered my directories around... it will not work. I snooped around and found this, but I'm not sure if its relevant. My pages are located in different directories, specifically these:

http://myip/nc_fc_db/nch_users/nch_process_addfc.php -- Cookie is SET here
http://myip/nc_fc_db/nch_chat/nch_chat.php -- Cookie is READ here

Any help would be greatly appreciated. Thanks!

Curtis D
06-17-2006, 03:23 AM
Use print_r (http://php.net/print_r) on $_COOKIE in the if statement where the cookie name is set to find out what's in the hash.

06-17-2006, 03:36 AM
I used the following code:

$name_chat = print_r ($_COOKIE['name_cookie']);
echo $name_chat;

and I got:
Notice: Undefined index: name_cookie in C:\pathtomyserverstuff\nc_fc_db\nch_chat\nch_chat.php on line 44

Did I do that correctly?

Curtis D
06-17-2006, 10:30 AM
You can't store print_r w/o output buffering, in this case, it just echo'd it to the browser, thus giving the same effect if you had not tried to store it.

In any case, I just noticed this code:

$value = "".$name."";This assignment doesn't make sense. First of all, you concatenate two empty string values on either side of the variable, which does nothing. Second, more important, you use $name, but you hadn't declared it above. I assume that means you're using register globals, which is a risk.

When on, register_globals will inject your scripts with all sorts of variables, like request variables from HTML forms. This coupled with the fact that PHP doesn't require variable initialization means writing insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this directive by default. When on, people use variables yet really don't know for sure where they come from and can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by users and disabling register_globals changes this.
Your error is almost 100% likely because that $name variable has nothing in it when using setcookie().

Change your code to the following

if ( isset($_POST['name']) )
setcookie('cookie_name', strip_tags($_POST['name']), ((60^3)*24+time());

// This is a short hand if() statement, doing what yours did above
// it's called the ternary operator
echo isset($_COOKIE['cookie_name']) ? 'Welcome, '.htmlentities($_COOKIE['cookie_name']) : 'You need to register';

Note that your HTML register form must have method="post" in the form tags, as well as making sure the name input element matches $_POST['whatever']. If using my code, you'd need <input type="text" name="name" ... />

06-18-2006, 12:05 AM
You forgot to add a singlequote ' in your if-statement:


change it to,


06-18-2006, 03:01 AM
Actually, I fixed it already, and it was a path error. I didn't know you had to assign paths to cookies, and that was my problem. The cookie was created in a different path than where it is used, thus it was restricted to where it was created. When I assigned it a path of the entire domain, it worked perfectly.

Thanks for the help however, and I'll use that if statement to shorten my code. :)

EDIT: Oh, and if you'll recall, that was a SNIPPET of my code. I had already declared $name elsewhere, all I did was put in the relevant code that had the cookie in it.

Curtis D
06-19-2006, 03:24 AM
Good job on getting it resolved, Drevin ;)