开发者

Detecting BlackBerry and Iphone Browsers in JavaScript or PHP

i want to redirect peaple who goes in my site to other page if they are using a mobile browser with a php o java scrip开发者_高级运维t. Thanks


Very easy will be to parse USER-AGENT string or get_browser() PHP function. Try :

echo $_SERVER['HTTP_USER_AGENT'];
var_dump(get_browser(null, true));

Every browser is sending own HTTP_USER_AGENT string.

Mobile devices USER_AGENT list

For completed solution take a look to bavotasan page or just google.


In addition to checking the User-Agent header, you may also want to check the X-Wap-Profile and Profile headers, since some third-party browsers may not send a correct User-Agent header (they may be spoofing an IE or Firefox header). The order that I like to check the headers, when looking for mobile clients, is:

  1. X-Wap-Profile
  2. Profile
  3. User-Agent


In javascript:

<script type="text/javascript">
    $(document).ready(function () {
        var deviceAndroid = "android";
        var deviceIphone = "iphone";
        var deviceBlackberry = "blackberry";
        var uagent = navigator.userAgent.toLowerCase();
        DetectDevice();


        function DetectDevice() {
            if (uagent.search(deviceAndroid) > -1) {}
            else if (uagent.search(deviceIphone) > -1) {}
            else if (uagent.search(deviceBlackberry) > -1) {}
            else { }
        }
    });
</script>


You need to check the User-Agent header:

if (preg_match("/(BlackBerry|(iP(hone|od))/i", $_SERVER['HTTP_USER_AGENT'])) ) {
    ...
}


The $_SERVER['HTTP_USER_AGENT'] contains word "BlackBerry" or "iPhone" respectively. "iPod" too if it's iPod Touch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