Home Free Templets Works Mind Chiller Photo Gallery Family & Friends Links
 
 

.

Using PHP to know the informetion about the visitors of your site.

 

In this tutorial we will explain how to gather informations of the visitors of your site. So here is a quick guide that can help you make your own logging system. In the present lesson I'll try to help you to create your own system of site attendance statistics gathering. We’ll need PHP knowledge, skills of working with MySQL and knowledge of some environment variables for this.
Our script will show the following statistics:

  • 1. Number of hits made for each page
  • 2. Number of hits came from each I.P address.
  • 3. Number of hits for each page in each day.
  • 4. Number of hits came from each browser.
  • 5. Number of hits came from each operating system.

Let’s start with designing of database (MySQL). We have to create connect.php to connect with database.

connect.php:

<?php
$host =”localhost”;
$username ="root";
$password="elephant";
$db="db1";
$link = mysql_connect ($host, $username , $password) or die ("Unable to connect to Database!");
mysql_select_db ($db, $link) or die ("Unable to select base");
/* In this statement we select the database db1.*/
?>

Now we create the table user_log consisting log_when (current page), log_who (I.P address), log_when (time of hitting), hour, os (operating system) and browser fields. We have to create install.php file. The code is given below:

install.php:

<?php
include (“ connect.php”);
$sql="drop table if exists user_log";
/* If user_log table exists in the database then drop this table from the database.*/
mysql_query ($sql) or die (mysql_error ());
/*In this statement we execute the above query otherwise through mysql error.*/
$sql="create table user_log (
log_when varchar (11),
log_who varchar (15),
log_where varchar(255),
hour int(10),
os varchar(11),
browser varchar(11))";
/*In the above statement we create a table consisting log_when, log_where, log_who, os, hour and browser fields.*/
mysql_query($sql) or die(mysql_error());
/* execute this query to create user_log table. If the table is not created then it will show a sql error. */
?>

The next step is to gather information about browser or operating system. In this section we have to create browser.php to check type of browser and operating system.

browser.php:
<?php
if((ereg ("Nav", getenv("HTTP_USER_AGENT"))) || (ereg("Gold", getenv("HTTP_USER_AGENT"))) || (ereg("X11", getenv("HTTP_USER_AGENT"))) || (ereg("Mozilla", getenv("HTTP_USER_AGENT"))) || (ereg("Netscape", getenv("HTTP_USER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_USER_AGENT"))) AND (!ereg("Konqueror", getenv("HTTP_USER_AGENT")))) $browser = "Netscape";
/*This branch checks environment variable value which is obtained using function getenv() for presence of special strings corresponding to particular browser. In this case it's Netscape. In branch verity case $browser variable will be assigned Netscape value. Otherwise else part will execute.*/
else
if(ereg("Opera", getenv("HTTP_USER_AGENT"))) $browser ="Opera";
/*This branch checks environment variable value for presence of string corresponding to Opera browser. In branch verity case $browser variable will be assigned Opera value. Otherwise next levels of verification are executed. In this way other browser will be checked.*/
else
if(ereg("MSIE", getenv("HTTP_USER_AGENT")))
$browser = "MSIE";
else
if(ereg("Lynx", getenv("HTTP_USER_AGENT")))
$browser = "Lynx";
else
if(ereg("WebTV", getenv("HTTP_USER_AGENT")))
$browser = "WebTV";
else
if(ereg("Konqueror", getenv("HTTP_USER_AGENT")))
$browser = "Konqueror";
/*This following branch checks environment variable value for presence of string corresponding to "search machines". In branch verity case $browser variable will be assigned Bot variable. Otherwise next levels of verification are executed.*/
else
if((eregi("bot", getenv("HTTP_USER_AGENT"))) || (ereg("Google", getenv("HTTP_USER_AGENT"))) || (ereg("Slurp", getenv("HTTP_USER_AGENT"))) || (ereg("Scooter", getenv("HTTP_USER_AGENT"))) || (eregi("Spider", getenv("HTTP_USER_AGENT"))) || (eregi("Infoseek", getenv("HTTP_USER_AGENT"))))
$browser = "Bot";
/*If all of branches described above is false then $browser variable will be assigned other value. */
else
$browser = "Other";
//Now we have to check the type of operating system:
if(ereg("Win", getenv("HTTP_USER_AGENT"))) $os = "Windows";
/*This branch checks environment variable value for presence of string corresponding to particular OS.In branch verity case $os variable will be assigned Windows variable. Otherwise next levels of verification are executed.*/
else
if((ereg("Mac", getenv("HTTP_USER_AGENT"))) || (ereg("PPC", getenv("HTTP_USER_AGENT"))))
$os = "Mac";
else
if(ereg("Linux", getenv("HTTP_USER_AGENT")))
$os = "Linux";
else
if(ereg("FreeBSD", getenv("HTTP_USER_AGENT")))
$os = "FreeBSD";
else
if(ereg("SunOS", getenv("HTTP_USER_AGENT")))
$os = "SunOS";
else
if(ereg("IRIX", getenv("HTTP_USER_AGENT")))
$os = "IRIX";
else
if(ereg("BeOS", getenv("HTTP_USER_AGENT")))
$os = "BeOS";
else
if(ereg("OS/2", getenv("HTTP_USER_AGENT")))
$os = "OS/2";
else
if(ereg("AIX", getenv("HTTP_USER_AGENT")))
$os = "AIX";
/*If all of branches described above are false then $os variable will be assigned Other value.*/
else
$os = "Other";
?>

