Page 1 of 1
Cookies code not working
Posted: Sat Feb 25, 2012 8:41 pm
by esoftsolz
Code: Select all
<html>
<head>
<title>!..CookiePage..!</title>
</head>
<body>
<?php
$expire=time()+(3600*24*30);
if((!ISSET($_COOKIE["user"]))&&(!ISSET($_COOKIE["no_of_times"])))
{
setcookie("user","Rajesh",$expire);
}
else
{
echo"Welcome".$_COOKIE["user"]."..!<br/>";
}?>
</body>
</html>
Above is the php code I have written. It is not showing the desired output. Instead it shows an error as below
"Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\xampp\practice\cookies.php:6) in C:\xampp\htdocs\xampp\practice\cookies.php on line 10" I am a PHP learner. Can any PHP expert help me here.
with regards
esoftsolz
Re: Cookies code not working
Posted: Sat Feb 25, 2012 11:33 pm
by califdon
That's right. Once your script has sent
anything to the browser, you cannot use setcookie() function. It must be used
before anything like <html>, even a space or a blank line, has been sent. Refer to the manual:
http://php.net/manual/en/function.setcookie.php
Re: Cookies code not working
Posted: Sun Feb 26, 2012 1:34 am
by flying_circus
Califdon is technically correct, but I have found that it doesnt always work well with the design of my code. You can also use output buffering to tackle this problem. Check out the php manual for
ob_start()
Re: Cookies code not working
Posted: Sun Feb 26, 2012 9:09 am
by esoftsolz
Thanks for your help. It works now.

Re: Cookies code not working
Posted: Sun Feb 26, 2012 12:20 pm
by califdon
flying_circus wrote:Califdon is technically correct, but I have found that it doesnt always work well with the design of my code. You can also use output buffering to tackle this problem. Check out the php manual for
ob_start()
Thanks, flying_circus, I always forget that, because I seldom use output buffering. Actually, I don't use cookies very much. There are so many things to remember!!
