homepage Welcome to WebmasterWorld Guest from 54.197.147.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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?
IntegrityWebDev




msg:4332061
 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?

Thanks,
Chris

 

penders




msg:4332089
 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;
}



IntegrityWebDev




msg:4332106
 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