Important alert: (current site time 7/15/2013 9:31:41 PM EDT)
 

article

Online User Counter (HOW TO CODE )Fully Explained !!!!!!!!!!!!With Source Code

Email
Submitted on: 3/26/2004 3:54:29 AM
By: stephen antony  
Level: Beginner
User Rating: By 4 Users
Compatibility: 5.0 (all versions), 4.0 (all versions), 3.0 (all versions), Pre 3.0
Views: 24426
author picture
(About the author)
 
     ONLINE User Counter (HOW TO CODE ) Fully Explained !!!!!!!!!!!!With Source Code

 
 
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.
				

Online  User Counter (HOW TO CODE ) Fully Explained !!!!!!!!!!!!With Source Code


 

Hai all on PSC
This Is Stephen antony i was really felling boarded today morning still my exams are not over any way i thought of writing something for psc i actually though to code some app in PERL later when some idea was sparkling over my Grey matter so i decided to Explain how to code an online users script in PERL even though this program has a lot of usefulness for your website as :
* The visitor wont fell alone while reading an article on your site as they fell some more people are their when the script shows
Their are 4 visitors in this website
The visitor feels a virtual company (I think planet source code must also use this script so that we can know how many people are on the site )
SO let me start
What is Online User Counter ?
this script tells users how many online visitor in your web site.
How it can be done ?
We all Know that when a computer is connected to the internet the ISP will assign an IP address for the computer in which way you connect a cable ,dsl,dial up etc ,,,,,,,,,,,,,,,,
So when a user from his browser request a website by entering its domain on the web browser as such
www.fatimacollege.net (The OFFICIAL WEBSITE OF FATIMA MATA NATIONAL COLLEGE)
the web browser will ask for its web servers DNS to the ISP 's DNS server and from their through many hubs and stuff finally( IF you fell difficult to grab this part pls contact me i will be happy to help you ) It will reaach the web server where the the website of fatimacollege is located the web server the web server will get the IP of the computer which requested the website www.fatimacollege.net and it will send the WebPages to that browser !
NOTE :
Here when many people request a website the web server will feed all the request simultaneously !
since one visitor will be from one unique IP we will be able to how many users are online by knowing how may request are made and from which IP
So Objective ?
The aim of our program should be a program that should fach information from the web server that how many client computers are connected to the web server by counting their IP

Choosing a language ?

                        IF you ask me to choose which language for this program i will tell PERL yeah it ts simply a great language which can do wonders  I love it it can run on any sorts of web servers regardless of the OS with full functionality

How to code ?

Coding can be done by getting server variables

How to get the server variables

     Server variables are environment variables that tell you about the environment that your application is running in. you can fetch all of information in the server variables, everything from what browser your user is browsing with to what type of server software your host is running. the connection type the IP  and a lol every thing any way I will give you the list of server variables we are using server variables to get remote IP okay so here is the list

USAGE :

$VAR = $ENV{'SERVER_VARIABLE'};

VAR =VARIABLE

SERVER VARIABLE =Server variable

       Server Variables

                          By :                                                   Stephen antony

