Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Set cookie before all other processing?

1:13 pm on Jun 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0

I'm in new territory and hoping for some help.

At the very top of my page I am setting a location cookie. To get the value of this cookie requires 2 database look ups, an xml look up and some math. Immediatly at the top of the HTML body (but down past this cookie set), I need to display the content of that cookie.

The problem I am getting is that the cookie is setting but the first time around the page isn't displaying the content. If I refresh it is there, if I watch it in Firebug it is setting but on the first load it isn't displaying.

I don't know enough about the mechanics of PHP to know if this is right or possible, but its almost like the page is continuing to process and display before the cookie has a chance to be completely set.

Given that is there any way to ensure that my cookie is set before any other processing takes place on the page?

2:04 pm on June 28, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0

Presumably you are reading the cookie by examining the $_COOKIE[] array? As you have found, this will only be set on the next page request. To make this available in the current request you can set this array property at the same time you setcookie()...

function saveMyCookie($name, $value, ....) { 
$ok = setcookie($name,$value,....);
if ($ok) $_COOKIE[$name] = $value;
return $ok;

2:44 pm on June 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0

THAT WAS IT! I was looking for a solution in the wrong direction...thanks!

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members