Solved

Counter for Website

Posted on 2001-06-08
14
265 Views
Last Modified: 2012-05-04
What is the simplest way to know how many hits there are on a site. I'm looking for the count first and 2nd how to show the hits on the site itself

TIA for your help
0
Comment
Question by:emery_k
14 Comments
 
LVL 17

Expert Comment

by:dorward
Comment Utility
Pay a visit to http://www.thecounter.com/

The best way though is to gain access to your server logs (ask your provider for access) and then use an analysis package on them,
0
 
LVL 17

Expert Comment

by:psimation
Comment Utility
Are you looking for a simple webcounter that sits on your website and just displays an incrementing number on the site each time a new visitor hits?
There are lots of ways to do this, and by far the most popular way is via a cgi script. There are many such scripts available for free on hte internet ( do a search on google.com for webcounters) and you should find them...
However, you will still need to know how to use it. You will probably need some assistance from your provider/hosting company to assist you in implementing the cgi script.
They will need to tell you what kind of servers they run {most of these scripts are written in perl, and as such will not run on a microsoft webserver}, even if you run your site from a Unix box, you still need to find out where the perl program is on hte server, and you will need to change the script to point to that.
Then you will simply call that cgi script from within the html code of your existing page. Some providers also doesn't include cgi/server side services with all of their packages, so you'll definately have to find out from them what the situation on cgi-scripts are.
I had so much trouble getting some of these counters to run, that I opted to another solution in the end, I wrote a simple routine with PHP that reads a number from a mysql table, increments it, displays it, and then writes the new number back into the db. Works like a charm for me, and I don't have any worries about where perl is etc.
However, you still need to do some db setup, and most puritans out there will scorn at me for using this method...
You can also use 3rd party companies that does the tracking for you.

There are also tools that examine your server logs to gain added information, and one that I use myself is webalizer. This is however also a rather involved way of getting the data, as you need ssh/telnet access to your server to run the command from the command line or have a cron job run it for you at a set interval...
0
 
LVL 1

Author Comment

