PHP, JS: How to detect Safari browser


PHP

/**
 * Copyright: www.tutorialspots.com
 **/
function isSafari($ua) {
    return preg_match("/^((?!chrome).)*safari/i",$ua) && stripos($ua,' version/')!==false && stripos($ua,'mqqbrowser')===false;
}

Example:

$uas = array( 
    'Mozilla/5.0 (Windows NT 6.1 WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
    ,'Mozilla/5.0 (iPad CPU OS 10_1_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.79 Mobile/14B100 Safari/602.1'
    ,'Mozilla/5.0 (iPhone CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4'
    ,'Mozilla/5.0 (iPhone CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mercury/7.2 Mobile/9B206 Safari/7534.48.3'
    ,'Mozilla/5.0 (iPhone U CPU iPhone OS 7_0_4 like Mac OS X en-US) AppleWebKit/534.35 (KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.35 Puffin/3.11505IP Mobile'
    ,'Mozilla/5.0 (iPad CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Coast/2.0.5.71150 Mobile/11B651 Safari/7534.48.3'
    ,'Mozilla/5.0 (Macintosh Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36'
    ,'Mozilla/5.0 (iPad CPU OS 7_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) YaBrowser/14.2.1700.0 Mobile/11D167 Safari/9537.53'
    ,'Mozilla/5.0 (iPad CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/6.0 MQQBrowser/4.1.1 Mobile/11A501 Safari/7534.48.3'
    ,'Mozilla/5.0 (iPad U CPU OS 7_1 like Mac OS X ru iPad3,6) AppleWebKit/534.46 (KHTML, like Gecko) UCBrowser/2.4.0.367 U3/1 Safari/7543.48.3'
);
 
foreach($uas as $ua){  
    echo $ua.": ".(isSafari($ua)?'Safari':'Not Safari');
    echo "<br />";
}

Result:

Mozilla/5.0 (Windows NT 6.1 WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2: Safari
Mozilla/5.0 (iPad CPU OS 10_1_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.79 Mobile/14B100 Safari/602.1: Not Safari
Mozilla/5.0 (iPhone CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4: Not Safari
Mozilla/5.0 (iPhone CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mercury/7.2 Mobile/9B206 Safari/7534.48.3: Not Safari
Mozilla/5.0 (iPhone U CPU iPhone OS 7_0_4 like Mac OS X en-US) AppleWebKit/534.35 (KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.35 Puffin/3.11505IP Mobile: Not Safari
Mozilla/5.0 (iPad CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Coast/2.0.5.71150 Mobile/11B651 Safari/7534.48.3: Not Safari
Mozilla/5.0 (Macintosh Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36: Not Safari
Mozilla/5.0 (iPad CPU OS 7_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) YaBrowser/14.2.1700.0 Mobile/11D167 Safari/9537.53: Not Safari
Mozilla/5.0 (iPad CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/6.0 MQQBrowser/4.1.1 Mobile/11A501 Safari/7534.48.3: Not Safari
Mozilla/5.0 (iPad U CPU OS 7_1 like Mac OS X ru iPad3,6) AppleWebKit/534.46 (KHTML, like Gecko) UCBrowser/2.4.0.367 U3/1 Safari/7543.48.3: Not Safari

Javascript

function isSafari(ua) {
    return /^((?!chrome).)*safari/i.test(ua) && ua.toLowerCase().indexOf(' version/')>-1 && ua.toLowerCase().indexOf('mqqbrowser')==-1;
} 

1 Comment

Leave a Reply