2013-04-06 8 views
8

私は最近、ユーザーがMacにいるかどうかを識別するシンプルで簡単なPHPスクリプトを試してみるために、多くのGoogle検索を行ってきました。オペレーティングシステムがMacであるかどうかを判断

「コントロール」または「コマンド」と言っているキーボードショートカットがあるかどうかをユーザーに伝えたい場合に使用します。コンピュータがMacの場合、ブラウザやその他何かを知る必要はありません。ここで

は私が求めているものの概要である可能である:

if (operating_system == Mac) 
{ 
    echo "command"; 
} 
else 
{ 
    echo "control"; 
} 

答えて

16

ページを作成します。

<?php 
$user_agent = getenv("HTTP_USER_AGENT"); 

if(strpos($user_agent, "Win") !== FALSE) 
$os = "Windows"; 
elseif(strpos($user_agent, "Mac") !== FALSE) 
$os = "Mac"; 
?> 

identifier.phpその後、あなたのサイトのヘッダにそれを含めます。 Windowsの携帯電話の場合は

<?php 
if($os === "Windows") 
{ 

} 
elseif($os === "Mac") 
{ 

} 
?> 

編集:あなたはこのようにそれを使用することができます。その後

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone os') > 0) { 
    $mobile_browser = 1; 
    } 
+1

ありがとうございました!これは完全に機能しました。 :) – totallyuneekname

+0

ニースは助けてくれたことを知っています:) –

+0

このコードは、すべてのiOSデバイスを、Mac OS Xのような "Mac OS X"を含むMacの原因として検出します。代わりに、 "Mac"ではなく "Macintosh"を確認する必要があります。 – kremalicious

1

は、PHPに組み込まれているget_browser()機能を試してみてください。

$browser = get_browser(null, true); 
echo "Platform: " . $browser["platform"] . "\n"; 
+0

感謝早く答える!真実を伝えるために、私はかなり新しいphpです。あなたは私のためにifステートメントフォーマットにこれを入れても構いませんか?ありがとうございました – totallyuneekname

+0

注:これは、browscapがphp.iniで設定され、browscapも存在する場合にのみ機能します。私の経験では、これはあまり一般的ではありません。 – Ares

+0

@ user2180108 get_browser()は完全な機能のためにbrowsecap.iniが必要です。これはあなたの管理入力を必要とします.. get_browserについて読むために提供されたURLを使用し、ここに行きます:browsecap.iniのためのhttp://tempdownloads.browserscap.com/ –

1

Detect exact OS version from browser あたりとしてhttp://ellislab.com/codeigniter/user-guide/libraries/user_agent.html

を見てください短い答え:まったくできません。

長い答え:

あなたが持っているすべては、通常、OS名とバージョンが含まれているHTTP User-Agentヘッダ、内の情報です。

通常、Mac OSとLinuxで動作しているブラウザは、正確なOSを特定するのに十分な情報を送信します。たとえば、ここに私のUser-Agentヘッダです:

のMozilla/5.0(; U; Linuxのx86_64で、X11 EN-US; RV:1.9.0.7)のGecko/2009030423のUbuntu/8.10(勇敢)のFirefox/3.0。 7

私はUbuntu 8.10 Intrepid Ibexを実行していることがわかります。

そして、ここにはあります私のMacBook Proの上で何のFirefoxとSafari 4ベータ版のレポート:

のMozilla/5.0(Macintosh版; U;インテルのMac OS X 10.5; EN-US; RV:1.9.0.7)のGecko/2009021906 Firefoxの/ 3.0.7

のMozilla/5.0(Macintosh版; U;インテルのMac OS X 10_5_6; EN-US)のAppleWebKit/528.16(KHTML、ヤモリのような)バージョン/ 4.0のSafari/528.16

のWindows一方、ブラウザは通常、特定のパッケージではなくOSバージョンのみを報告します(Pro、ビジネスなど):

Mozilla/5。0(Windowsは、U; Windows NTの5.1; EN-US; RV:XXX)のGecko/20041107 Firefoxの/ XX

//これはあなた

$uagent = $_SERVER['HTTP_USER_AGENT'] . "<br/>"; 

