Need help with mobile detection PHP script

Hi,

I had a PHP file in place that would re-route incoming traffic to a different website when people used a mobile browser

up until one of the more recent IOS updates it worked on iphones and ipads

currently it only works on android... can someone please help me correct or change the code so that it works with the newer IOS version

<?php
$tablet_browser = 0;
$mobile_browser = 0;
 
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $tablet_browser++;
}
 
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/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','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['HTTP_USER_AGENT']),'opera mini') > 0) {
    $mobile_browser++;
    //Check for tablets on opera mini alternative headers
    $stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
    if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
      $tablet_browser++;
    }
}
 
/*if ($tablet_browser > 0) {
   // do something for tablet devices
  // echo 'is tablet';
}
else if ($mobile_browser > 0) {
   // do something for mobile devices
   echo 'is mobile';
}
else {
   // do something for everything else
  // echo 'is desktop';
}   */
 
?>
Mobile-Detect.class.php
LVL 1
punkrawkdude99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

enrmmobaCommented:
i know you are looking for a PHP solution but i think its more reliable to use Javascript / CSS to detect mobile browsing.

function detectmob() { 
 if( navigator.userAgent.match(/Android/i)
 || navigator.userAgent.match(/webOS/i)
 || navigator.userAgent.match(/iPhone/i)
 || navigator.userAgent.match(/iPad/i)
 || navigator.userAgent.match(/iPod/i)
 || navigator.userAgent.match(/BlackBerry/i)
 || navigator.userAgent.match(/Windows Phone/i)
 ){
    return true;
  }
 else {
    return false;
  }
}

Open in new window


or

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

Open in new window


or you could check the screen width x height
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
punkrawkdude99Author Commented:
i have an index.php that points to the mobile detection php and depending the answer it either loads a flash site or a mobile.php site...

how do i use the above javascipt with a index.php ... let me attach that as well so you can see how it works

<?php
////please write this code to index page at top//////

include_once("Mobile_Detect.class.php");

if($mobile_browser>0){
include("mobile_site.php");

exit;
}

?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Whit Industries - Santa Clarita Valley's Premiere Computer And Network Specialists</title>
<meta name="description" content="The perfect computer and network solution for small to medium businesses.">
<meta name="keywords" content="Whit Industries, Small to Medium Business, Computer Solutions, Network Solutions, Technology, Computer Help, Network Help, IT Infrastructure Design, Business Technology Strategies, Network Security, Wireless Networks, Day-to-Day IT Services">
</head>
<script src="inc/active.js" type="text/javascript"></script>
<script src="inc/resize.js" type="text/javascript"></script>
<script language="JavaScript" src="active.js" type="text/javascript"></script>
<body bgcolor="#FFFFFF" LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%" id="main">
  <tr>
    <td valign="top">
<script type="text/javascript">
  AC_FL_RunContent('codebase','http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','scale','noscale','width','100%','height','100%','id','flash','align','top','src','flash','quality','high','bgcolor','#ffffff','name','flash','allowscriptaccess','sameDomain','allowFullScreen','true','pluginspage','http://www.macromedia.com/go/getflashplayer', 'movie','flash' );
</script>
<noscript>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="flash" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="true" />
<param name="movie" value="flash.swf" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param name="salign" value="t" />
<param name="bgcolor" value="#FFFFFF" />
<embed src="flash.swf" quality="high" scale="noscale" salign="t" bgcolor="#FFFFFF" width="100%" height="100%" name="flash" align="middle" allowScriptAccess="sameDomain"allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</noscript>
</td>
  </tr>
</table>

</body>
</html>
index.php
0
punkrawkdude99Author Commented:
bump.... still need help with this
0
Adrian HealCommented:
Looks like your PHP code is reading HTTP_USER_AGENT to determine the device that has requested the web page. It is checking the user agent strings. New devices have different User agent strings. You may just need to find an up to date list and apply the new ones. The list of ID strings is easy to spot in your code. I notice you do not have    iphone    in your list of user agent strings (add to the 1st preg_match). On my websites i also included   mobile  (i would add this to the 2nd preg_match).

OTHER options for detecting a mobile:-

1) Continue to use your PHP script with the and need for continual updates (more current lists of what things to look for in HTTP_USER_AGENT are available online.

2) Write code in JavaScript to do the same thing as the PHP script (if you and your team would prefer javascript and have a reason to not use PHP). Again you will need to update the ID strings that need checking to see if the request came from small screen device.

3) Put a button on your website leading to a mobile version. Perhaps even a welcome page that offers people the mobile or normal website. THE EASY WAY.

4) Use media queries to check the page width and apply an appropriate style. There are plenty of resources for media queries online. This allows you to apply different CSS depending on device.

5) JavaScript can be used to test screen width (like a media query):
 if (screen.width<800) {window.location = "http://m.mydomain.com";  }

6) I wouldn't use .htaccess for this.
0
punkrawkdude99Author Commented:
not what i was hoping for...but good answer none the less
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.