I was wondering how to get the ip location and also what OS the visitor is running. Say for instance, I use a Mac so if I would visit this website it would say Mac OS.
So basically something like ipchicken.com has.
To get the client's ip, use the global $_SERVER
array:
$ip = $_SERVER['REMOTE_ADDR'];
To resolve the ip to a hostname you can use gethostbyaddr()
:
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
For the user agent header, which will contain information about the OS and the browser which the client is using, the preferred way is to use the function get_browser()
:
$clientInfo = get_browser(null, true);
var_dump($clientInfo);
... what will give you an array like:
Array
(
[browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
[parent] => Firefox 0.9
[platform] => WinXP
[browser] => Firefox
[version] => 0.9
[majorver] => 0
[minorver] => 9
[cssversion] => 2
[frames] => 1
[iframes] => 1
[tables] => 1
[cookies] => 1
[backgroundsounds] =>
[vbscript] =>
[javascript] => 1
[javaapplets] => 1
[activexcontrols] =>
[cdf] =>
[aol] =>
[beta] => 1
[win16] =>
[crawler] =>
[stripper] =>
[wap] =>
[netclr] =>
)
The IP of the visitor is available in the $_SERVER['REMOTE_ADDR']
field, the user agent string is available in the $_SERVER['HTTP_USER_AGENT']
field.
Please note that both information may or may not be correct, depending on whether the visitor uses an HTTP proxy or modifies the headers sent by the browser.
The Name Address
can be obtained by a reverse DNS request.
To get information like the visitors' operating system or browser vendor, you can use the get_browser()
function provided by PHP or a replacement (depending on the environment you run your code in, the browscap.ini
file required by PHPs get_browser()
function might be outdated or missing).