Here is how to detect a mobile device using php.

nerveosu

macrumors member
Original poster
Sep 17, 2001
85
0
For anyone who wants to use it..

This is a way to detect with php if a visitor to your web site is using a mobile device browser.. it works. If you have a mobile device that this doesn't detect, all you have to do to add it is to find a unique bit of identifying information in your device's user agent and stick it in the list of mobile devices in this code.

------------

<?php


/* detect mobile device*/
$ismobile = 0;
$container = $_SERVER['HTTP_USER_AGENT'];

// A list of mobile devices
$useragents = array (

'Blazer' ,
'Palm' ,
'Handspring' ,
'Nokia' ,
'Kyocera',
'Samsung' ,
'Motorola' ,
'Smartphone',
'Windows CE' ,
'Blackberry' ,
'WAP' ,
'SonyEricsson',
'PlayStation Portable',
'LG',
'MMP',
'OPWV',
'Symbian',
'EPOC',

);

foreach ( $useragents as $useragents ) {
if(strstr($container,$useragents)) {
$ismobile = 1;
}
}



if ( $ismobile == 1 ) {
echo "<p>mobile device</p>";
echo $_SERVER['HTTP_USER_AGENT'];
}

?>
 

qwertydesign

macrumors newbie
Nov 18, 2008
1
0
there is also a web service out there that provides GEO location as well as handset information real time.

There are a few out there that do this, but we use handsetdetection.com when developing new sites for clients.
 

web_god61

macrumors regular
May 14, 2004
111
1
wouldn't
Code:
in_array($_SERVER['HTTP_USER_AGENT'], $useragents)
make more sense than a for loop?
 

serbang

macrumors newbie
May 30, 2012
1
0
re

Even if this topic is a bit old, the mobile detection problem is rather new. Try Mobile_Detect class from http://code.google.com/p/php-mobile-detect/

Code:
Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
 

justindocanto

macrumors newbie
Jun 11, 2012
2
0
I wrote an easy way to detect mobile browsers & devices in PHP

I wrote this script to detect a mobile browser or device in PHP.

The code detects a user based on the user-agent string by preg_match()ing. It has 100% accuracy on all current mobile devices and I'm currently updating it to support more mobile devices as they come out. The code is called isMobile and is as follows:

Code:
function isMobile() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
You can use it like this:

Code:
// Use the function
if(isMobile())
    // Do something for only mobile users
else
    // Do something for only desktop users
To redirect a user to your mobile site, I would do this:

Code:
// Create the function, so you can use it
function isMobile() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

// If the user is on a mobile device, redirect them
if(isMobile())
    header("Location: http://m.yoursite.com/");
Let me know if you have any questions and good luck!