Thursday, 18 August 2011

Error while creating a cookie - Warning: Cannot modify header information


Are you getting this kind of error when creating a cookie or setting a cookie -

Warning: Cannot modify header information - headers already sent by (output started at e:\wamp\www\mysite\test.php:3) in e:\wamp\www\mysite\test.php on line 4

This error comes when some data is sent to the browser before setting the cookie.
So setcookie or setrawcookie function should be the first to be called means,

setcookie or setrawcookie function should be the first line on your page.

So if you use code like this

some html tags here or other contents or data

<?php
setcookie( "cookiename", "cookievalue", time()+3600);
?>

so the above code will give error.

So you should do following thing.


<?php
setcookie( "cookiename", "cookievalue", time()+3600);
?>


No comments:

Post a Comment