Home > php教程 > php手册 > php判断用户浏览器类型是否为微信浏览器

php判断用户浏览器类型是否为微信浏览器

WBOY
Release: 2016-06-02 09:13:54
Original
1659 people have browsed it

判断用户浏览器类型我们只要使用php中的 $_SERVER["HTTP_USER_AGENT"]就可以获取到了,但这样获取到的是非常原始的数据,我们要判断类型需要加上一段strpos($_SERVER['HTTP_USER_AGENT'],"MicroMessenger "就可以了,具体步骤如下所示.

PHP方法:利用PHP的"_SERVER"数组"HTTP_USER_AGENT"项,获取该页面的用户代理的信息,来完成这个工作,代码如下:

<?php echo $_SERVER["HTTP_USER_AGENT"];?>
Copy after login

winows/chrome 输出结果:

Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/30.0.1599.69 Safari/537.36

而【ios7/weixin】下输出结果:

Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B554a MicroMessenger/5.0.3

在字符串中可以看出"MicroMessenger "为微信浏览器版本,所以可以使用"MicroMessenger"参数,来判断访问来源是否为"微信浏览器",代码如下:

if(strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],"MicroMessenger ")) 
{ 
    echo "Hello Weixin";//phprm.com
}
Copy after login

Javascript方法,代码如下:

function isweixin(){ 
    var ua = navigator.userAgent.toLowerCase(); 
    if(ua.match(/MicroMessenger/i)=="micromessenger") { 
        return true; 
    } else { 
        return false; 
    } 
}
Copy after login

                           


教程网址:

欢迎收藏∩_∩但请保留本文链接。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template