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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

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

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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);

?>
Post Reply