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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.