1.jquery作法 : 使用navigator.userAgent
function get_device(){
var ua = navigator.userAgent;
var checker = {
iphone: ua.match(/(iPhone|iPod|iPad)/),
android: ua.match(/Android/)
};
if (checker.android){
return "android";
}
else if (checker.iphone){
return "iphone";
}
else {
return "web";
}
}
2.PHP作法,使用mobile detect
參考說明:http://mobiledetect.net/
composer 下載指令:
composer require mobiledetect/mobiledetectlib
PHP範例:
$detect = new Mobile_Detect; $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer'); $scriptVersion = $detect->getScriptVersion(); echo "deviceType:".$deviceType."<br>"; echo "scriptVersion:".$scriptVersion."<br>"; echo "Iphone:".$detect->isIphone()."<br>"; echo "isAndroidOS".$detect->isAndroidOS()."<br>"; echo "isiOS".$detect->isiOS()."<br>"; // 注意 // $detect->isIphone() // $detect->isAndroidOS() // $detect->isiOS() // 如果不符合則不會回傳任何資料,符合就會回傳1
沒有留言:
張貼留言