Forum Moderators: coopster

Message Too Old, No Replies

Cookie Problem

         

radiator251

12:11 am on Nov 19, 2009 (gmt 0)

10+ Year Member



So I'm trying to figure out cookies. I have a login with a standard "remember me" selection, and it sets the cookies like this:


$expire = time() + 60*60*24*30;
if($_POST['remember'])
{
setcookie("NITEFLIP_REMEMBER_USERNAME", $_POST['username'], $expire);
setcookie("NITEFLIP_REMEMBER_PASSWORD", $_POST['password'], $expire);
}

...and sure enough, when I look at the cookies in FireFox, they appear as I set them. Cool.

However, my problem is I can't seem to retrieve them. I put a tester php script at the top of my home page:

if(isset($_COOKIE['NITEFLIP_REMEMBER_USERNAME']))
{
echo "Username is set";
}

...but nothing is appearing. What am I doing wrong?

Thanks.

skinsey

2:03 am on Nov 19, 2009 (gmt 0)

10+ Year Member




Here's some code from one of my sites. Hope this helps
$_POST['set'] is the remember me box.

setcookiecode.php

if(isset($_POST['set'])){
$expire = time()+60*60*24*30;
$password = $_POST['password'];
$encoded = base64_encode(serialize($password));
setcookie("user", $_POST['user'], $expire);
setcookie("password", $encoded, $expire);
setcookie("set", $_POST['set'], $expire);
}
else
{
setcookie ("user", "", time() - 3600);
setcookie ("password", "", time() - 3600);
setcookie ("set", "", time() - 3600);
}

top.php

$user = $_COOKIE['user'];
$password = $_COOKIE['password'];
$password = unserialize(base64_decode($password));

echo "$user $password";

radiator251

9:19 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



Thanks, but that doesn't seem to be working. Again, the cookies are set fine, but the retrieving doesn't work. Does it maybe differ across browsers, or is there another method of doing so?

TheMadScientist

11:59 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd try setting the path and domain too...

If you are setting them in a sub-directory and then trying to access them on the root they will not be available, and they will only be available on one version of the domain the way you are setting them, so if they are set at www.example.com/dir/ they will not be available at www.example.com or example.com/dir/.

Also, you said you're using FireFox, what domain and path does it say they're available for? Is that where you're trying to access them from?