Wednesday, October 19, 2011

PHP script for redirecting to mobile website

Put following content on top of your index page and change the $mobileSite value. Then the page will be redirected to the specified website when accessing from a mobile device.
$mobileSite = "m.mysite.com";

$mobileDevices = array('vnd.wap.xhtml+xml', 'sony', 'symbian', 'nokia',
'samsung', 'mobile', 'windows ce', 'epoc', 'opera mini', 'nitro', 'j2me',
'midp-', 'cldc-', 'netfront', 'mot', 'up.browser', 'up.link', 'audiovox',
'blackberry', 'ericsson', 'panasonic', 'philips', 'sanyo', 'sharp', 'sie-',
'portalmmm', 'blazer', 'avantgo', 'danger', 'palm', 'series60', 'palmsource',
'pocketpc', 'smartphone', 'rover', 'ipaq', 'au-mic', 'alcatel', 'ericy',
'vodafone', 'wap1', 'wap2', 'teleca', 'playstation', 'lge', 'lg-', 'iphone',
'android', 'htc', 'dream', 'webos', 'bolt', 'nintendo');
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
foreach($mobileDevices as $mobile){
if(strpos($userAgent, $mobile)) {
header("Location: $mobileSite");
}
}

echo "My website";

No comments:

Post a Comment