AUTH_TYPE Authentication the server used
AUTH_USER Authenticated user name
CERT_COOKIE Unique ID of the client certificate
CERT_FLAGS Is client certification valid?
CERT_ISSUER Issuer of the client certificate
CERT_KEYSIZE Number of bits in the SSL key
CERT_SECRETKEYSIZE Number of bits in the secret key
CERT_SERIALNUMBER Serial Number for the client certificate
CERT_SERVER_ISSUER Issuer of the the server certificate
CERT_SERVER_SUBJECT Subject of the server certificate
CERT_SUBJECT Subject of the client certificate
CONTENT_LENGTH Length of the content
CONTENT_TYPE MIME type of the current page
GATEWAY_INTERFACE CGI version from server
HTTPS Is this secure through SSL?(secure socket layer)
HTTPS_KEYSIZE Number of bits in the SSL key
HTTPS_SECRETKEYSIZE Number of bits in the secret key
HTTPS_SERVER_ISSUER Issuer of the server certificate
HTTPS_SERVER_SUBJECT Subject of the server certificate
INSTANCE_ID ID for this instance in IIS (IIS WINDOWS)
INSTANCE_META_PATH Metabase path for this instance
LOCAL_ADDR IP of server
LOGON_USER NT login for current user(IIS WINDOWS)
PATH_INFO Server virtual path
PATH_TRANSLATED Server absolute path
QUERY_STRING Variable name value pairs from the url string
REMOTE_ADDR Client IP address for requesting machine
REMOTE_HOST Client IP address for requesting host
REMOTE_USER Remote User
REQUEST_METHOD Method of request
SCRIPT_NAME virtual path and file name of current script
SERVER_NAME Server name
SERVER_PORT Port being accessed
SERVER_PORT_SECURE 0=not secure, 1=secure
SERVER_PROTOCOL Name/Version of protocol used
SERVER_SOFTWARE HTTP software used on the server
URL URL without the domain name
HTTP_ACCEPT MIME types the browser knows
HTTP_ACCEPT_LANGUAGE Browser's language setting
HTTP_CONNECTION HTTP Connection
HTTP_HOST Domain hosting this request
HTTP_USER_AGENT Browser being used
HTTP_PRAGMA Cache page or not?
HTTP_COOKIE Cookie related to current webpage


 

 So Why wait let us Start coding ?

#!/usr/bin/perl

              The location of perl interpreter on your computer or on your web server

$url = $ENV{'SCRIPT_NAME'};
$ip = $ENV{'REMOTE_ADDR'};
$now=time;
$data = "data.txt";

here the SCRIPT_NAME I didn't discuss sorry see you might have done a perl script and and you want it to run on different systems so some use .cgi while others .pl which means a lot of Recoding to avoid this when we use this variable even though the script works with which ever name it will work !!

THE IP is collected we are going to use data.txt to store the IP

Now let us open the data file and add the data and lock the file

open (FILE,$data);
#flock(FILE,2);
@text = <FILE>;
#flock(FILE,8);
close (FILE);
foreach $line (@text){
chop($line);
($ftime,$fip)=split(/=/,$line);
if ($ip ne $fip){


$diff = time-$ftime;
if (($diff<$tol)&&($diff>0)){
push @newtext, $line;
}
}
}

NOW we can assign each visitor from an IP a number and his online time also

Close the file and write the result

$newline="$now=$ip"; # online time and IP for the user
push @newtext,$newline;
$steve=scalar(@newtext); #online user number for the user from an unique IP
open (FILE,">$data");
#flock(FILE,2);
foreach $line (@newtext){print FILE "$line\n";}
#flock(FILE,8);
close (FILE);

$text ="visitor";
$text .="s" if ($steve>1);

Summing up

Now to make the script give the output through a  WEB browser me have to code the script in a way let  so the full code will be like this !!!

pls go through the code carefully

#!/usr/bin/perl


$bgcolor = "#cccccc"; #change as you like
$color = "#fffffff"; #change as you like
$refresss=30; #refresh time (seconds). to see whether any new is their change if you need
$size = "2"; #change as you like
$face = "book man ols style"; #change as you like
print "Content-type: text/html\n\n";
$url = $ENV{'SCRIPT_NAME'};
$ip = $ENV{'REMOTE_ADDR'};
$now=time;
$data = "dta.txt";

open (FILE,$data);
#flock(FILE,2);
@text = <FILE>;
#flock(FILE,8);
close (FILE);
foreach $line (@text){
chop($line);
($ftime,$fip)=split(/=/,$line);
if ($ip ne $fip){


$diff = time-$ftime;
if (($diff<$refresss)&&($diff>0)){
push @newtext, $line;
}
}
}

$newline="$now=$ip";
push @newtext,$newline;
$steve=scalar(@newtext);
open (FILE,">$data");
#flock(FILE,2);
foreach $line (@newtext){print FILE "$line\n";}
#flock(FILE,8);
close (FILE);

$text ="visitor";
$text .="s" if ($steve>1);


print <<HtmlEND;
<html><head>
<meta http-equiv="REFRESH" content="$refresss; URL=$url"></head>
<body bgcolor="$bgcolor" topmargin="2" leftmargin="2">
<table border="6" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#808000" width="100%" id="AutoNumber1" bordercolorlight="#FF00FF" bordercolordark="#0000FF">
<tr>
<td><b><font size="6" face="Arial Unicode MS" color="$color">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font size="4" face="Arial" color="$color">&nbsp;</font><font size="6" face="Arial Unicode MS" color="$color">
</font></b><font size="6" face="Arial Unicode MS" color="$color">$steve $text online
</font></td>
</tr>
</table>

