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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hrose
Forum Newbie
Posts: 4
Joined: Wed Feb 01, 2012 2:57 pm

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

Post 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\">";
}
}

}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post by social_experiment »

Is this a Joomla component or module?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
hrose
Forum Newbie
Posts: 4
Joined: Wed Feb 01, 2012 2:57 pm

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

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post by social_experiment »

Ok; do you have a url where you downloaded the code
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
hrose
Forum Newbie
Posts: 4
Joined: Wed Feb 01, 2012 2:57 pm

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

Post 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>";

}

?>
hrose
Forum Newbie
Posts: 4
Joined: Wed Feb 01, 2012 2:57 pm

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

Post 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????????????
Post Reply