homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Set cookie before all other processing?

5+ Year Member

Msg#: 4332059 posted 1:13 pm on Jun 28, 2011 (gmt 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?




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

Msg#: 4332059 posted 2:04 pm on Jun 28, 2011 (gmt 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;


5+ Year Member

Msg#: 4332059 posted 2:44 pm on Jun 28, 2011 (gmt 0)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved