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?
:-|

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.