Over the past couple of days i've been working on a little script that get the users IP, Host, and browsing agent. Getting the IP was failrly simple, the host, was a little trickier bcuz I was using REMOTE_HOST for awhile (which was returning nothing) until I read a few things about gethostbyaddr().
The main issue here, is the get_browser() function. So far, it works great and as far as I know it supports 7 different browsers (read the commenting in the script).
I'm looking for as many people as possible to test my script by going
here or you can copy/paste the entire script below and test it on your own server/etc.
Please tell me of any bugs/suggestions that you find/think of. Also, if this script detects you browser correctly (and I don't have it under "tested browsers") then please let me know and I will add it to the list.
This script may be used/distributed to anyone if and
Only if it is fully credited to me and whom ever else helped create/beta test it (which is shown in the script comments).
Enjoy!
PHP Code:
<?php
/*-------------------------------------------------------------
Created by: Ryan Miller
Started on: Monday, June 27, 2005
Last modified: June 29, 2005
Copyright 2005+ All Rights Reserved.
Features: Gets IP and Host from IP.
Will return a User ID which is IP@HOST
Returns a browsing agent that the user
is currently using.
Note: Not all browsers have been tested.
Current tested browsers include:
Mozilla Firefox v0.10.1
IE v6.0
Opera / Opera 8.01 (Thanks ParaSnake!)
Note#2: Currently, only mozilla firefox and
Opera 8.01 agents will return with a version.
Special Thanks:
The Online PHP Manual
www.BioRUST.com | www.forums.biorust.com
ChangeLog -
v1.0.1 - Added error handling for browser agent.
- Removed unnecessary code in get_browser()
- Fixed ID support for future MySQL DB handling
and printing.
v1.0.2 - Added full agent display, for testing
- Changed unit strings to bold
v1.0.3 - Added support for Opera 8.01
- Changed the order in which the browser is
detected. (opera 8.01 is now checked first)
v1.0.4 - Added support for Maxthon browsers.
---------------------------------------------------------------*/
function get_user_info()
{ $ip = trim($_SERVER['REMOTE_ADDR']);
if($ip === false)
{ return false; }
$host = gethostbyaddr($ip);
if(!$host)
{ $host = 'Unkown'; }
// print info out
print '<strong>User ID:</strong> '.$ip;
if($host)
{ print '@'.$host.'<br>'; }
//display browser/ver
$bInfo = browser_info();
if($bInfo === false)
{ print '<script>alert("Your browsing agent could not be detected. This very well could be a script bug/error. Please notify the script creator for further info and help.");</script>';
}
return true;
}
function browser_info()
{ # Checks for:
# Opera (opera 8.01, opera)
# Maxthon (maxthon)
# IE (msie, internet explorer)
# Firefox (firefox)
# Konqeror (konqeror)
# Lynx (lynx)
# Netscape (mozilla)
$agent = trim($_SERVER['HTTP_USER_AGENT']);
if(!$agent)
{ return false; }
//get opera 8.01 and version
if(stristr($agent,'opera 8.01'))
{ $id = 1;
$browser = 'Opera';
$ver = 'v8.01';
}
elseif(stristr($agent,'opera'))
{ $id = 1;
$browser = 'Opera';
}
elseif(stristr($agent,'maxthon'))
{ $id = 2;
$browser = 'Maxthon';
}
elseif(stristr($agent,'msie'))
{ $id = 3;
$browser = 'Microsoft IE';
}
elseif(stristr($agent,'internet explorer'))
{ $id = 3;
$browser = 'Microsoft IE';
}
elseif(stristr($agent,'firefox'))
{ $id = 4;
$browser = 'Mozilla Firefox';
// get version
$str = stristr($agent,'firefox');
$bArr = explode('/',$str);
$ver = $bArr[1];
}
elseif(stristr($agent,'konqeror'))
{ $id = 5;
$browser = 'Konqeror';
}
elseif(stristr($agent,'lynx'))
{ $id = 6;
$browser = 'Lynx';
}
elseif(stristr($agent,'mozilla'))
{ $id = 7;
$browser = 'Netscape';
}
else
{ $browser = 'Unknown'; }
// print agent for firefox/opera 8.01 (agent/version).
if((($id) && ($id == 1)) || (($id) && ($id == 4)))
{ print '<strong>Browser:</strong> '.$browser.' v'.$ver.'<br>'; }
// print for all other agents (agent)
elseif((!$id) || ($id != 4))
{ print '<strong>Browser:</strong> '.$browser.'<br>'; }
}
// print user info. If ip is not true, print error.
$user_info = get_user_info();
if(!$user_info)
{ print '<script>alert("It appears your IP address was unable to be found. Please notify the script creator for further info and help.");</script>';
exit;
}
// print full agent
print '<br><strong>Agent:</strong> '.$_SERVER['HTTP_USER_AGENT'].'<br>';
?>