function os_info($uagent) 
{ 
    // the order of this array is important 
    global $uagent; 
    $oses = array(
     'Win311' => 'Win16', 
     'Win95' => '(Windows 95)|(Win95)|(Windows_95)', 
     'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)', 
     'Win98' => '(Windows 98)|(Win98)', 
     'Win2000' => '(Windows NT 5.0)|(Windows 2000)', 
     'WinXP' => '(Windows NT 5.1)|(Windows XP)', 
     'WinServer2003' => '(Windows NT 5.2)', 
     'WinVista' => '(Windows NT 6.0)', 
     'Windows 7' => '(Windows NT 6.1)', 
     'Windows 8' => '(Windows NT 6.2)', 
     'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)', 
     'OpenBSD' => 'OpenBSD', 
     'SunOS' => 'SunOS', 
     'Ubuntu' => 'Ubuntu', 
     'Android' => 'Android', 
     'Linux' => '(Linux)|(X11)', 
     'iPhone' => 'iPhone', 
     'iPad' => 'iPad', 
     'MacOS' => '(Mac_PowerPC)|(Macintosh)', 
     'QNX' => 'QNX', 
     'BeOS' => 'BeOS', 
     'OS2' => 'OS/2', 
     'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)' 
    ); 
    $uagent = strtolower($uagent ? $uagent : $_SERVER['HTTP_USER_AGENT']); 
    foreach ($oses as $os => $pattern) 
     if (preg_match('/' . $pattern . '/i', $uagent)) 
      return $os; 
    return 'Unknown'; 
} 
echo os_info($uagent); 
1

残念ながら、これができない本当にも役立ちます完了しました。ユーザーエージェントスニッフィングがここに壊れている理由に行き過ぎずにあなたの質問に答えるために


は、あなたがやりたいだろういくつかのコードは次のようになりますため

$mactest = strpos($_SERVER["HTTP_USER_AGENT"], 'Macintosh') ? true : false; 

if($mactest) { 
    do something 
} else { 
    do something else 
} 
5
<?php 

    function getBrowserOS() { 

     $user_agent  = $_SERVER['HTTP_USER_AGENT']; 
     $browser  = "Unknown Browser"; 
     $os_platform = "Unknown OS Platform"; 

     // Get the Operating System Platform 

      if (preg_match('/windows|win32/i', $user_agent)) { 

       $os_platform = 'Windows'; 

       if (preg_match('/windows nt 6.2/i', $user_agent)) { 

        $os_platform .= " 8"; 

       } else if (preg_match('/windows nt 6.1/i', $user_agent)) { 

        $os_platform .= " 7"; 

       } else if (preg_match('/windows nt 6.0/i', $user_agent)) { 

        $os_platform .= " Vista"; 

       } else if (preg_match('/windows nt 5.2/i', $user_agent)) { 

        $os_platform .= " Server 2003/XP x64"; 

       } else if (preg_match('/windows nt 5.1/i', $user_agent) || preg_match('/windows xp/i', $user_agent)) { 

        $os_platform .= " XP"; 

       } else if (preg_match('/windows nt 5.0/i', $user_agent)) { 

        $os_platform .= " 2000"; 

       } else if (preg_match('/windows me/i', $user_agent)) { 

        $os_platform .= " ME"; 

       } else if (preg_match('/win98/i', $user_agent)) { 

        $os_platform .= " 98"; 

       } else if (preg_match('/win95/i', $user_agent)) { 

        $os_platform .= " 95"; 

       } else if (preg_match('/win16/i', $user_agent)) { 

        $os_platform .= " 3.11"; 

       } 

      } else if (preg_match('/macintosh|mac os x/i', $user_agent)) { 

       $os_platform = 'Mac'; 

       if (preg_match('/macintosh/i', $user_agent)) { 

        $os_platform .= " OS X"; 

       } else if (preg_match('/mac_powerpc/i', $user_agent)) { 

        $os_platform .= " OS 9"; 

       } 

      } else if (preg_match('/linux/i', $user_agent)) { 

       $os_platform = "Linux"; 

      } 

      // Override if matched 

       if (preg_match('/iphone/i', $user_agent)) { 

        $os_platform = "iPhone"; 

       } else if (preg_match('/android/i', $user_agent)) { 

        $os_platform = "Android"; 

       } else if (preg_match('/blackberry/i', $user_agent)) { 

        $os_platform = "BlackBerry"; 

       } else if (preg_match('/webos/i', $user_agent)) { 

        $os_platform = "Mobile"; 

       } else if (preg_match('/ipod/i', $user_agent)) { 

        $os_platform = "iPod"; 

       } else if (preg_match('/ipad/i', $user_agent)) { 

        $os_platform = "iPad"; 

       } 

     // Get the Browser 

      if (preg_match('/msie/i', $user_agent) && !preg_match('/opera/i', $user_agent)) { 

       $browser  = "Internet Explorer"; 

      } else if (preg_match('/firefox/i', $user_agent)) { 

       $browser  = "Firefox"; 

      } else if (preg_match('/chrome/i', $user_agent)) { 

       $browser  = "Chrome"; 

      } else if (preg_match('/safari/i', $user_agent)) { 

       $browser  = "Safari"; 

      } else if (preg_match('/opera/i', $user_agent)) { 

       $browser  = "Opera"; 

      } else if (preg_match('/netscape/i', $user_agent)) { 

       $browser  = "Netscape"; 

      } 

      // Override if matched 

       if ($os_platform == "iPhone" || $os_platform == "Android" || $os_platform == "BlackBerry" || $os_platform == "Mobile" || $os_platform == "iPod" || $os_platform == "iPad") { 

        if (preg_match('/mobile/i', $user_agent)) { 

         $browser = "Handheld Browser"; 

        } 

       } 

     // Create a Data Array 

      return array(
       'browser'  => $browser, 
       'os_platform' => $os_platform 
      ); 

    } 


    $user_agent  = getBrowserOS(); 

    $device_details = "<strong>Browser: </strong>".$user_agent['browser']."<br /><strong>Operating System: </strong>".$user_agent['os_platform'].""; 

    print_r($device_details); 

    echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT'].""); 

    ?> 
