We focus on High Quality but Free WordPress themes.
3 ways to detect mobile devices in your website by using PHP
image from hammer
Usually, we need provide two versions of our websites to our client, one is pc version, another is mobile website. If you’d like to provide mobile version of website, you should detect your client is from mobile devices firstly.
Today, I will introduce you 3 ways to detect mobile devices in your website by using PHP:
Firstly (Recommend)
//detect mobile devicesfunction is_mobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte"); $is_mobile = false; foreach ($mobile_agents as $device) { if (stristr($user_agent, $device)) { $is_mobile = true; break; } } return $is_mobile; }//it’s very easy to useif( is_mobile() ){ Your Code }
secondly
<?php
// check if wap
function check_wap(){
if(stristr($_SERVER['HTTP_VIA'],"wap")){
return true;
}elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){
return true;
}elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){//check USER_AGENT
return true;
}else{
return false;
}
}
thirdly: Use lightweight PHP class Mobile_Detect (Recommend)
Usage
Include and instantiate the class:
<?php include 'Mobile_Detect.php'; $detect = new Mobile_Detect();
Basic usage, looking for mobile devices or tablets:
<?php
if ($detect->isMobile()) {
// Any mobile device.
}
<?php
if($detect->isTablet()){
// Any tablet device.
}
Check for a specific platform with the help of the magic methods:
<?php
if($detect->isiOS()){
// Code to run for the Apple's iOS platform.
}
<?php
if($detect->isAndroidOS()){
// Code to run for the Google's Android platform.
}
There are more usages, please refer here.
Hope these help.
