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:
- X-Wap-Profile
- Profile
- 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.
精彩评论