Jump to content

Problems with cookie


AXiSS

Recommended Posts

I am working on a site and trying to make templates, which users can switch between. I thought a cookie would be the best way to remember the user's choice. At the beginning of the index page I have
[code]<?php

if(!isset($_COOKIE['template'])) {
setcookie('template', '2_3.css');
}
[/code]

But that isn't setting a cookie for some reason. If I click on a different template link which goes to a page with just a setcookie function that redirects back to the homepage, the cookie is set.

Secondly, if I exit the browser and come back, it doesn't read the cookie, so is there anything I need to include to make the cookie permanent?
Link to comment
https://forums.phpfreaks.com/topic/34326-problems-with-cookie/
Share on other sites

The cookie only becomes active after the next page load.  If you load your page without any cookies, then refresh, it'll work fine.  To get around it, have just this code on index.php, and change your current index.php to main.php (or something else you like).

[code]
<?php

if(!isset($_COOKIE['template'])) {
setcookie('template', '2_3.css');
}

header("Location:main.php");
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34326-problems-with-cookie/#findComment-161710
Share on other sites

[quote author=Cagecrawler link=topic=122552.msg505776#msg505776 date=1168917697]
The cookie only becomes active after the next page load.  If you load your page without any cookies, then refresh, it'll work fine.  To get around it, have just this code on index.php, and change your current index.php to main.php (or something else you like).

[code]
<?php

if(!isset($_COOKIE['template'])) {
setcookie('template', '2_3.css');
}

header("Location:main.php");
?>
[/code]
[/quote]

OK, I'll try that out. Thanks for the help!
Link to comment
https://forums.phpfreaks.com/topic/34326-problems-with-cookie/#findComment-162426
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.