Mobile detect with PHP

Tuesday 10th, Nov, 2015 | #PHP #Mobile

View More Demo Download

Today it is essential to detect whether a user is visiting a page from a cell phone mobile or a desktop PC to give you a better browsing experience

A simple way is use $_SERVER["HTTP_USER_AGENT"] php variable

Simple example detect mobile device php 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"]);
}

if(isMobile()){
//ACTION FOR MOBILE
}else {
//ACTION FOR NO MOBILE
};

Other way is use Mobile Detect

Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

How to use Mobile detect PHP?


// Include and instantiate the class.
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
 
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
 
}
 
// Any tablet device.
if( $detect->isTablet() ){
 
}
 
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){
 
}
 
// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
 
}
 
if( $detect->isAndroidOS() ){
 
}

Warning: include(view/Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.  ): failed to open stream: No such file or directory in /home/wdrfree/public_html/view/blog_detalle.php on line 274 Warning: include(view/Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.  ): failed to open stream: No such file or directory in /home/wdrfree/public_html/view/blog_detalle.php on line 274 Warning: include(): Failed opening 'view/Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.  ' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/wdrfree/public_html/view/blog_detalle.php on line 274