• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

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|up.link|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';
}   */
  • 3
1 Solution
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
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="http://www.w3.org/1999/xhtml" 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','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' );
<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" />

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 = "http://m.mydomain.com";  }

6) I wouldn't use .htaccess for this.
punkrawkdude99Author Commented:
not what i was hoping for...but good answer none the less
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now