当我们采用php作为服务数据端口,为移动端,pc端提供数据接口的时候,可能会要求记录接口访问的来源是来之哪里.
例如:生成订单,可能就需要记录,以便做一些数据分析,为运营和推广提供一些数据支持.
代码:
function order_source()
{
$useragent = strtolower($_SERVER["HTTP_USER_AGENT"]);
// iphone
$is_iphone = strripos($useragent,'iphone');
if($is_iphone){
return 'iphone';
}
// android
$is_android= strripos($useragent,'android');
if($is_android){
return 'android';
}
// 微信
$is_weixin= strripos($useragent,'micromessenger');
if($is_weixin){
return 'weixin';
}
// ipad
$is_ipad = strripos($useragent,'ipad');
if($is_ipad){
return 'ipad';
}
// ipod
$is_ipod = strripos($useragent,'ipod');
if($is_ipod){
return 'ipod';
}
// pc电脑
$is_pc= strripos($useragent,'windows nt');
if($is_pc){
return 'pc';
}
return 'other';
}
根据自己的需求 ,来做一些特殊处理比如区分浏览器,这里就不做介绍了.
自己 echo $_SERVER["HHTP_USER_AGENT"]出来看一下就明了.
tip:
$res = strripos($str,$findstr) 此方法是在 $str 字符串中匹配 $findstr 最后一次出现的位置,匹配不到返回 false ,
也就是说假如匹配的字符串在首位出现就会返回0,那么 if($res){...} 就无法正确返回,
比较正确的写法应该是 if($res === 0 || $res > 0){...} ,(3个等于号是不仅是值相等还要数据类型相等),
当然我们已经知道哪些关键字是不会出现在首位,所以上面那种写法就可以了.
如果觉得《php判断是不是iphone访问 php基于http协议访问 判断访问来源iphone android 微信浏览器 pc电脑...》对你有帮助,请点赞、收藏,并留下你的观点哦!