Solved

Counter for Website

Posted on 2001-06-08
14
270 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
ID: 6170500
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
ID: 6171281
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
ID: 6177309
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 1

Expert Comment

by:crouchet
ID: 6179003
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
ID: 6179036
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
ID: 6186129
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
ID: 6186433
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
 
LVL 1

Author Comment

by:emery_k
ID: 6187223
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
ID: 6187431
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
ID: 6187741
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
ID: 6191359
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
ID: 6191366
I will research the nobody issue with Apache
0
 
LVL 1

Expert Comment

by:crouchet
ID: 6193039
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
ID: 6193139
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

685 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