First, let's start with your syntax...
Originally posted by pkipper
<?
$visitcount = $HTTP_COOKIE_VARS["visits"];
if( $visitcount == "")$visitcount = 0;
else$visitcount++;
setcookie("visits",$visitcount);
print "This is visit number " . $visitcount;
?>
[/B]
You start things off with <?... usually a bad idea--use <?php, and you should probably turn off short tags if you plan on using any of the new standard feeds (XML, RSS, etc.). If your examples use <?, I would say ignore them and get a different source.
You then use $HTTP_COOKIE_VARS, which is deprecated. Use $_COOKIE instead, assuming your webhost isn't a few years behind. "else$visitcount++;" is just begging for an error.. I'm surprised you don't get one. Make sure you space every thing out correctly.
And finally, your setcookie() call. It should work under most circumstances, but there are a couple browser quirks. If you plan on having any IE6 visitors (which you probably will have), then be sure to set more parameters--namely, all of them. If you plan on NN4 support, it would also be wise to include all. So, in the long run, it's best to supply as much cookie information as you have available. There's more info about the parameters in the manual.
Originally posted by pkipper
Notice: Undefined index: visits in C:\Documents and Settings\PC\My Documents\WWW\home\a.php on line 2
Well, this won't be set yet. In fact, it won't be set until the next page load. You can do a quick check to see if it is set, however:
if (isset($_COOKIE['visits'])) { }
Originally posted by pkipper
Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\PC\My Documents\WWW\home\a.php:2) in C:\Documents and Settings\PC\My Documents\WWW\home\a.php on line 6
This is actually caused by the last error--you're sending a header, and the error is output. Thus, output is already sent, and you cannot send the header. Using the above fix should fix this one.
Originally posted by pkipper
This is visit number 0
Well, at least it's a number. So, you know the browser is getting the cookie.
Try and fix some of the above and see if you get any better mileage.
Hope that helps.