使用 PHP 检测操作系统的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4104607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:58:12  来源:igfitidea点击:

Easiest Way OS Detection With PHP?

phpuser-agent

提问by user435216

I'm trying to figure out the visitor's OS is either a Windows, Mac or Linux using PHP(I don't need the version, distro info.. etc). There's several methods out there however they look a bit too complicated for this simple requirement.

我试图弄清楚访问者的操作系统是使用 PHP 的 Windows、Mac 或 Linux(我不需要版本、发行版信息等)。有几种方法,但是对于这个简单的要求,它们看起来有点太复杂了。

Are there any simple ways that could provide this sort of information yet still being quite reliable?

是否有任何简单的方法可以提供此类信息但仍然非常可靠?

Thanks in advance.

提前致谢。

采纳答案by Thariama

For an easy solution have a look here. The user-agent header might reveal some OS information, but i wouldn't count on that.

有关简单的解决方案,请查看此处。用户代理标头可能会显示一些操作系统信息,但我不会指望这一点。

For your use case i would do an ajax call using javascript from the client side to inform your server of the client's OS. And do it waterproof.

对于您的用例,我将使用来自客户端的 javascript 进行 ajax 调用,以通知您的服务器客户端的操作系统。并做到防水。

Here is an example.

这是一个例子。

Javascript (client side, browser detection + ajax call ):

Javascript(客户端,浏览器检测+ajax调用):

window.addEvent('domready', function() { 
  if (BrowserDetect) { 
    var q_data = 'ajax=true&browser=' + BrowserDetect.browser + '&version=' + BrowserDetect.version + '&os=' + BrowserDetect.OS; 
    var query = 'record_browser.php' 
    var req = new Request.JSON({url: query, onComplete: setSelectWithJSON, data: q_data}).post(); 
  } 
}); 

PHP (server side):

PHP(服务器端):

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $session = session_id(); 
    $user_id = isset($user_id) ? $user_id : 0; 
    $browser = isset($_POST['browser']) ? $_POST['browser'] : ''; 
    $version = isset($_POST['version']) ? $_POST['version'] : ''; 
    $os = isset($_POST['os']) ? $_POST['os'] : ''; 

    // now do here whatever you like with this information
} 

回答by Ali AlNoaimi

<?php

$agent = $_SERVER['HTTP_USER_AGENT'];

if(preg_match('/Linux/',$agent)) $os = 'Linux';
elseif(preg_match('/Win/',$agent)) $os = 'Windows';
elseif(preg_match('/Mac/',$agent)) $os = 'Mac';
else $os = 'UnKnown';


echo $os;

?>

回答by rémy

use the Net_UserAgent package

使用 Net_UserAgent 包

docu is here: http://pear.php.net/package/Net_UserAgent_Detect/docs/latest/Net_UserAgent/Net_UserAgent_Detect.html#methodgetOSString

文档在这里:http: //pear.php.net/package/Net_UserAgent_Detect/docs/latest/Net_UserAgent/Net_UserAgent_Detect.html#methodgetOSString

get the php file here: package/Net_UserAgent_Detect/docs/latest/__filesource/fsource_Net_UserAgent__Net_UserAgent_Detect-2.5.1Detect.php.html

在此处获取 php 文件:package/Net_UserAgent_Detect/docs/latest/__filesource/fsource_Net_UserAgent__Net_UserAgent_Detect-2.5.1Detect.php.html