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;
}

?>

97 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. Hi, I do think this is a great website. I stumbledupon it 😉 I’m going to return once again since i have book-marked it. Money and freedom is the best way to change, may you be rich and continue to help others.

  4. Next time I read a blog, I hope that it doesn’t disappoint me just as much as this particular one. I mean, Yes, it was my choice to read, however I actually thought you’d have something interesting to talk about. All I hear is a bunch of whining about something you could possibly fix if you weren’t too busy searching for attention.

  5. Hello there! This article couldn’t be written much better! Reading through this post reminds me of my previous roommate! He always kept talking about this. I am going to send this post to him. Pretty sure he’ll have a great read. Many thanks for sharing!

  6. This is a very good tip particularly to those new to the blogosphere. Short but very precise info… Appreciate your sharing this one. A must read article!

  7. I’m extremely pleased to discover this website. I need to to thank you for ones time for this wonderful read!! I definitely savored every part of it and I have you bookmarked to check out new stuff in your site.

  8. Can I simply say what a comfort to uncover someone that truly understands what they’re talking about over the internet. You certainly realize how to bring a problem to light and make it important. More and more people must look at this and understand this side of the story. I was surprised you aren’t more popular given that you certainly possess the gift.

  9. Hi there! I could have sworn I’ve been to your blog before but after going through some of the posts I realized it’s new to me. Nonetheless, I’m certainly pleased I stumbled upon it and I’ll be book-marking it and checking back often.

  10. You ought to be a part of a contest for one of the highest quality sites on the web. I’m going to highly recommend this website!

  11. Oh my goodness! Incredible article dude! Thank you so much, However I am having difficulties with your RSS. I don’t understand why I am unable to subscribe to it. Is there anyone else having identical RSS issues? Anyone that knows the answer will you kindly respond? Thanx.

  12. The unique wood structure was closed and demolished in 1961 as a consequence
    of safety considerations and changed by the winding concrete staircase nonetheless in use at this time.
    According to legend, it acquired its present name from a local actual property agent who
    placed a sign at the bottom of the staircase, in reference to both the Bible story a couple of ladder to heaven and the gorgeous
    views at the top of the structure.

  13. An impressive share! I have just forwarded this onto a friend who was doing a little research on this. And he actually ordered me breakfast simply because I found it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to talk about this issue here on your site.

Leave a Comment