3
function getUserOS(){ 
$osList = array 
(
    'Windows 7' => 'windows nt 6.1', 
    'Windows Vista' => 'windows nt 6.0', 
    'Windows Server 2003' => 'windows nt 5.2', 
    'Windows XP' => 'windows nt 5.1', 
    'Windows 2000 sp1' => 'windows nt 5.01', 
    'Windows 2000' => 'windows nt 5.0', 
    'Windows NT 4.0' => 'windows nt 4.0', 
    'Windows Me' => 'win 9x 4.9', 
    'Windows 98' => 'windows 98', 
    'Windows 95' => 'windows 95', 
    'Windows CE' => 'windows ce', 
    'Windows (version unknown)' => 'windows', 
    'OpenBSD' => 'openbsd', 
    'SunOS' => 'sunos', 
    'Ubuntu' => 'ubuntu', 
    'Linux' => '(linux)|(x11)', 
    'Mac OSX Beta (Kodiak)' => 'mac os x beta', 
    'Mac OSX Cheetah' => 'mac os x 10.0', 
    'Mac OSX Puma' => 'mac os x 10.1', 
    'Mac OSX Jaguar' => 'mac os x 10.2', 
    'Mac OSX Panther' => 'mac os x 10.3', 
    'Mac OSX Tiger' => 'mac os x 10.4', 
    'Mac OSX Leopard' => 'mac os x 10.5', 
    'Mac OSX Snow Leopard' => 'mac os x 10.6', 
    'Mac OSX Lion' => 'mac os x 10.7', 
    'Mac OSX (version unknown)' => 'mac os x', 
    'Mac OS (classic)' => '(mac_powerpc)|(macintosh)', 
    'QNX' => 'QNX', 
    'BeOS' => 'beos', 
    'OS2' => 'os/2', 
    'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)' 
); 

$useragent = $_SERVER['HTTP_USER_AGENT']; 
$useragent = strtolower($useragent); 

foreach($osList as $os=>$match) { 
    if (preg_match('/' . $match . '/i', $useragent)) { 
     break; 
    } 
    else 
    { 
     //$os = "Not automatically detected.<br />$useragent"; 
     $os = "unknown"; 
    } 
} 

return $os; 
} 
関連する問題