2017-02-03 11 views
-1

これまではJavaScriptを使用してクライアント側でブラウザの詳細検出を行っています。ブラウザの詳細検出のベストプラクティス - クライアント側(JavaScriptを使用)またはサーバー側(PHPおよびWURFLを使用)で行う必要があります

data['browserDetails'] = { 
    'browser'  : getBrowser(), 
    'majorVersion' : getBrowserMajorVersion(), 
    'appCodeName' : navigator.appCodeName, 
    'appName'  : navigator.appName, 
    'appVersion' : navigator.appVersion, 
    'language'  : navigator.language, 
    'platform'  : navigator.platform, 
    'cookieEnabled' : navigator.cookieEnabled, 
}; 

// http://stackoverflow.com/a/9851769 
function getBrowser() 
{ 
    var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; 
    if (isOpera) { 
     return 'Opera'; 
    } 

    var isFirefox = typeof InstallTrigger !== 'undefined'; 
    if (isFirefox) { 
     return 'Firefox'; 
    } 

    var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification); 
    if (isSafari) { 
     return 'Safari'; 
    } 

    var isIE = /*@[email protected]*/false || !!document.documentMode; 
    if (isIE) { 
     return 'IE'; 
    } 

    var isEdge = !isIE && !!window.StyleMedia; 
    if (isEdge) { 
     return 'Edge'; 
    } 

    var isChrome = !!window.chrome && !!window.chrome.webstore; 
    if (isChrome) { 
     return 'Chrome'; 
    } 

    return 'Unknown'; 
} 

// http://stackoverflow.com/a/38080051 
function getBrowserMajorVersion() 
{ 
    var ua= navigator.userAgent, tem, 
      M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; 

    if (/trident/i.test(M[1])) { 
     tem= /\brv[ :]+(\d+)/g.exec(ua) || []; 
     return (tem[1] || ''); 
    } 

    if (M[1]=== 'Chrome') { 
     tem= ua.match(/\b(OPR|Edge)\/(\d+)/); 
     if (tem!= null) return tem[2]; 
    } 

    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?']; 
    if ((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]); 
    return M[1]; 
} 

私はかつてPHP + WURFLを使用して、サーバー側コードでブラウザの詳細検出を実行しようとしていました。私の非常に限られたテストケースでは、サーバー側の検出がクライアント側の検出よりも優れている(正確な)ことは示されていません。

私はブラウザの詳細検出のベストプラクティスは何ですか?クライアント側(JavaScriptを使用して)またはサーバー側(PHPとWURFLを使用)で行うべきかどうかについては、精度に関してはどうでしょうか?

ありがとうございました。

+1

潜在的な興味:https://51degrees.com/device-detection。開示私は51度に所属していません。私たちはプロジェクトの1つでそれを活用しています。 –

+0

PHPはヘッダーに文字列しか持っていないので、JSにアクセスできるかどうかは分かりません。 – nogad

+0

これは意見に基づく質問です。私はVTCを使っていましたが、私はレビューキューで私のものを使ってきました。 17kユーザーはよりよく知っている必要があります! – miken32

答えて

1

私は伝統的にサーバー側のブラウザを検出します。

ブラウザがアプリケーションを読み込む前に何をレンダリングするかを検出する理由これが、私の使用事例でサーバ側の検出が最も効果的だった理由です。私はこれが役に立てば幸い、私はその後、私はこの

<?php $mobilehtml="<a href=''></a>"; 

$ismobile = check_user_agent('mobile'); 
if($ismobile) { 
    echo "$mobilehtml"; 
} 
else { 

    echo "$browserhtml"; 
} 
?> 

に似た関数への呼び出しを作成して、長い時間前に

<?php 
/* USER-AGENTS This is my custom function to add contact logic... 
================================================== */ 
function check_user_agent ($type = NULL) { 
    $user_agent = strtolower ($_SERVER['HTTP_USER_AGENT']); 
    if ($type == 'bot') { 
      // matches popular bots 
      if (preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent)) { 
        return true; 
        // watchmouse|pingdom\.com are "uptime services" 
      } 
    } else if ($type == 'browser') { 
      // matches core browser types 
      if (preg_match ("/mozilla\/|opera\//", $user_agent)) { 
        return true; 
      } 
    } else if ($type == 'mobile') { 
      // matches popular mobile devices that have small screens and/or touch inputs 
      // mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America 
      // detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here 
      if (preg_match ("/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent)) { 
        // these are the most common 
        return true; 
      } else if (preg_match ("/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent)) { 
        // these are less common, and might not be worth checking 
        return true; 
      } 
    } 
    return false; 
} ?> 

を拾ったこのPHPの関数を使用する

2
  1. user-agentプロキシ(またはいくつかの洗練されたファイアウォールとプロキシの組み合わせ)で変更または削除できます。
  2. JavaScriptがサーバーからロードされます。

ですので、javascriptを使用してクライアント側で確認することをおすすめします。

関連する問題