Quickly and easily explains how to create your own counters, so that you can track the users that come to your page without them knowing a thing!
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Ok, so you want to find out who has been visiting your site do you? Well, luckily this is very easy using PERL 5.0! As long as you have CGI support (a cgi-bin) on your web page, and a space for a file (probaly wont be very big unless you experience like 5000 users a day!)
Anyways, lets start off the code with your general declaration...
******************************************
#!/usr/local/bin/perl -w
******************************************
This will enable PERL on your web page.
Now, what do we want to log? Well, let's log the total amount of visitors, as well as each visitors IP Adress. Then you can compare how many unique visitors you have, plus it is nice and easy.
******************************************
$USERIP = $ENV{'REMOTE_ADDR'};
******************************************
Ok, that was painless enough. But we want to find out how many total visitors we have had, this is a little more difficult. You must have a file *LOCATED IN YOUR CGI-BIN* called Counter.DAT (for the purpose of this tutorial at least). When you create it make sure the value of the file (the text inside) only is
0
No quotes or anything else, this is just a starting value that lets you add 1 to it each time the user views the page. Now, we need to open this file and get the value (how many views were had before this user came.)
******************************************
$fil = "counter.dat";
open(UFILE2, "$fil") || die "Error: Counter File Missing\n";
$Counterval = <UFILE2>;
close (UFILE2);
$Counterval =+ 1;
******************************************
Ok a little more complicated, but still basically easy. We just opened the file and said whatever was in it can assume the variable $Counterval. So if the file read 5, $Counterval would equal 5. After we got it, we said that it was equal to itself, so again to understand lets patch in 5.
$Counterval = $Counterval + 1
is equal to
5 = 5 + 1
or 6! It just tells it to add 1 to itself. Ok now lets write it back into the file...
(Note: I am writing, not appending. I want to overwrite the previous variable.)
******************************************
open(UFILE3, ">$fil") || die "Error: Counter File Missing!\n";
print UFILE3 $Counterval;
close (UFILE3);
******************************************
Ok. Now we want to save their IP address, so lets create a file called IPLOG.dat. This will hold all our IP's, again to make this tutorial easier, create a file called iplog.dat yourself, but leave it blank. Since we are using the APPEND method, it doesn't have to have a value in it to start out with. Remember, the variable $USERIP was the ip address (look at the top of this tutorial!), so we need to append this into a new line in the iplog.dat file.
******************************************
$fil = "iplog.dat";
open(UFILE4, ">>$fil") || die "Error: IPLOG File Missing!\n";
print UFILE4 "$USERIP\n";
close (UFILE4);
******************************************
Great! Lets overview this. First we replaced the old variable $fil with the new file we need to open. Then we opened it and APPENDED (>>) to it with their ip. Notice I used a \n (new line) return at the end, this is to make it alot easier to read, and organize it so that each ip is on one line. Simple enough right? And the user never noticed anything! You can track alot of stuff, that may be useful to your sales, or just cool to know! Anyways, a few tips to remember:
1) Make sure all files are in the CGI-BIN directory and that your host supports CGI
2) Make sure that all files are set to CHMOD 755 (Most FTP programs will set this upon request or servers will automatically). If you are unsure, just try the program. If it gives an error then set your CHMOD.
Anyways, here is the source code without any breaks. Enjoy, its free for grabs, and free for modification! Thanks. - Alex
***********************************************
#!/usr/local/bin/perl -w
$USERIP = $ENV{'REMOTE_ADDR'};
$fil = "counter.dat";
open(UFILE2, "$fil") || die "Error: Counter File Missing\n";
$Counterval = <UFILE2>;
close (UFILE2);
$Counterval =+ 1;
open(UFILE3, ">$fil") || die "Error: Counter File Missing!\n";
print UFILE3 $Counterval;
close (UFILE3);
$fil = "iplog.dat";
open(UFILE4, ">>$fil") || die "Error: IPLOG File Missing!\n";
print UFILE4 "$USERIP\n";
close (UFILE4);
***********************************************
Thanks guys, I am just starting with tutorials, I will be doing alot of new ones in CGI (Perl 5) and Visual Basic hopefully. Bye!
UPDATED
I fixed some of the problems I was having with the HTML code, and no you don't *need* to use flock command, only if you have a VERY high traffic site.
i though you had to flock this type of file to avoid to instances acess the file at the same time. (If this comment was disrespectful, please report it.)
This is the most powerful and easy to use Perl code example I've used n a long time! Thanks, alex! (If this comment was disrespectful, please report it.)
Its ok code, I probably would have done it a little different so it is not as long and you could record which visitor had which ip and what time they came at :-) (If this comment was disrespectful, please report it.)
It would probably be wise to check for errors when closing the files. Is it really necessary to open that many files anyway? You could just put in one flat text file delimitered by a pipe or something. It isn't worth setting $fil all those times either if it just going to change in a second. Instead, just hardcode it--saves space. (If this comment was disrespectful, please report it.)
hi, nice tutorial, but is there suppposed to be some sort of link in my webpage to this cgi script, or something?? Does the script work just by being in the cgi-bin?? Thanks (If this comment was disrespectful, please report it.)
rob.. it's < and > [i'm not sure if it used that as html or not, but if it did.. there's ;'s (semicolans) after the t's]
TheHumanTrashcan.. nice name lol.. i'm not positive how to do this.. i'm new at cgi/perl.. but i think you put on the page you want the counter to be on if you want it to be invisable... you'd need to have the script as "counter.cgi" and it'd have to be in your cgi-bin directory [ex. yoursite.com/cgi-bin/] but maybe this is .pl or whatever.. i'm not sure.. like i said.. i'm new to perl :) (If this comment was disrespectful, please report it.)
what about dynamic ip addresses, i can be seen as two different users if i log off and back on, i may get another address cause mine are dynamic... (If this comment was disrespectful, please report it.)
Isn't a counter script kind of worthless if the user has to open the perl script before anything is recorded? How do i make it so that when a user opens my homepage (html) it records the hit? Also, how can i display the current hit count on my homepage? (If this comment was disrespectful, please report it.)
The idea (just to make sure this is clear) is that i don't care how many people open "counter.pl" i want to know how many people open "index.html" (If this comment was disrespectful, please report it.)
3/6/2003 8:01:00 AM:
Wrong. You do need flock(). I have seen so many medium sites burned by this. What you're saying is that you don't need to do it the right way if you're a small site. (If this comment was disrespectful, please report it.)
Even moderate traffice will cause issues if you do now use flock. Flocking costs too little to ignore it, and hurts too bad when two users hit the file at the same time and wipe it out. (If this comment was disrespectful, please report it.)
5/26/2005 11:06:49 AM:
srry but i really dont get a thingy of it :S where do u have 2 put this in your html??? (im scripting with notepad) (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)