</body></html>
HtmlEND
;

 

Not over it again we have to install this script pls do name the script as you like .cgi or .pl okay dear now to display the number of online users on each page we can use frames to display for that let us add these few lines of html code to which ever page we need this online user counter to appear !!

<I  F  R  A  M    E frameborder="0" scrolling="no" width="123" height="16"
S  R   C="FILENAME.PL" n a m e="ONLINECOUNTER" style="c olo r: #FF6600; border-style: double; border-width: 3; background-color: # 8 0 8 0 0 0" align="middle">
</I  F  R  A   M  E>

okay for the time being that finishes our job !!!!!! This script can be expanded for many more such as using geographic targeting we can show which all visitors are from which all Countries like ;

      THE WEBSITE HAS 3 VISITORS ONLINE FROM INDIA,ICELAND,ITALY

 I will explain it later since i am busy with my exams i will do code that soon and post it on this website soon for the time being please do vote for my Article and code

                                                                                     THANKS A LOT

                                                                                 V Stephen antony

                                                                                     http://www.stephenonline.tk

 

 


 

 


 

 

 

 

 
 



 


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

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
3/27/2004 10:50:14 PMstephen antony

PLEASE leave your comments and ratings
THANKS !!!!!!!!!
Stephen antony
(If this comment was disrespectful, please report it.)

 
3/29/2004 12:59:15 AM

really nice work cool i am new to thissite but i understood what perl is with how to codeanonline user counter i do one website i depend oon afc live now i can code my own
thanks
stephen antony
(If this comment was disrespectful, please report it.)

 
3/31/2004 8:59:49 AMZak Farrington

Nice!
(If this comment was disrespectful, please report it.)

 
4/11/2004 12:10:25 PMstephen antony

Dear Friends !!! You can show the connection type ,Browser being used,and many many such information on Each User Online on the webpage by adopting my above method !! You can also Execute the script in other ways also !!!!
(If this comment was disrespectful, please report it.)

 
4/25/2004 12:36:04 PM

I dont know much about programming, coding but i do know i like people who take the time to teach others what they know or think is benificial, i hope to lear more than what i have so far, but im geting old, 57 and cant remember very well anymore, but i will not give up because there are still greate people like Stephen to inspire me...!
(If this comment was disrespectful, please report it.)

 
5/4/2004 8:59:42 AMDraKon

Nice code for pure perl only but to make this better I'd recomend useing a mySQL database or two.. depending on how far you want to take this program... Reason being i've actully developed a online counter myself and a good thing about mySQL is it helps to avoid the dreaded lock lag. On a small user volume site this would be just the ticket but for a highvolume site say 1000 hits a min or so you start running into the locks causeing people's display of the page to be haulted maybe a second or two. But that also depends on server speed memory and so on.. anyway keep codeing this is fine work.. Long Live PERL!
(If this comment was disrespectful, please report it.)

 
5/9/2004 9:19:03 AMstephen antony

thanks i wished the code to be in pure perl
(If this comment was disrespectful, please report it.)

 
5/27/2004 6:24:55 AM

Hey this is Nice! how did you to show the countries??
(If this comment was disrespectful, please report it.)

 
5/27/2004 4:50:09 PM

OK, very nice, indeed. So how do I put this at the bottom of my page and only have it refresh the visitor counter without refreshing the whole page?
(If this comment was disrespectful, please report it.)

 
5/27/2004 11:43:05 PMstephen antony

add REFRESH Tag On the HTML Code Inside The Script
(If this comment was disrespectful, please report it.)

 
10/5/2004 6:45:45 AMDanny Pryor

Man this is freakin un-real......I've downloaded code after code from P.S.C. and no-one has explained as to how to implement the counter into the page at all , until now ! AS I'm a NEWBIE at the whole Perl thing , I find this post very useful and find it a pitty that Steve has only recieved 3 Votes !
You have my vote ,
(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.)
 

To post feedback, first please login.