by:emery_k
Comment Utility
The comments are helping clarify things. I must apologize for not clarifying that this is on the Linux BOx I just set i[ inhouse on an ADSL connection and am doing some basic web site stuff to learn as much as anything. I guess that means that when I call my ISP (that is myself) he's pretty dumb about this.
OOPS.
0
 
LVL 1

Expert Comment

by:crouchet
Comment Utility
To psimation:

you don't actually have to do a any database stuff to use php for this. Sure, if you were storing a lot of stuff that would be the way to go but for just one number I would just stick it in an ascii file.

I just whipped up this code to set up a counter with PHP:

<?

$filename = "counter.txt";

if(file_exists($filename))
{
   if(!($counterFile = fopen($filename, "r")))
   {
      // If this script does not work, uncomment the line below
      // (remove the double slashes) for an error message.
      //print("Error - could not open file $filename for reading");
   }
   else
   {
      //Ok, we found the file and opened it, now read the old count.
      $count = trim(fgets($counterFile,255));
      fclose($counterFile);
      if(!settype($count, "integer"))
      {
         // If what we got is not a number, set count to zero.
         $count=0;
      }
   }
}
else
{
   // If there is no file, this must be the first time through.
   $count = 0;
}
// Add one to the count and print it to the browser. You may want
// to change the style part to present the counter with a different
// font, color, alignment, etc.
$count++;
print("<div align=\"center\">");
print("<p style=\"text-align:center; width:.75in; background-color:black; color:lime; font-weight:bold;\">\n");
printf("%07d",$count);
print("</p>\n");
print("</div>");

if(!($counterFile = fopen($filename, "w")))
{
   // If this script does not work, uncomment the line below
   // (remove the double slashes) for an error message.
   //print("Error - could not open file $filename for writing");
}
else
{
   // Ok, all is well, so write the new count to the file.
   fputs($counterFile, $count);
}
?>

I tested out this counter on one of my pages so I know it works. Just put it in your HTML where you want a counter. If it does not work, you probably have a permission problem or PHP is not installed on your machine. To fix the permission problem, CD to the directory where your html resides and use these commands:

touch counter.txt
chown nobody counter.txt

To make PHP work on your machine at all it must be installed. You will need the PHP package (assuming an RPM based distro like RedHat). Under Redhat, for instance, you would need to install something like:

php-4.0.4pl1-3.rpm

Or some revision thereof.

BTW, Anyone who wants to use my code above, feel free. With such a short snippet that should go without saying but in this day of legal rights and software patents, I guess I need to give explicit permission. If you need a legal model, think of this as liscensed under LGPL.

JC
0
 
LVL 1

Expert Comment

by:crouchet
Comment Utility
Oops --

I forgot to mention that you will need to rename the html that contains the PHP code above. just change the .html (or .htm) to .php so index.html would become index.php or fred.htm would become fred.php

JC
0
 
LVL 1

Author Comment

by:emery_k
Comment Utility
crouchet
Looks like slick way to do it and exactly what I was looking for. Works part of the way but error message shows up. Have PHP installed, renamed to index.php, did permission problem fix. Also created counter.txt with value of 500 and redid permission fix. Take a look at 209.206.247.1
It adds 1 to what is in counter.txt then sits there.
0
 
LVL 1

Expert Comment

by:crouchet
Comment Utility
I can't seem to get to your site from here (I am behind a company firewall, so who knows what that means). I included two debugging statements in the PHP counter code, but I commented them out. Go ahead and remove the // from in front of those two print statements and run it again. Let me know if it gives you an error message.

Also, when you say that it adds 1 to counter.txt, are you saying that it actually changes the number in the file? If so, the problem may be an HTML problem rather than a PHP problem. Call that page up in a browser and choose View, Source. Copy that and paste it into a message so I can take a look at it (assuming you don't have any Secret Knowledge(tm) on that page <grin>).

JC

PS - I have turned that code into a function and I am putting it into a library on my system so it can be called from any page with PHP. I changed the name of the counter.txt file so it includes the calling page name. That means each page on my site can have it's own counter without the files overwriting one another. index.php would create a counter file called indexCnt.txt, staff.php would create staffCnt.txt, etc. If you want, I can post those changes once I have it all done.

The "right way" to do this is to have various lines in one counter.txt file and parse out that file to find the right counter. Maybe that will be my version 2.0 <grin>.

0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 1

Author Comment

by:emery_k
Comment Utility
Try www.nhwyoming.com
I'll look at it closer also. I really like the idea of counters per page.
0
 
LVL 1

Author Comment

by:emery_k
Comment Utility
Error message is
Warning: fopen("counter.txt","w") - Permission denied in /var/www/html/needhelp/index.php on line 154
Error - could not open file counter.txt for writing

Apparently it is having problems writing to increment the counter as counter.txt stays at the 500 I put in. I did the `touch counter.txt' & `chown nobody counter.txt' as you said. This is  a new install of RH7.1 if that makes a difference. Hopefully you can get to the page with www.nhwyoming.com so we don't need to clutter this up but if not I will paste it in.
0
 
LVL 1

Accepted Solution

by:
crouchet earned 50 total points
Comment Utility
Ok, the problem is still permissions. I assumed your server would run your web pages as "nobody" as is the default for RH, but apparently not.

Ok, as root CD to the directory with the counter.txt file and use this command:

chmod 666 counter.txt

And try it again. This is not a fix I would recommend on a machine with a lot of untrusted users, but this sounds more like a personal box so it should be safe enough to have this file world writable.

I'll post my improved code once I have it done.

JC
0
 
LVL 1

Author Comment

by:emery_k
Comment Utility
Works GREAT. Accepted as answer. when you get if finalized please let me know at emeryk@eksco.net
0
 
LVL 1

Author Comment

by:emery_k
Comment Utility
I will research the nobody issue with Apache
0
 
LVL 1

Expert Comment

by:crouchet
Comment Utility
I'll post it once it's done, but that will probably be next week. I got kinda busy this week.

JC
0
 
LVL 1

Author Comment

by:emery_k
Comment Utility
What you did accomplishes what I asked for. Anything more is icing on the cake so I can certainly wait. I really like this way as it is so simple. Thanks again
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now