Reputation: 359
I seem to be losing the cookie information when going from page to page.
I have the generation of a cookie in a header.php file, which is then included on other pages. If i refresh one page the cookie does create a new one. However, when i go to a new page a new cookie is generated.
I have getCookie() in my header and then using $cookieID in the rest of the site when i need to use it.
here is the function im using to check if a cookie already exisits and if not generate the cookie
function getCookie() {
if(isset($_COOKIE['ID'])){
$cookieID = $_COOKIE['ID'];
}
else{
//generate random value for cookie id
$charid = strtoupper(md5(uniqid(rand(), true)));
$uuid = substr($charid, 0, 8)
.substr($charid,20,12);
setcookie( "ID", $uuid, time()+604800 ); // 7 days
$cookieID = $_COOKIE['ID'];
//need to refresh the page to get the new cookie ID
echo "<META HTTP-EQUIV='Refresh' CONTENT='0'> ";
}
return $cookieID;
}
Upvotes: 1
Views: 1448
Reputation: 10420
You should set the path parameter in the setcookie()
call, to, for example /
, if your site isn't in some subdirectory:
setcookie('ID', $uuid, time() + 604900, '/');
Also take care to use the same domain name everywhere for your site, otherwise some confusion may arise when a cookie set for, say, www.example.com is not availabe on example.com
without the www subdomain.
Upvotes: 3
Reputation: 7315
Try the following;
setcookie( "ID", $uuid, time()+604800, '/');
This sets the domain to root, so should work for your whole website.
Also, if you set a cookie on example.com - it automatically carries over to sub1.example.com, www.example.com etc but unfortunately this doesn't work in reverse.
Upvotes: 2
Reputation: 3372
This may or may not be your issue, but make sure your domain is not changing in any way when you nav through your app.
For example, going from http://www.yourpage.com/page1 to http://yourpage.com/page2 would lose your cookies, because you dropped the www.
Upvotes: 1