Website statistic (User Information) in PHP
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:
- Website statistic (User Information) in Javascript
- How to get working site path and directory name in php?
- Magento: Track Visitor’s Information
- PHP : Read Write Xml with SimpleXML
- Regular Expression check, Validation in PHP
- PHP: How to get Main or Base URL?
- How to change the source code and modify/parse a website?
- How to get(view) html source code of a website
- Displaying date and time
- Random number, string generation in PHP
