Cookies and page includes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peteywheats
Forum Newbie
Posts: 2
Joined: Tue Jan 22, 2008 9:33 pm

Cookies and page includes

Post 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
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Cookies and page includes

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Cookies and page includes

Post by Zoxive »

Cookies are not set until the next page load. (They are sent in headers)

Take a look at sessions.
peteywheats
Forum Newbie
Posts: 2
Joined: Tue Jan 22, 2008 9:33 pm

Re: Cookies and page includes

Post 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!
Post Reply