How to Detect Mobile Device in PHP

Subodh Poudel Feb 02, 2024
  1. Use the mobiledetect Class to Detect Mobile Devices in PHP
  2. Use HTTP_USER_AGENT and the preg_match() Function to Detect Mobile Devices in PHP
How to Detect Mobile Device in PHP

We will introduce some methods to detect mobile devices in PHP.

Use the mobiledetect Class to Detect Mobile Devices in PHP

We can use a lightweight PHP class called Mobile Detect to detect mobile devices in PHP. It also detects tablet devices. The library uses certain HTTP headers and user-agent strings to detect mobile devices. We can download the library using the composer with the following command.

composer require mobiledetect/mobiledetectlib

The library provides various methods like isMobile(), isTablet(), isIOS() to detect various mobile enviornments. We can create an object of the Mobile_Detect() class and use these methods.

For example, download the library in the project directory using the composer command above. Next, require the file autoload.php using the require_once function. The file is located inside the vendor directory. Next, create an object $detect of the Mobile_Detect() class. Then, use the function isMobile() in an if condition. Inside the if block, display the message Mobile device detected and display the message Mobile device not detected in the else block.

The example below will detect if the webpage is accessed from a mobile device. The output section below displays the case when the webpage is opened from a PC. We can find Responsive Design Mode going to Inspect Element with a right-click on the webpage. There, we can choose different mobile devices and refresh the script. When we choose a mobile device, the output will change to Mobile device detected. In this way, we can use the Mobile Detect class to detect mobile devices in PHP.

Example Code:

require_once "vendor/autoload.php";

$detect = new Mobile_Detect;

if ( $detect->isMobile() ) {
echo "Mobile device detected";
}
else {
echo "Mobile device not detected";
}
?>

Output:

Mobile device not detected

Use HTTP_USER_AGENT and the preg_match() Function to Detect Mobile Devices in PHP

We can use the string HTTP_USER_AGENT to get the information about the user’s browser to visit the website. We will use the $_SERVER superglobal variable and the string as an array element. The superglobal variable contains information about the webserver. We will create a custom collection of user-agent strings that are found in mobile devices. Then, we can check if those match with the browser the current user is browsing using the preg_match() function. The collection of the user-agent strings can be manually added along with the release of new supported mobile devices. A list of an updated collection of user-agent strings is found here

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. Use the $user_agent variable as the second parameter. Finally, display the message accordingly using the if-else condition.

Here, we opened the webpage from an iPhone. Therefore the user-agent string matches the collection. In this way, we can detect the mobile device in PHP.

Example Code:

$user_agent = $_SERVER["HTTP_USER_AGENT"];
if(preg_match("/(android|webos|avantgo|iphone|ipod|ipad|bolt|boost|cricket|docomo|fone|hiptop|opera mini|mini|kitkat|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i",$user_agent ))
{
echo "mobile device detected";
}
else{
echo "mobile device not detected";
}

Output:

Mobile device detected
Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn