This is a discussion on Help setting cookies without page reload. within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello All I'm trying to write a script that sets a cookie without having to refresh the page to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello All
I'm trying to write a script that sets a cookie without having to refresh the page to read it back in. This is how I am doing it at the moment: setcookie ("TestCookie", "", 0); setcookie ("TestCookie", "Knock Knock",time()+3600); session_start(); if (!isset($HTTP_SESSION_VARS['count'])) { $HTTP_SESSION_VARS['count'] = 0; $url = $HTTP_SERVER_VARS["PHP_SELF"]; $non_html_amp = false; if ( !empty($SID) && !eregi('sid=', $url) ){ $url .= ( ( strpos($url, '?') != false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID;} $header_location = ( @preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')) ) ? 'Refresh: 0; URL=' : 'Location: '; header($header_location . $url); exit; } else { unset($HTTP_SESSION_VARS["count"]); if (isset($_COOKIE['TestCookie'])) $COOKIE_Result = "<B>Passed</B>"; else $COOKIE_Result = "<B><font color=\"red\">Failed to set Cookies</font></B>"; } Echo "$COOKIE_Result<br>\n"; The only downside is this script doesn't want to work on all servers. Some server throw a fuss with the session_start(); So is there a way to get it to do the same without having to use sessions or having to refresh the page? Something like this: 1: Set Cookie 2: Read Cookie So not having to do it like this. 1: Set Cookie 2: Refresh Page 3: Read Cookie Thanks in advance Paul |
|
|||
|
Paul Kirby wrote:
> I'm trying to write a script that sets a cookie without having to refresh > the page to read it back in. > So is there a way to get it to do the same without having to use sessions > or having to refresh the page? > Something like this: > > 1: Set Cookie > 2: Read Cookie > > So not having to do it like this. > > 1: Set Cookie > 2: Refresh Page > 3: Read Cookie > > Thanks in advance > Paul i think there is a way to do it, but it's a bit tricky and intrusive on your code. If you use ob_start() to control the output of your site then you can send cookies even after data has been "sent". This works because ob_start() allows you to buffer all "normal" output, but does not catch header code, which passes freely to the client. (i learned this quite be accident.) http://www.php.net/ob_start For your case, i think the trick is simply to jump back to the start of your code, instead of doing a refresh, if you determine that the cookie hasn't been set yet. Keep in mind that using ob_start/ob_end requires that you pay quite a bit of attention to where you ob_start() and where you ob_end() - a bit of care is required to make sure you match each start with an end, for example. Caveat: i do not know if the cookie is set as soon as the header arrives or if the user agent waits until the whole request is complete. If the latter, then this trick won't do what you're trying to do. Also, this may very well be agent-specific, so you may find that it works on one browser but not another. -- ----- stephan beal Registered Linux User #71917 http://counter.li.org I speak for myself, not my employer. Contents may be hot. Slippery when wet. Reading disclaimers makes you go blind. Writing them is worse. You have been Warned. |
|
|||
|
Paul Kirby wrote:
> Ok > How would I make a variable stay set after page is refreshed. > Without using sessions or cookies? You can't, unless you encode it into every link on your page, so that it is passed back to your page on any click. The whole reason sessions and cookies were conceived was to fill this gap. -- ----- stephan beal Registered Linux User #71917 http://counter.li.org I speak for myself, not my employer. Contents may be hot. Slippery when wet. Reading disclaimers makes you go blind. Writing them is worse. You have been Warned. |
|
|||
|
Oh bugger :(
/me slaps session_start() for not working on other servers :( Thanks anyway Paul "stephan beal" <stephan@wanderinghorse.net> wrote in message news:bfgte6$3r6$3@ork.noris.net... > Paul Kirby wrote: > > Ok > > How would I make a variable stay set after page is refreshed. > > Without using sessions or cookies? > > You can't, unless you encode it into every link on your page, so that it is > passed back to your page on any click. The whole reason sessions and > cookies were conceived was to fill this gap. > > -- > ----- stephan beal > Registered Linux User #71917 http://counter.li.org > I speak for myself, not my employer. Contents may > be hot. Slippery when wet. Reading disclaimers makes > you go blind. Writing them is worse. You have been Warned. > |
![]() |
| Thread Tools | |
| Display Modes | |
|
|