Redirect mobile users to a mobile friendly page with php

After allot of trial and error I ended up coming up with some code that will redirect users to a mobile friendly page, this method won’t really be useful in about 5 years because modern browsers like safari allow full page viewing but this will get you by until then. Remember this code will have to change constantly if you want to stay up to date with the latest hand held devices and mime types.

To get started all you have to do is place the following code in the stop of your php page. If your loading an application you can post this below your application just don’t output any data before the code is executed. To get started making mobile friendly pages you can purchase your domain names and hosting by clicking here.


$mobile_browser = '0';if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
strtolower($_SERVER['HTTP_USER_AGENT']))){
$mobile_browser++;
}

if((strpos(strtolower($_SERVER[‘HTTP_ACCEPT’]),’application/vnd.wap.xhtml+xml’)>0) or
((isset($_SERVER[‘HTTP_X_WAP_PROFILE’]) or isset($_SERVER[‘HTTP_PROFILE’])))){
$mobile_browser++;
}

$mobile_ua = strtolower(substr($_SERVER[‘HTTP_USER_AGENT’],0,4));
$mobile_agents = array(
‘w3c ‘,’acs-‘,’alav’,’alca’,’amoi’,’audi’,’avan’,’benq’,’bird’,’blac’,
‘blaz’,’brew’,’cell’,’cldc’,’cmd-‘,’dang’,’doco’,’eric’,’hipt’,’inno’,
‘ipaq’,’java’,’jigs’,’kddi’,’keji’,’leno’,’lg-c’,’lg-d’,’lg-g’,’lge-‘,
‘maui’,’maxo’,’midp’,’mits’,’mmef’,’mobi’,’mot-‘,’moto’,’mwbp’,’nec-‘,
‘newt’,’noki’,’oper’,’palm’,’pana’,’pant’,’phil’,’play’,’port’,’prox’,
‘qwap’,’sage’,’sams’,’sany’,’sch-‘,’sec-‘,’send’,’seri’,’sgh-‘,’shar’,
‘sie-‘,’siem’,’smal’,’smar’,’sony’,’sph-‘,’symb’,’t-mo’,’teli’,’tim-‘,
‘tosh’,’tsm-‘,’upg1′,’upsi’,’vk-v’,’voda’,’wap-‘,’wapa’,’wapi’,’wapp’,
‘wapr’,’webc’,’winw’,’winw’,’xda’,’xda-‘);

if(in_array($mobile_ua,$mobile_agents)){
$mobile_browser++;
}
if (strpos(strtolower($_SERVER[‘ALL_HTTP’]),’OperaMini’)>0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’windows’)>0) {
$mobile_browser=0;

}

//change this to your mobile friendly page
if($mobile_browser>0){
header( “Location: http://www.yourwebsite.com/mobile/” ) ;
} else {
// do something else
}

$ua = $HTTP_USER_AGENT;
if (stristr($ua, “Windows CE”) or stristr($ua, “AvantGo”) or stristr($ua,
“Mazingo”) or stristr($ua, “Mobile”) or stristr($ua, “T68”) or stristr($ua,
“Syncalot”) or stristr($ua, “Blazer”) or stristr($ua, “NetFront”))
{
$DEVICE_TYPE=”MOBILE”;
}

if (isset($DEVICE_TYPE) and $DEVICE_TYPE==”MOBILE”)
{
//change this to your mobile friendly page
header( “Location: http://www.yourwebsite.com/mobile/” ) ;
exit;
}

?>

13 thoughts on “Redirect mobile users to a mobile friendly page with php”

  1. Code worked seamless. If you canpy and paste the code remember to place the characters <? at the beginning of this as they are truncated.

    Regards,

    Ron Bobarsky
    Product Development
    Custom Molding and Coating

  2. Excellent post it will be quite excited to open a website on the mobile just like on the computer i will try definitely this code as well i have learn more than enough from this post please keep it up.

  3. Hello dear friend, I would like to offer placement of your link (or links) on different platforms of the internet such as: forums, blogs, comments and much more. . .

    Increase your Visibility Boost Your Seo Rank – Get Organic Traffic From Google. Ranking in Google isn’t hard. All you need is a healthy number of backlinks from referring domains that have authority and trust in Google’s eyes.

    This Backlinks Service Benefits:

    1. Easily get Google rankings

    2. Get a lot of traffic from Google

    3. You can earn from the website in different ways

    4. Increase Domain Authority (DA)

    Quality guaranteed !

    PRICE – 20$

    WebSite – https://goo.su/ZUHZ

Leave a Comment