Page 1 of 1

Cookies - is it possible to display them as they're sent?

Posted: Thu Jul 27, 2006 7:37 pm
by Locust
I have a log in form which posts the information to itself ($_SERVER['PHP_SELF']) and at the top of the page it does the login and sets cookies but in my output the cookie values don't show until I refresh the page? Is it possible to do this?

As a rough psuedo code

Code: Select all

user_login()
 |
 |
  --------------------------------------------------> if login is sucessful, set cookies
                                                      otherwise do nothing
 |
 |
Login is sucessful, setcookie()




echo cookies <------- blank until I refresh
I assume that cookies must be set before the page even is read (i.e the script) but wasn't sure.

Posted: Thu Jul 27, 2006 7:55 pm
by Ambush Commander
The hack way: when you use setcookie, also set the cookie to the $_COOKIE array.

Better way: create a cookie wrapper class instead of interfacing with $_COOKIE. When a cookie is set (using the wrapper), the change is immediately effected.

Posted: Thu Jul 27, 2006 8:07 pm
by Benjamin
You can set the cookie anytime before the header is sent, and you can set it anytime after the page is sent to the browser with javascript.

Posted: Thu Jul 27, 2006 9:09 pm
by Locust
I've recearched for about 40 minutes but I'm not any closer. Thanks for the reply but I'd rather not go near javascript just yet :P

Could you enlighten me on my other options, particularly a 'cookie wrapper class'? I've researched for a long time but I don't understand this. If you could provide an exmaple, I'm a very fast learner.

Thank you :D

Posted: Thu Jul 27, 2006 9:14 pm
by Benjamin
javascript set cookie is actually very simple, and you can echo it with php, but you shouldn't have to. I don't see why you can't set the cookie before the headers are finished being sent.

Posted: Thu Jul 27, 2006 9:22 pm
by Locust
astions wrote:I don't see why you can't set the cookie before the headers are finished being sent.
I don't know how.

Posted: Thu Jul 27, 2006 9:42 pm
by d3ad1ysp0rk
Locust wrote:
astions wrote:I don't see why you can't set the cookie before the headers are finished being sent.
I don't know how.
Put the setcookie call before ANY output. A line break, a <html> tag, an echo, ANYTHING constitutes output.

Posted: Fri Jul 28, 2006 6:23 pm
by Ambush Commander
Because the cookies ARE working (and not throwing the header error), I assume that he has output buffering enabled by default, and whether or not the cookies are being set is not the problem.

What I think the problem is, is this:

Code: Select all

<?php

setcookie('foobar', 'value');

echo 'Current cookies:';
var_dump($_COOKIE);

?>
The first time you navigate to this page, the set cookie isn't output. Refresh, and now the cookie appears in $_COOKIES.

A hack fix would be:

Code: Select all

<?php

$name = 'foobar';
$value = 'value';

setcookie($name, $value);
$_COOKIE[$name] = $value;

echo 'Current cookies:';
var_dump($_COOKIE);

?>