droe9376 2013-04-13 19:13
浏览 38
已采纳

获取IP位置和操作系统

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.

  • 写回答

2条回答 默认 最新

  • dongwei5794 2013-04-13 19:19
    关注

    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] =>
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置