Home | Scripts | Tutorials | Books | Hosting | Forums | Links|

Navigation:

Home
Source code
Tutorials
RSS feeds
Articles
Wordpress plugins
Books
Sample Chapters
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links






 
 

SPONSORS



 
 

Browser wars script

There has been a battle on for years between the various browsers for market share , at the moment Internet Explorer dominates but that could always change . In this script we will log all hits to our page and then determine the market share for Internet explorer and Netscape .

First we must build our database using MySQL and then create a table to store the hits . Start up your MySQL client and enter the following .

CREATE DATABASE browser;

USE browser ;

CREATE TABLE browser(
id int DEFAULT '0' NOT NULL auto_increment,
browser varchar(255) NOT NULL,
PRIMARY KEY (id)
);

That is the database , now we require a script to log all hits on our page . Here is that script we have called ours simplestats.php

<?php
//store the user info in $browser
$browser = $HTTP_USER_AGENT ;
//connect to our database
$db = mysql_connect("localhost","username" ,"password");
mysql_select_db ("browser",$db);
//our SQL query to insert the browser info into our table
$sql = "INSERT INTO browser(browser) VALUES('$browser')";
//store the result of the query
$results = mysql_query ($sql);
?>

Now test this out by navigating to the simplestats.php page and pressing refresh a few times , if you have Netscape and Internet Explorer on your system this will work even better .

Now we will jump to the script that generates the bar graphs for our data . This is called gdbars.php

<?
Header( "Content-type: image/jpeg");
$im = ImageCreate(150,200);
// Allocate colors
$red=ImageColorAllocate($im,255,0,0);
$green=ImageColorAllocate($im,0,255,0);
$blue=ImageColorAllocate($im,0,0,255);
$white = ImageColorAllocate($im,255,255,255);
$gray = ImageColorAllocate ($im , 0xC0, 0xC0 , 0xC0);
ImageFill($im,0,0,$gray);
// Determine size of image
$x=imagesx($im);
$y=imagesy($im);
// Create some bars from data
ImageFilledRectangle ($im , 10,$bluevalue, 60, 200, $blue );
ImageFilledRectangle ($im , 70,$redvalue, 120, 200, $red );
// Display modified image
ImageJpeg($im);
// Release allocated ressources
ImageDestroy($im);
?>

The next script is the script to display the results , this gets the total hits , Internet Explorer hits and Netscape hits .

<?php
// Connect to the database
$db = mysql_connect("localhost","username","password");
mysql_select_db("browser",$db);
//get the total hits and store in $total
$sql = "SELECT * FROM browser";
$results = mysql_query($sql);
$total = mysql_num_rows($results);
//for debugging to check the total hits
//echo $total;
//get the internet explorer total
$sql = "SELECT * FROM browser WHERE browser LIKE ('%MSIE%')";
$results = mysql_query($sql);
$msie = mysql_num_rows($results);
//for debugging to check the internet explorer hits
//echo $msie;
//get the netscape total
$sql = "SELECT * FROM browser WHERE browser LIKE ('%Netscape%')";
$results = mysql_query($sql);
$netscape = mysql_num_rows($results);
//for debugging to check the netscape hits
//echo $netscape;
//work out hits as percentages
$msiehits = ($netscape/($netscape + $msie));
$netscapehits = ($msie/($netscape + $msie));
//generate graphics
function graphgen ( $blueval , $redval )
{
$redstats = round($blueval*100,1 );
$bluestats = round($redval*100,1 );
$blueval = ($blueval * 200);
$redval = ($redval * 200);
echo"
<table><tr><td align=\"center\">$bluestats%</td><td>
<img src= \"gdbars.php?bluevalue=$blueval&redvalue=$redval\">
</td><td align= \" center \" >$redstats%</td></tr></table>" ;
}
echo"
<tr><td align=\"center\">
<table bgcolor=\"#cccccc\">
<tr><td align=\"center\" valign=\"bottom\">" ;
graphgen ($msiehits ,$netscapehits);
echo "</td></tr><tr><td align=\"center\" align=\"center\">" ;
echo "Internet Explorer Vs Netscape" ;
echo "</td></tr></table>";
?>

Here is a screen capture of our display , we will have a bigger online stats program later .

Notes :

Of course you could get more detailed info by checking for browser versions and different browsers .