Now we have to create loginstat.php page where we can insert values into the database. The code is describe line by line

loginstat.php:

<?php
include ("browser.php");
/*In the first line of the program we have to include the browser.php page for get information about browser and operating system.*/
$who = $_SERVER['REMOTE_ADDR'];
/*Now we store the IP address from which the hit is made to a variable $who using the following function.$_SERVER['REMOTE_ADDER']. This method return the IP address of the machine from which hits are made.*/
$when =date ("d.m.Y");
/*Now we want the current date of hitting. We get current date using the following predefine function given below .We store current date within $when variable.*/
$where = $_SERVER['PHP_SELF'].'/'.$_SERVER['QUERY_STRING'];
/*Using the following code we get the current hitting page and it will store in the $where variable.*/
$browser = addslashes($browser);
$os = addslashes($os);
/*Now we store the browser and operating system in variables $browser and $os. Addslashes method returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NULL (the NULL byte). */
After that we create logEntry(). With in this function we use insert statement to insert values within the database. Code for this is given below:
function logEntry($when, $who, $where, $hour, $os, $browser){
// insert values to the database
$insert = "INSERT INTO user_log (log_when, log_who, log_where, hour, os, browser) VALUES ('$when', '$who', '$where', '$hour', '$os', '$browser')";
if (mysql_query($insert)){
return true;
}
else {
return false;
} }
/*If the query execute successfully then return true other wise return false.*/
Now we check whether this function return true or false. If this function return true then this page will forward to the location stat.php where we fetch data from database and print all those values.
if (!logEntry($when, $who, $where, $hour, $os, $browser)){
echo "Failed to log user!";
}
else {
header ("location : stat.php");
// this statement redirects the current page to stat.php
}
?>


Stat.php is used mainly for fetching data from database and printing all the fetched data.

stat.php.

<?php
//For getting total number of hits we used the following query
$sql="select count(*) as cc from user_log";
$query=mysql_query($sql);
$row=mysql_fetch_array($query);
/*fetches the number of row effects.*/
echo'<b><font color="#FF0000">Total hits='.$row['cc'].'</font></b><br>';
/*Here we print number of hits .*/
The following code gives the number of hits for each page using group by clause.
$sql="select log_where, count (*) as cc from user_log group by log_where order by cc desc";
/* Select log_where and the number of rows affected using group by clause and in descending order.*/
$query=mysql_query($sql);
print' <table width="644" border="1">';
print' <tr>
<td <b><font color="#FF0000">file name</font></b></td>
<td <b><font color="#FF0000">hit</font></b></td>
</tr>
</tr>';
/*Here we create a table with two columns named file name and hit.*/
while($row=mysql_fetch_rows($query)){
print' <tr>
<td width="46">'.$row['log_where'].'</td>
<td width="46">'.$row['cc'].'</td>
</tr>';
}
print "</table><br>";
/*Here we print the value of file name and hit field.*/
//The next segment of code gives the number of hits for each IP address.
$sql="select log_who,count(*) as cc from user_log group by log_who order by cc desc";
$query=mysql_query($sql);
// Execute this query.
print' <table width="644" border="1">';
print' <tr>
<td <b><font color="#FF0000">IP adress</font></b></td>
<td <b><font color="#FF0000">Hits</font></b></td>
</tr>
</tr>';
/*Here we create a table with two columns named IP address and hits.*/
while($row=mysql_fetch_array($query)){
print' <tr>
<td width="46">'.$row['log_who'].'</td>
<td width="46">'.$row['cc'].'</td>
</tr>';
}
print "</table><br>";
//The following code gives the number of hits in each day.It will print in accending order.
$sql="select log_when, count(*) as cc from user_log group by log_when order by log_when desc";
$query=mysql_query($sql);
print' <table width="644" border="1">';
print' <tr>
<td <b><font color="#FF0000">Date</font></b></td>
<td <b><font color="#FF0000">Hits</font></b></td>
</tr>
</tr>';
while($row=mysql_fetch_array($query)){
print' <tr>
<td width="46">'.$row['log_when'].'</td>
<td width="46">'.$row['cc'].'</td>
</tr>';
}
print "</table><br>";
//Next part of the code mention the number of hits for each browser.
$sql="select browser, count(*) as cc from user_log group by browser";
$query=mysql_query($sql);
print' <table width="644" border="1">';
print' <tr>
<td <b><font color="#FF0000">Browser</font></b></td>
<td <b><font color="#FF0000">Hits</font></b></td>
</tr>
</tr>';
while($row=mysql_fetch_array($query)){
print' <tr>
<td width="46">'.$row['browser'].'</td>
<td width="46">'.$row['cc'].'</td>
</tr>';
}
print "</table><br>";
//For operating system the code is given below:
$sql="select os, count(*) as cc from user_log group by os";
$query=mysql_query($sql);
print' <table width="644" border="1">';
print' <tr>
<td <b><font color="#FF0000">Operating System</font></b></td>
<td <b><font color="#FF0000">Hits</font></b></td>
</tr>
</tr>';
while($row=mysql_fetch_array($query)){
print' <tr>
<td width="46">'.$row['os'].'</td>
<td width="46">'.$row['cc'].'</td>
</tr>';
}
print "</table><br>";
?>

That's all .Thanking You
Contributed by : Sawan Kr. Bera /KoderGuru Team