2

我想知道如何获取 ip 位置以及访问者正在运行的操作系统。比如说,我使用的是 Mac,所以如果我访问这个网站,它会说 Mac OS。

所以基本上像ipchicken.com这样的东西。

4

2 回答 2

2

要获取客户端的 ip,请使用全局$_SERVER数组:

$ip = $_SERVER['REMOTE_ADDR'];

要将 ip 解析为主机名,您可以使用gethostbyaddr()

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);

对于用户代理标头,它将包含有关客户端正在使用的操作系统和浏览器的信息,首选方法是使用该函数get_browser()

$clientInfo = get_browser(null, true);
var_dump($clientInfo);

...什么会给你一个像这样的数组:

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] =>
)
于 2013-04-13T19:19:35.697 回答
1

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).

于 2013-04-13T19:21:18.483 回答