0

hi all,
in a php file i have written these codes:

<html> 
 <body>
 <h1>Cookie Example </h1>
<font size=+2 face = verdana> 
<?  $visitcount = $HTTP_COOKIE_VARS["visits"];  
     if( $visitcount == "") 
         $visitcount = 0; 
     else$visitcount++; 
     setcookie("visits",$visitcount); 
     print "This is visit number " . $visitcount;  
?> 
</font>  
</body>  
</html>

when i browse it in internet browser it gives me a warning,and it is this :

cannot add header information-headers already snt by (......) ....

and a line number which refers to that line setcookie has been written .
why is it so?
:-|

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by paradox814
0

you cannot have any text sent to the user before you create a cookie, so your code should be replaced with...

<?
     //$visitcount = $HTTP_COOKIE_VARS["visits"];
     //if( $visitcount == "") 

     if (!isset($HTTP_COOKIE_VARS["visits"]))
         $visitcount = 0;
     else
         $visitcount = $HTTP_COOKIE_VARS["visits"]+1; 
     setcookie("visits",$visitcount); 
?> 
<html> 
 <body>
 <h1>Cookie Example </h1>
<font size=+2 face = verdana> 
This is visit number <? echo $visitcount ?>
</font>  
</body>  
</html>

this is the correct way of doing what you want, if the cookie is not created then assign the value to zero, otherwise add 1. What you were doing would generate a warning depending on the php settings on any other server.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.