Quick Search for:  in language:    
Quickly,easily,explains,create,your,counters,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 75,331. lines
 Jobs: 26. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Perl.
Message Sender
By sp on 1/15


Click here to see a screenshot of this code!Mailing List v2.0
By Aaron L. Anderson on 1/7

(Screen Shot)

ShowIMG
By Jeff Mills on 1/5


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

A Information Tracker (Advanced Counter)

Print
Email
 

Submitted on: 9/11/2000 7:57:30 PM
By: Alex Seidler  
Level: Beginner
User Rating: By 9 Users
Compatibility:5.0 (all versions), Active Perl specific, 4.0 (all versions), 3.0 (all versions), Pre 3.0

Users have accessed this article 15186 times.
 
(About the author)
 
     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...   
1) 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.   
2) 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.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) 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.


Other 1 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
9/13/2000 5:31:46 AM:rob
use < and > for the < > signs. i though you had to flock this type of file to avoid to instances acess the file at the same time.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/22/2000 8:56:16 PM:Ian Ippolito
Nicely done tutorial. Very easy to understand! Ian
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/25/2000 11:36:46 AM:PaC
you forgot to use flock() second you use ++ not =+ and if you were doing =+ it should be +=
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/2/2000 2:22:59 AM:skitz
Alex smells kinda funny, but at least i understand the tutorial.:-P
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/11/2000 3:07:18 PM:aguy
This is the most powerful and easy to use Perl code example I've used n a long time! Thanks, alex!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/16/2000 7:53:17 PM:robp
i like this code alot. it worked perfect! :-) go alex!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/22/2001 5:05:33 PM:Mark Hill
Good tutorial. Nice & relaxed :-D ... More!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/6/2001 11:33:07 PM:Sakatius
Thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/6/2001 11:33:44 PM:Sakatius
Thanks Alex.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/8/2001 7:30:28 PM:Hmm
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 :-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/19/2001 12:11:28 AM:damian
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/26/2001 9:18:15 AM:TheHumanTrashcan
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/10/2001 5:43:13 PM:HyPeR
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 <img src="/cgi-bin/counter.cgi" width="1" height="1"> 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 :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/29/2001 11:37:47 PM:Jyothika
Hi alex.. good code. Jyo
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/13/2002 2:55:26 AM:Dax Ahweng
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...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/28/2002 7:57:03 PM:Akujin
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/28/2002 7:59:58 PM:Akujin
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"
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.