<?php
//get the user's IP
if (getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else {
$ip = getenv("REMOTE_ADDR");
}
//retrieve stored IP and count
$query = mysql_query("SELECT `ip`, `count` FROM `counter`")
OR die(mysql_error());
$row = mysql_fetch_array($query);
$count = $row['count'];
//is the stored IP different from the user's IP?
if ($ip != $row['ip']) {
$count++;
//store the user's IP and set the incremented count
mysql_query("UPDATE `counter` SET `ip`='$ip', `count`='$count'")
OR die(mysql_error());
}
echo $count;
?>
I think the problem lies in that your table has only 1 row.
The way you are retrieving this row means that if no rows are returned, you get no count to work with.
I'm not too sure of the rationale of pining the IP address either, since likely the ping will be blocked.