tags:

views:

324

answers:

2
if(isset($_POST['remember'])) {
    if(!isset($_COOKIE['tracker_id'])) {
        setcookie('tracker_id', $_SESSION['id'], time()+2592000);
        setcookie('tracker_username', $_SESSION['username'], time()+2592000);
        setcookie('tracker_rsn', $_SESSION['rsn'], time()+2592000);
    }
}

I know the code works, because I check to see if there are cookies before I log in, and there aren't. I log in, and there are cookies. I close the tab (not the browser), re-open it in a new tab, and the cookies aren't there. I'm not sure if I overlooked something, but I'm not quite sure what's up here...

Any help is appreciated

+1  A: 

Your code should work as you describe provided that:

1) there is nothing else serverside interfering with the values of the cookies 2) you haven't told your browser to treat all cookies as session cookies

Have you built a test rig with just the minimum code necessary to set and read the cookies? Fromt the snippet you posted, there's obviously a lot more going on in your code. And try to test it in different browsers.

Something like:

<?php

if ($_GET['set') {
   setcookie('tracker_id', 'tracker_id set at ' . date('r'), time()+2592000);
   setcookie('tracker_username', 'tracker_username set at ' . date('r'), time()+2592000);
   setcookie('tracker_rsn', 'tracker_rsn set at ' . date('r'), time()+2592000);
}
print_r($_COOKIE);
?>

C.

symcbean
Tested it with the code you posted, and it worked fine. Which means I have some code or something interfering, I guess. I'll go look into more.
Andrew
+1  A: 

How are you checking to see if the cookies are there or not? Checking cookies array doesn't tell you the whole story. If you don't already have it, download the Web Developer Addon for FireFox. It has a feature to view, edit, and delete cookies for the site you are on. View your cookies after logging in. That will give you insight as to what is actually being set by the browser. Then close the browser and open again, visit site, and view the cookies again. See if they are perhaps still there, and just not being read.

I've learned the hard way that it's a really good idea to set the cookie path and domain explicitly rather than letting PHP default it. Otherwise cookies from mydomain.com will have a different path than www.mydomain.com and that can lead to the www cookies not being read from mydomain.com and other fun stuff. We now always explicitly set our cookies, after wasting probably a week's worth of development time over the course of 6 months trying to track that issue down.

If paths aren't the issue you might be inadvertently deleting the cookies. Do the values you are passing in from the SESSION always exist for sure when that code is run? A false value tells PHP to delete a cookie, which could happen by accident if the values are not defined in the array and you don't have a strong error reporting level.

Matt Inglot
They're set when I log in (I was checking using that add-on), I close and re-open the browser, and they're unset. I removed any code that could unset it, but it's still happening. I'm just going to move on and come back to it later, since it's really just starting to waste my time. (It's a weekend project, anyways. Not a huge deal.)
Andrew
No problem if you want to move on, but you didn't really indicate what Web Developer told you... was the cookie expiry set correctly? Also do the cookies disappear if you move one page forward? Ie. go to login page, post to login form, then go any other page on site? If so then you are accidentally deleting the cookies.
Matt Inglot
The expire date is accurate, and they don't disappear when I change pages. Now if I close the tab and go back, they won't appear *unless* I refresh the page. =/. When I close the browser, reopen it, and go back, they'll appear *untill* I refresh. The only code that could unset it is in the logout page, which is just setting the cookie to a negative value. So, unless there's some other way to unset cookies or something...
Andrew