首页> 实战笔录 >PHP开发笔记 >PHP PHP
PHP怎么判断访问的设备是ios还是安卓?
作者:小萝卜 2020-09-19 【 PHP 】 浏览 987
简介在开发的时候我们有时候需要判断客户端是用的安卓还是ios访问的网站,怎么判断呢?是否微信访问的?
废话不多说,直接上代码:
//请求这个方法
public function decide(){
$type = self::get_device_type();
if($type == 'ios'){
header('location:https://itunes.apple.com/us/app/id1450386883?l=cn&ls=1&mt=8');
exit();
}elseif($type == 'android'){
$wx = self::is_wx();
if($wx=='no'){
//不是微信浏览器
exit();
}else{
//是微信访问的
exit();
}
}else{
//。。。
exit();
}
}
//调取该方法判断是ios还是安卓
static public 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;
}
//调用该方法判断时候是微信浏览器访问的
static public function is_wx(){
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'MicroMessenger') === false) {
// 非微信浏览器禁止浏览
return 'no';
} else {
// 微信浏览器,允许访问
return 'yes';
}
}
很赞哦! (0)