Link to home
Start Free TrialLog in
Avatar of punkrawkdude99
punkrawkdude99

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of enrmmoba
enrmmoba

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of punkrawkdude99
punkrawkdude99

ASKER

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
bump.... still need help with this
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.
not what i was hoping for...but good answer none the less