Page 1 of 1
cookie help
Posted: Fri Jul 19, 2002 11:57 pm
by Sevengraff
when i use this:
Code: Select all
<?PHP
setcookie("page", $SCRIPT_NAME,time()+120);
?>
I get this:
Warning: Cannot add header information - headers already sent by (output started at /usr/local/psa/home/vhosts/abeta.anime-honor.com/httpdocs/function.php:168) in /usr/local/psa/home/vhosts/abeta.anime-honor.com/httpdocs/add_ffa.inc on line 12
what am i doing wrong? Ive never used cookies before.
Posted: Sat Jul 20, 2002 12:15 am
by gnu2php
It's because cookies are sent in HTTP "headers," and these headers are always sent before the HTML is. So you need to set your cookie before anything is outputted.
Or you could use "
output buffering," to prevent anything from being outputted:
Code: Select all
<?php
ob_start();
echo "Hello\n";
setcookie ("cookiename", "cookiedata");
ob_end_flush();
?>
Posted: Sat Jul 20, 2002 12:17 am
by Sevengraff
thanks, i'll try that!
Posted: Sat Jul 20, 2002 12:24 am
by Sevengraff
no, i still get the same error, what do you mean by "Se your cookie before anything is outputted"?
Posted: Sat Jul 20, 2002 1:03 am
by gnu2php
I mean that you need to make sure nothing gets sent to the browser before you set the cookie.
For example:
Code: Select all
<html> // Cookie won't work (<html> is outputted)
<?php setcookie("cookiename", "cookiedata"); ?>
</html>
But this should work:
Code: Select all
<?php ob_start(); ?> // Output buffer is enabled
<html>
<?php setcookie ("cookiename", "cookiedata"); ?>
</html>
<?php ob_end_flush(); ?> // This sends the HTML to the browser
Just make sure
<?php ob_start(); ?> is at the very top of the PHP script.
Posted: Sat Jul 20, 2002 1:20 am
by Sevengraff
ohhh. ok thanks!
Posted: Sat Jul 20, 2002 5:18 am
by twigletmac
There was of course this sticky at the top of the forum that explains all that:
http://liberty.dnsprotect.com/~devnetwo ... php?t=1157
Mac