Sunday, September 27, 2009

Redirecting to a Mobile Site Using PHP

In this day and age, a lot of people are browsing the web with their mobile devices. If any business wants to stay with the trend, they will have to have a mobile website, along with their regular site. Some companies or individuals may have the knowledge to design their own web pages, but may not be too familiar with PHP. Then alas, you have come to the right page.

I am going to show you a simple PHP function that will redirect your mobile viewers from your regular site to your (hopefully) low bandwidth intensive “Mobile Site”. With each generation of wireless networks, the providers are developing better coverage, wider coverage, and faster transfer speeds. Also with smart phones becoming more commonplace by the minute, the need for mobile sites will become less and less. Until the need is gone, the code I will show you in this article will come in handy. So let’s begin, and create the code to get your mobile viewers to the right place.
First there are a few things to keep in mind:

1. Your server must support PHP

2. Your page must have the .php extension.

Locate the page you will be directing mobile users from (ie. index.php) and go and paste this script at the very top of the page, even above the <!DOCTYPE line.

<?php


if(mobileredirect()) header("Location:http://yoursite.com/mobile/mobilepage.php");


function mobileredirect(){


if(preg_match("/Trident/i",$_SERVER["HTTP_USER_AGENT"])) return false;


if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) return true;


if(preg_match("/wap\.
\.wap/i",$_SERVER["HTTP_ACCEPT"])) return true;


if(isset($_SERVER["HTTP_USER_AGENT"])){


if(preg_match("/Creative\ AutoUpdate/i",$_SERVER["HTTP_USER_AGENT"])) return false;


if(preg_match("/MSIE/i",$_SERVER["HTTP_USER_AGENT"])) return false;


$uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "iphone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");

foreach($uamatches as $uastring){

if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) return true;

}

}

return false;

}

?>

The only change left for you to make the location (Location:http://yoursite.com/mobile/mobilepage.php) on the second line to your mobile page.

This code even works for the newer phones like Apple’s iphone and Google’s G1. However, if there is a phone that you know of that this code does not work for, just add the phone to the array and you should be fine.

Well, there you have it. Paste the code, change the location, and away your site will go into the mobile frontier.

Download PHP code here.

No comments:

Post a Comment