Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Check if the page run on Mobile or Desktop browser [duplicate]

Tags:

html

css

php

mobile

In my PHP page I should display two different text contents according to whether the page run under mobile or desktop browser. Is there a way to perform this control in PHP?

like image 923
stacker Avatar asked Mar 05 '13 16:03

stacker


People also ask

How do I check if a website is open in mobile or desktop PHP?

An easy way to detect a mobile or desktop device in PHP is to check if the HTTP user agent contains the word “mobile”. $ua = strtolower($_SERVER["HTTP_USER_AGENT"]); $isMob = is_numeric(strpos($ua, "mobile"));

How to Detect if mobile device PHP?

Use HTTP_USER_AGENT and the preg_match() Function to Detect Mobile Devices in PHP. For example, create a variable $user_agent and store $_SERVER["HTTP_USER_AGENT"] in it. Then use the preg_match() function to match the user-agent string. Use the collection of strings as the first parameter.


2 Answers

There is a very nice PHP library for detecting mobile clients here: http://mobiledetect.net

Using that it's quite easy to only display content for a mobile:

include 'Mobile_Detect.php'; $detect = new Mobile_Detect();  // Check for any mobile device. if ($detect->isMobile()){    // mobile content } else {    // other content for desktops } 
like image 89
alphadevx Avatar answered Sep 22 '22 09:09

alphadevx


I used Robert Lee`s answer and it works great! Just writing down the complete function i'm using:

function isMobileDevice(){     $aMobileUA = array(         '/iphone/i' => 'iPhone',          '/ipod/i' => 'iPod',          '/ipad/i' => 'iPad',          '/android/i' => 'Android',          '/blackberry/i' => 'BlackBerry',          '/webos/i' => 'Mobile'     );      //Return true if Mobile User Agent is detected     foreach($aMobileUA as $sMobileKey => $sMobileOS){         if(preg_match($sMobileKey, $_SERVER['HTTP_USER_AGENT'])){             return true;         }     }     //Otherwise return false..       return false; } 
like image 36
Felipe Balduino Cassar Avatar answered Sep 26 '22 09:09

Felipe Balduino Cassar