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;
} 

Recent search terms:

  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1887 OvgUxpr9qO2nSY7xfBBfqrMyKKrUz4ZjLCjLo80gRf5IrfuQNKFPcR5BNsfVUgiM 7aaec94f496207709c48efc5ef2ebb32c8e11a85&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1835 ZGxufH30J8MJ_MDW7_eEPqzMtLH9KZq8nNB_Q8AcDKcWl3j5lklju-sDkwa1khhA c51e1f08e2bd267b995c9b636faf220c76be524c&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1835 VU0AO2XBnXAZrUdIOOnGU2LZptOW-7sGYnKlwIMZoY93wSAEclh_oMkdv0xeid7I 0f7712dd02c0521c3ad075f1e38c65ae67edd337&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1834 YWPd6nk1TKRPyaD2KYzhY3vhcm33KArMxGEK5EKawp74L6YgrBpLmXcb8bk1ZsyF 598a17e7ea1e6490ea3819e29dceeb9381b5980b&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1834 yWMjtu9FxUjapcTSnvf3GkyziJNN0Tj-ADNw5g3fGL7aoCPZMkd83Zm47YHbFUf0 7cd3391ba476a87bf582495ae36c3f0714bd10b7&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1833 Q-Vn_lOOhEuFJ9uqXKk8SrqN5AqsYEoTQgasbPXyzlq892UO9llKE_liyi4PGpDe e7c900021e88f31490a3f8898d48740cee42f2c3&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1832 wuLXh-z3vcfe4cqn1U0PZWAzAmXzIi_PGAEzTx1CXeYLraUYa_TgMuIbbQiIiVLr f63126dcb5cb627cd661ef541127a72e52ff5ca2&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1832 -2rfkitlUPVnfpX13YnAVa6mxFlAJNvpGyiGR-tQOH0_d8HQTQkhzE_wHI3X7jiC 0795a796d7e16e688fa0883a222c708b00b08d27&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 Zb78OiuhC5bCxv-QxVfqR1bDaFQvcm6oRuuv6WMwc4lXxrUYCwpP9sSLlw0w8dfv 62ff1f4ca462067d49dfe64d72112b5c7018f832&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 WfdxAawySrrrB3jifkZMq6BnVkHVrshYg2hxD6Lfcn_c80w12de47xRrID0cW4Di 6128459a98446919efc1195afb37715f1b6d1a67&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

1 Comment

Leave a Reply