Page 1 of 1

Cookies and page includes

Posted: Wed Jan 23, 2008 10:30 am
by peteywheats
Howdy Folks,
I'm having a problem, and cannot seem to figure out why. I'm hoping one of you wizards can help me out.
I have some index.php pages that are basically there to feed a cookie to a main index.php page.
You can see the code for the feeder index pages here:

Code: Select all

 <?php 
// This is one of the index pages, it's purpose is to feed the value "setting_one" 
// to the included index page and display the properly formatted page
$domain = "censored.removed.redacted.com";
 
// Removes old cookie value, sets new cookie to last 1 year.
setcookie ( "channelidcookie", "", time () - 360, "/", $domain );
setcookie ( "channelidcookie", "setting_one", time () + (60 * 60 * 24 * 365), "/", $domain );
 
// include the main index page
include ("../launcher/index.php");
 
?>
The layout of the page is handled on the main index file "../launcher/index.php" which depends on having the correct cookie to display properly, and that is not happening. If you reload the page in the browser, it works fine and displays the correct layout, but the first load, for some strange reason, loads the wrong way, as if it still had the old cookie.
I theorized that maybe I needed to pause the script before including the main page, but that didn't work.
Any advice or direction would be greatly appreciated. Thanks,
peteywheats

Re: Cookies and page includes

Posted: Wed Jan 23, 2008 10:33 am
by Chalks
I'm still somewhat new to php, so I'm not very familiar with the cookie functions, however I would recommend just putting a clause in your header that says "if cookie !exists, use default display".


Edit: never mind. I read your post again, and it looks like it's referencing an old cookie. Maybe make sure cookie is destroyed before creating a new one?

All this talk about cookies is making me hungry.

Re: Cookies and page includes

Posted: Wed Jan 23, 2008 10:37 am
by Zoxive
Cookies are not set until the next page load. (They are sent in headers)

Take a look at sessions.

Re: Cookies and page includes

Posted: Wed Jan 23, 2008 10:40 am
by peteywheats
Zoxive wrote:Cookies are not set until the next page load. (They are sent in headers)

Take a look at sessions.

OK, a lightbulb just went off. I will use a standard variable to set display type on the main index page, and the cookie will be available for subsequent page loads.
THANKS!