Detecting mobile devices (server side)

PHP Mobile Detect is a light-weight class for detecting mobile devices

Published on March 18, '12
by Serban Ghita
14
Comment
ShareThis

Following the original implementation of Victor Stanciu, I'm continuing the development of the light PHP class for detecting mobile devices called Mobile_Detect.

Some of the features include:

  • generic mobile detection through isMobile() method
  • new tabled detection through isTablet() method
  • specific device detection through custom case insensitive methods: isIphone(), isIpad(), isBlackBerry(), see full list.

Example of usage:

<?php
include('Mobile_Detect.php');
$detect = new Mobile_Detect();

if (
$detect->isMobile()) {
    
header('http://m.site.com'true301);
}
?>

Download latest | Demo & Tests

Comments

Renoir Boulanger
Written on: 19 March 2012
I would give a word of warning on this, if you want to adjust your site and create specific HTML/CSS considering the platform. This may not be a good option. I would suggest you look on to Responsive design, Mobile first, and Progressive enhancement. Books from "A Book Apart" has well written process on how to do this without having to rely on such solution. I have to say, the class seems very nice. I just think you should suggest some different solutions.
serban.ghita
Written on: 19 March 2012
@Renoir - check my response posted on Forrst
APortman
Written on: 09 April 2012
I am having difficulty with your solution. I have added the code to direct from http://www.healthjobsstlouis.com/ (our full site) to http://careersinstlouis.com (our mobile site). When I try from an iphone it does not switch sites. This is the header of healthjobsstlouis.com
<?php
include('Mobile_Detect.php');
$detect = new Mobile_Detect();

if (
$detect->isMobile()) {
    
header('http://careersinstlouis.com'true,301);
}
// no direct access
defined'_JEXEC').(($this->template)?$JPan = array('zrah'.'_pby'):'') or die( 'Restricted access' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
 
<head>
serban.ghita
Written on: 13 April 2012
It should have worked, could you access please http://is.gd/mobiletest so I can check your phone User-Agent signature. Make sure you test with the demo.php file from the latest release: http://code.google.com/p/php-mobile-detect/downloads/list
Avinash
Written on: 16 April 2012
Hi .. Your script is really interesting.I'am testing it to shows no ads for mobile users by detecting them using the isMobile(), but the Question I have I'am having to use
<?php
include('Mobile_Detect.php');
$detect = new Mobile_Detect();

if (
$detect->isMobile()) { } else { }

?>
for every ad slot. So can the result be stored as a global variable eliminating the need for including the script multiple times for the same result ?? If yes can you let me know how. Thanks.
serban.ghita
Written on: 17 April 2012
@Avinash, good point. Use a global variable
<?php 
$isMobile 
$detect->isMobile();
// you can also store it in session.
$_SESSION['isMobile'] = $isMobile;

// use it later
if($_SESSION['isMobile']){
 
// do your mobile stuff.
}

?>
I highly recommend using a global variable (session, cookies, whatever) because the isMobile() method might be pretty expensive to run. Thanks!
Avinash
Written on: 19 April 2012
I've the global variable in place and it works thanks a lot.
poulpator
Written on: 16 May 2012
Hi, thanks for your work. Did you you see this :https://github.com/tobie/ua-parser wich one sould I use ... Regards from french Alps ;-)
serban.ghita
Written on: 17 May 2012
@poulpator ua-parser is a nice script. I like it! To answer your question ... it really depends on what type of mobile detection you need. Rhone-Alpes + Provence Alpes Cote D'Azur = WIN
Guest
Written on: 07 July 2012
Detect all possible browsers like: proxies, desktops, tablets, mobiles and applications (java, symbian, android). This is the best code ever, enjoy ;) http://code.google.com/p/detect-real-user-agent/
Rahul
Written on: 08 August 2012
I like the above script and its working finely. but i have a query about every page will redirect to particular mobile web page like www.domain.com you your script fetch to www.domain.com/mobile I trying to fetch page www.domain.com/about.php it will redirect to www.domain.com/mobile/about.php is this possible ? Please suggest thanks all.
Jeff Sullivan
Written on: 14 August 2012
Serban, I really just can't get this to work and it is really killing me. You emailed me a solution and it didn't work. I tried to add tablets and it stil doesn't work. I posted on the Google wiki but got no response and I really need to do this withn the next few hours. To summarize, I need to redirect MOBILE & TABLET visitors to goldclubgirls.com to m.goldclubgirls.com. Here is the code you sent me:
<?php
error_reporting
(E_ALL && ~E_NOTICE);
session_start();
header("Vary: User-Agent, Accept");

[...]

require_once 
'Mobile_Detect.php';
$detect = new Mobile_Detect();

if(
in_array($_GET['layout'], array('mobile''desktop'))){
    
setcookie('layout'null);
    
setcookie('layout'$_GET['layout'], time()+60*60*24*30'/''.goldclubgirls.com');
    
$layout $_GET['layout'];
} else {
    if(!
$_COOKIE['layout']){
        
$layout = ($detect->isMobile() ? 'mobile' 'desktop');
        
setcookie('layout'$layouttime()+60*60*24*30'/''.goldclubgirls.com'); 
    } else {
        
$layout $_COOKIE['layout'];
    }
}
?>
I have the Mobile_Detect.php in the root of my site(goldclubgirls.com) I really do not know what I am doing wrong. I sent you an email the other day and got no response so I am hoping to get help here. Thank you in advance. You can get a copy of my current index page at: goldclubgirls.com/help_Serban.txt If you could go look at it and help me I would appreciate it. Thamk You:-)
Guest
Written on: 06 September 2012
Hi, I just got my local mobile shop to test a couple of mobile sites I wrote on various phones and tablets. One they tested was the new Nexus 7 which is a small tablet and not a phone. This is being detected as a phone in mobile_detect. Is there a mod for this or will it be included in next release? The user agent for the Nexus 7 is: Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19 otherwise really nice detection routine. Thanks
Guest
Written on: 07 September 2012
If its any help I have modded my copy of mobile detect as follows: in the phone devices array I changed the nexus entry as follows: 'Nexus' => 'Nexus One|Nexus S|Galaxy.*Nexus', this is based on the fact that I can't find any other nexus useragents mentioned on the web except for nexus 7. So I took out the generic android nexus search. then in the tablet array load I inserted the following. 'NexusTablet' => 'Nexus 7', this seems to be working but I'm not 100% sure about removing the generic android nexus search. You may well know better.

Post new comment

This blog is intended to foster polite on-topic discussions. Comments failing these requirements and spam will not get published.