Remote OS detection with PHP
I run a Apache/php server on Mac, and I want to开发者_StackOverflow中文版 show different info based on the OS that accesses it.
How can I do that with PHP?
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.
Here is an example.
Javascript (client side, browser detection + ajax call ): (Note: You will need the detection script from here)
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):
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
}
You can look at the user-agent header ($_SERVER['HTTP_USER_AGENT']
). For example, an iPhone user-agent looks like:
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3
An Windows XP one might look like:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
And so on. See a simple script here. You can also use get_browser()
(you'll need browscap though)
Note that this header can easily be faked and thus shouldn't be used for anything mission critical.
Reliably? You can't.
Unreliably? $_SERVER['HTTP_USER_AGENT']
often contains OS information.
function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}
// check if we have a number
if ($version==null || $version=="") {$version="?";}
return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
var_dump(getBrowser());
see details enter link description here
get_browser function will parse $_SERVER['HTTP_USER_AGENT'] for you. You need browsecap.ini though.
Rather than implementing this yourself I'd try using an existing piece of software like Browser.php that parses the user agent string for you. There's so many little things you need to take into account it's best to use a specialized library rather than trying to learn all these little platform/browser incongruities yourself.
精彩评论