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">
</font><font size="4" face="Arial" color="$color"> </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
|