Hits Counter (PHP code) doesn't work quit right?
Posted: Wed Feb 01, 2012 3:12 pm
I have this code and the 'hits.txt' works great, incrementing by "1" for each website page hit. The problem is that the file 'hits.txt' gets decrumented to "0" and starts all over again after a day or two. I don't understand why? I use bluehost and I running joomla 1.7.3 and the 'hits.txt' file is loacted where the joomla core files are, if that has anything to do with it. I'm a C++ programming and want to learn php, so I'm a beginner. 
Thanks,
-Heather
$counterstyle = "text";
$imagetype = "8";
## -- END OF CONFIG SECTION -- ##
$hitslog = "hits.txt"; # Path to the hits file, leave alone if in the same directory
# $hitslog = "http://localhost/templates/mw_plain001/ ... s/hits.txt"; # Path to the hits file, leave alone if in the same directory
$imagefolder = "http://localhost/templates/mw_plain001/ ... ats/digits"; # The full path to the images directory.
## Get the hitslog file ready
$hits = file($hitslog);
$hits = $hits[0] + 1;
## Opening the hits file and write the number of hits:
$fp = fopen($hitslog, "w");
fwrite($fp, $hits);
$menu = JSite::getMenu();
$menuItem = &$menu->getActive();
if($menuItem->home == 1) {
## Text counter, print the number of hits
if ($counterstyle == "text") { print "<b>Content View Hits </b><br>". "    ". $hits. "</br>"; }
## If Image Counter, get the required type and print them out.
if ($counterstyle == "images") {
$digit = strval($hits);
for ($i = 0; $i < strlen($hits); $i++) {
print "<img src=\"$imagefolder/$imagetype/$digit[$i].gif\" alt=\"There have been $hits visitors to this website\">";
}
}
}
?>
Thanks,
-Heather
$counterstyle = "text";
$imagetype = "8";
## -- END OF CONFIG SECTION -- ##
$hitslog = "hits.txt"; # Path to the hits file, leave alone if in the same directory
# $hitslog = "http://localhost/templates/mw_plain001/ ... s/hits.txt"; # Path to the hits file, leave alone if in the same directory
$imagefolder = "http://localhost/templates/mw_plain001/ ... ats/digits"; # The full path to the images directory.
## Get the hitslog file ready
$hits = file($hitslog);
$hits = $hits[0] + 1;
## Opening the hits file and write the number of hits:
$fp = fopen($hitslog, "w");
fwrite($fp, $hits);
$menu = JSite::getMenu();
$menuItem = &$menu->getActive();
if($menuItem->home == 1) {
## Text counter, print the number of hits
if ($counterstyle == "text") { print "<b>Content View Hits </b><br>". "    ". $hits. "</br>"; }
## If Image Counter, get the required type and print them out.
if ($counterstyle == "images") {
$digit = strval($hits);
for ($i = 0; $i < strlen($hits); $i++) {
print "<img src=\"$imagefolder/$imagetype/$digit[$i].gif\" alt=\"There have been $hits visitors to this website\">";
}
}
}
?>