|
Posted by Mladen Gogala on 06/14/06 14:56
Carl Anatorian wrote:
> Another one :
> <?php
> if($_COOKIE['hello'] != "world") {
> $result = setcookie("hello","world",60*10);
> }
> ?>
> <html>
> <body> <?php
> if(isset($_COOKIE['hello'])) {
> echo "hello !";
> }
> ?>
> </body>
> </html>
>
> I think it will show "hello !" when I access it the second time, but it
> never.Why? I don't disable my browser's cookies.
Carl, setcookie returns boolean, so it would be prudent to check
whether it has actually happened. You should add something like:
if (!$result) { die("Better luck next time"); }
Second, cookies will NOT be sent if the HTML headers have already been
sent. Your heading (<html>) makes sure that they have been sent, so your
cookie will probably not be sent. You should do something like
<?php ob_start(); ?> before the HTML header.
--
Mladen Gogala
http://www.mgogala.com
[Back to original message]
|