2017-02-03 9 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. Disclosure 나는 51 도의 계열사가 아니며 단지 우리 프로젝트 중 하나에서 활용합니다. –

+0

PHP는 헤더에 문자열을 가지고, JS 더 많은 액세스 권한이 있는지 모르겠다. – nogad

+0

이것은 의견을 근거로 한 질문입니다. 나는 VTC를 쓸 것이지만 나는 리뷰 대기열에서 나의 것을 사용했다. 17k 사용자는 더 잘 알아야합니다! – miken32

답변

1

나는 전통적으로 서버 측에서 브라우저를 감지합니다.

브라우저가 응용 프로그램이로드되기 전에 렌더링하는 HTML을 변경하는 이유는 무엇입니까? 이것이 사용 사례에 가장 적합한 서버 측 감지 기능을 사용하는 이유입니다.

내가 내가 내가 희망이 도움이

<?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를 사용하여 확인하는 것이 좋습니다.