標籤:ice androi strpos dev type function 種類 turn cti
因為工作需要我們需要知道是什麼樣了使用者訪問了我網站了,現在的行動裝置種類多了,下面我們一起來看小編整理的一段php判斷iPhone、iPad、Android、PC裝置的例子.
注意:本代碼的PC系統為Windows
原理是判斷瀏覽器提交的USER AGENT
<?php
//擷取USER AGENT
$agent = strtolower($_SERVER[‘HTTP_USER_AGENT‘]);
//做判斷
$is_pc = (strpos($agent, ‘windows nt‘)) ? true : false;
$is_iphone = (strpos($agent, ‘iphone‘)) ? true : false;
$is_ipad = (strpos($agent, ‘ipad‘)) ? true : false;
$is_android = (strpos($agent, ‘android‘)) ? true : false;
if($is_pc)
{
echo "這是PC";
}
if($is_iphone)
{
echo "這是iPhone";
}
if($is_ipad)
{
echo "這是iPad";
}
if($is_android)
{
echo "這是Android";
}
如果你只判斷是否為iphone裝置可以如下來進行操作,代碼如下:
<?php
function get_device_type() {
$agent = strtolower($_SERVER[‘HTTP_USER_AGENT‘]);
$type = ‘other‘;
if(strpos($agent, ‘iphone‘) || strpos($agent, ‘ipad‘) )
{
$type = ‘ios‘;
}
if(strpos($agent, ‘android‘))
{
$type = ‘android‘;
}
return $type;
}
?>
PHP判斷iPhone、iPad、Android、PC裝置的方法