Acid
09-14-2005, 02:43 PM
This has been bugging me for a while and I can't think why it's happening. I have a basic hit counter on my site which used to use a flat text file to store the hit count, but the counter was resetting itself, so I changed the script to update a field in a MySQL database, but the counter is still resetting itself seemingly at random. This is the code I'm using:
<?php
$MySQLHost = "localhost";
$MySQLUsername = "****";
$MySQLPassword = "*******";
$MySQLDatabase = "counter";
$MySQLConnection = @mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
if (!$MySQLConnection) {
echo "<p><strong class=\"red\">ERROR:</strong> " . @mysql_error() . ".</p>\n";
}
$MySQLSelectDB = @mysql_select_db($MySQLDatabase, $MySQLConnection);
if (!$MySQLSelectDB) {
echo "<p><strong class=\"red\">ERROR:</strong> " . @mysql_error() . ".</p>\n";
}
$result = @mysql_query("SELECT `hits` FROM `count` WHERE `id` = '1'");
while ($row = mysql_fetch_array($result)) {
$currentcount = $row['hits'];
}
$imagedirectory = "/images/";
$currentcount += 1;
$newcount = $currentcount;
$digits = strlen($newcount);
$actdigits = strlen($newcount);
for ($a = 0; ; $a++) {
if ($a == $digits) {
break;
} else {
$img[$a] = "$imagedirectory";
$img[$a] .= "0.jpg";
}
}
for ($a = 0; ; $a++) {
if ($a == $actdigits) {
break;
} else {
$showdig = substr($newcount, $a, 1);
$img[$digits - $actdigits + $a] = "$imagedirectory";
$img[$digits - $actdigits + $a] .= "$showdig.jpg";
}
}
$displayblock = "";
for ($a = 0; $a < $digits; $a++) {
$displayblock .= "<img src=\"$img[$a]\" alt=\"Counter: $newcount\" />";
}
@mysql_query("UPDATE `count` SET `hits` = '" . $newcount . "' WHERE `id` = '1'");
@mysql_query("INSERT INTO `log` (`hitno`, `logged`) VALUES ('" . $newcount . "', '" . date("d-m-Y H:i:s") . "')");
echo $displayblock;
?>
I added a new table inton the database to log every hit and the time and date so that when the counter resets itself I know exactly when it happens and know what the last count was so I can manually put the count back to what it was, but this is happening five or six times a day and is starting to annoy me. Can anyone shed any light on what might be causing this? Anything wrong with the script?
In case it helpes the server is a Windows 2003 server running IIS 6.0 and PHP 4.3.7 with MySQL 4.0.20a-nt.
Cheers.
<?php
$MySQLHost = "localhost";
$MySQLUsername = "****";
$MySQLPassword = "*******";
$MySQLDatabase = "counter";
$MySQLConnection = @mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
if (!$MySQLConnection) {
echo "<p><strong class=\"red\">ERROR:</strong> " . @mysql_error() . ".</p>\n";
}
$MySQLSelectDB = @mysql_select_db($MySQLDatabase, $MySQLConnection);
if (!$MySQLSelectDB) {
echo "<p><strong class=\"red\">ERROR:</strong> " . @mysql_error() . ".</p>\n";
}
$result = @mysql_query("SELECT `hits` FROM `count` WHERE `id` = '1'");
while ($row = mysql_fetch_array($result)) {
$currentcount = $row['hits'];
}
$imagedirectory = "/images/";
$currentcount += 1;
$newcount = $currentcount;
$digits = strlen($newcount);
$actdigits = strlen($newcount);
for ($a = 0; ; $a++) {
if ($a == $digits) {
break;
} else {
$img[$a] = "$imagedirectory";
$img[$a] .= "0.jpg";
}
}
for ($a = 0; ; $a++) {
if ($a == $actdigits) {
break;
} else {
$showdig = substr($newcount, $a, 1);
$img[$digits - $actdigits + $a] = "$imagedirectory";
$img[$digits - $actdigits + $a] .= "$showdig.jpg";
}
}
$displayblock = "";
for ($a = 0; $a < $digits; $a++) {
$displayblock .= "<img src=\"$img[$a]\" alt=\"Counter: $newcount\" />";
}
@mysql_query("UPDATE `count` SET `hits` = '" . $newcount . "' WHERE `id` = '1'");
@mysql_query("INSERT INTO `log` (`hitno`, `logged`) VALUES ('" . $newcount . "', '" . date("d-m-Y H:i:s") . "')");
echo $displayblock;
?>
I added a new table inton the database to log every hit and the time and date so that when the counter resets itself I know exactly when it happens and know what the last count was so I can manually put the count back to what it was, but this is happening five or six times a day and is starting to annoy me. Can anyone shed any light on what might be causing this? Anything wrong with the script?
In case it helpes the server is a Windows 2003 server running IIS 6.0 and PHP 4.3.7 with MySQL 4.0.20a-nt.
Cheers.