Page 1 of 1

Hits Counter (PHP code) doesn't work quit right?

Posted: Wed Feb 01, 2012 3:12 pm
by hrose
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>". "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp&nbsp". $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\">";
}
}

}
?>

Re: Hits Counter (PHP code) doesn't work quit right?

Posted: Wed Feb 01, 2012 4:05 pm
by social_experiment
Is this a Joomla component or module?

Re: Hits Counter (PHP code) doesn't work quit right?

Posted: Wed Feb 01, 2012 4:32 pm
by hrose
No, this is a template override. I tried using it in place of Joomla's Statistic Module, which only counts hits on Articles that are clicked on and doesn't count Article Blogs that contain articles and Menu items that display pages, with components, like Fox Contacts. I need to count hits on all pages the user clicks on, not just Articles that are clicked on.

Re: Hits Counter (PHP code) doesn't work quit right?

Posted: Thu Feb 02, 2012 12:12 am
by social_experiment
Ok; do you have a url where you downloaded the code

Re: Hits Counter (PHP code) doesn't work quit right?

Posted: Fri Feb 03, 2012 3:14 pm
by hrose
social_experient: No URL, took an example from the web and changed it (lots - lol), to fit what I need. I thought that maybe it's because I don't close the file, "hits.txt. This is the new code: Notice that in the new code I use fclose() and in the old code I don't close the file. I'm not a PHP programmer, but in C programming I always had to close a file. Is this true with PHP???? Seems like PHP is allot like C?

<?PHP

$hitslog = "hits.txt";
## Get the hitslog file ready and read hitslog file
$hits = file($hitslog);
$hits = $hits[0] + 1;

## Opening the hits file and write the number of hits:
$fp = fopen($hitslog, "w");
fwrite($fp, $hits);
fclose($fp);

$menu = JSite::getMenu();
$menuItem = &$menu->getActive();
if($menuItem->home == 1) {

## Text counter, print the number of hits
## print "<b>Hits Since Dec. 20, 2011 </b><br>". "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp&nbsp". $hits. "</br>";

}

?>

Re: Hits Counter (PHP code) doesn't work quit right?

Posted: Sun Feb 05, 2012 2:46 pm
by hrose
I closed the file in the code and now instead of zeroing out, after five day of working correctly and count was at, 31456, it want to 11456??? What I'm I doing wrong in this code or is the good and it's outside the code????????????