Home » PHP

Website statistic (User Information) in PHP

30 March 2008 237 views Popularity: 1% Share/Bookmark

email

From the code below, you can get IP Address, Page name, Browser name, Operating System name, and Page Referrer name.

Note: If you directly run this code, you will not get Referrer name. To view referrer information, you can link the page below from any other page.  


<?php
/*********************************

WEBSITE STATISTIC IN PHP

Getting user information in PHP

Programmed By: Mukesh Chapagain

http://www.chapagain.com.np

http://blog.chapagain.com.np

*********************************/

//getting ip address
$ip = $_SERVER['REMOTE_ADDR'];

//getting the current page name
//the function basename() returns filename of the path provided
$page = basename($_SERVER['PHP_SELF']);

//getting referer for this page
//first checking whether the referer is set or not
if(isset($_SERVER['HTTP_REFERER']))
{
$referer = $_SERVER['HTTP_REFERER'];
}
else // if there is no referer
{
$referer = "No referer";
}

//getting browser and OS information
$user_agent = $_SERVER['HTTP_USER_AGENT'];

if(eregi("opera", $user_agent))
{
$browser = "Opera";
}
elseif(eregi("msie", $user_agent))
{
$browser = "IExplorer";
}
elseif(eregi("Konqueror", $user_agent))
{
$browser = "Konqueror";
}
elseif(eregi("Firefox", $user_agent))
{
$browser = "Firefox";
}
elseif(eregi("safari", $user_agent))
{
$browser = "Safari";
}
elseif(eregi("netscape", $user_agent))
{
$browser = "Netscape";
}
elseif(eregi("AOL", $user_agent))
{
$browser = "AOL";
}
else
{
$browser = "Other";
}

//getting OS (Operating System) information
// I have checked for only few OS. You can add them up.
if(eregi("windows nt", $user_agent))
{
$os = "Windows XP";
}
elseif(eregi("windows 98", $user_agent))
{
$os = "Windows 98";
}
elseif(eregi("linux", $user_agent))
{
$os = "Linux";
}
else
{
$platform = "Other";
}

// Displaying the result
echo "IP Address: $ip";
echo "<br/>";
echo "Page Name: $page";
echo "<br/>";
echo "Browser: $browser";
echo "<br/>";
echo "Operating System: $os";
echo "<br/>";
echo "Referer: $referer";
echo "<br/>";
echo "";

?>

Happy PHPing!!

Related posts:

  1. Website statistic (User Information) in Javascript
  2. How to get working site path and directory name in php?
  3. Magento: Track Visitor’s Information
  4. PHP : Read Write Xml with SimpleXML
  5. Regular Expression check, Validation in PHP
  6. PHP: How to get Main or Base URL?
  7. How to change the source code and modify/parse a website?
  8. How to get(view) html source code of a website
  9. Displaying date and time
  10. Random number, string generation in PHP