Need help with mobile detection PHP script


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

$tablet_browser = 0;
$mobile_browser = 0;
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
if (preg_match('/(up.browser||mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
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_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'wapr','webc','winw','winw','xda ','xda-');
if (in_array($mobile_ua,$mobile_agents)) {
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
    //Check for tablets on opera mini alternative headers
    if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
/*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';
}   */
Who is Participating?

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

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.

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


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

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

////please write this code to index page at top//////




<html xmlns="" xml:lang="en" lang="en">
<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">
<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>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%" id="main">
    <td valign="top">
<script type="text/javascript">
  AC_FL_RunContent('codebase',',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','', 'movie','flash' );
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase=",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="" />

punkrawkdude99Author Commented:
bump.... still need help with this
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 = "";  }

6) I wouldn't use .htaccess for this.
punkrawkdude99Author Commented:
not what i was hoping for...but good answer none the less
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

From novice to tech pro — start learning today.