Help setting cookies without page reload.

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-21-2003
Paul Kirby
 
Posts: n/a
Default Help setting cookies without page reload.

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


Reply With Quote
  #2 (permalink)  
Old 07-21-2003
stephan beal
 
Posts: n/a
Default Re: Help setting cookies without page reload.

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.

Reply With Quote
  #3 (permalink)  
Old 07-21-2003
stephan beal
 
Posts: n/a
Default Re: Help setting cookies without page reload.

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.

Reply With Quote
  #4 (permalink)  
Old 07-21-2003
Paul Kirby
 
Posts: n/a
Default Re: Help setting cookies without page reload.

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.
>



Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:33 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0