Home  >  Article  >  Backend Development  >  Detect mobile devices with PHP

Detect mobile devices with PHP

WBOY
WBOYforward
2024-02-28 12:01:01413browse

php Xiaobian Yuzai today introduces how to use PHP to detect mobile devices. With the popularity of mobile devices, responsive design of websites has become particularly important. By detecting the type of device used by users to access the website through PHP, we can provide customized content and layout for different devices to improve user experience. This article will introduce how to use PHP to detect the user's device type and provide better mobile adaptation for your website.


To detect mobile devices using the mobiledetect class in php

we can use the class named Mobile Detect Lightweight PHP class to detect mobile devices in PHP. It can also detect tablet devices. The library uses certain Http headers and user-agent strings to detect mobile devices. We can download the library using Composer using the following command.

<code><code class="language-bash hljs" data-lang="bash"><span style="display:flex;"><span>composer require mobiledetect/mobiledetectlib
</span></span></code></code>

This library provides various methods, such as isMobile(), isTablet(), is<strong class="keylink">iOS</strong>() to detect various mobile environments. We can create objects of class Mobile_Detect() and use these methods.

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

The following example will detect whether a web page is accessed from a mobile device. The output section below shows what happens when a web page is opened from a PC. We can inspect the element by right-clicking on the web page to find Responsive Design Mode. There we can select a different mobile device and refresh the script. When we select 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.

Sample code:

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require_once</span> <span style="color:#ba2121">"vendor/autoload.php"</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#19177c">$detect</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Mobile_Detect;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span> ( <span style="color:#19177c">$detect</span><span style="color:#666">-></span><span style="color:#7d9029">isMobile</span>() ) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Mobile device detected"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Mobile device not detected"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

Output:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Mobile device not detected
</span></span></code></code>

Using the HTTP_USER_AGENT and preg_match() functions in PHP Detecting Mobile Devices

We can use the string HTTP_USER_AGENT to get information about the website visited by the user's browser. We will use the $_SERVER superglobal variable and the string as the array element. Superglobal variables contain information about the NetworkServer. We will create a custom collection of user-agent strings found on mobile devices. We can then use the preg_match() function to check if these match the browser the current user is browsing. As new supported mobile devices are released, collections of user-agent strings can be added manually. An updated list of user agent string collections can be 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 a collection of strings as the first argument. Use the $user_agent variable as the second parameter. Finally, use the if-else condition to display the message accordingly.

Here, we opened the web page from the iPhone. So the user agent string matches the set. This way, we can detect mobile devices in PHP.

Sample code:

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#19177c">$user_agent</span> <span style="color:#666">=</span> <span style="color:#19177c">$_SERVER</span>[<span style="color:#ba2121">"HTTP_USER_AGENT"</span>];
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span>(preg_match(<span style="color:#ba2121">"/(<strong class="keylink">Android</strong>|<strong class="keylink">WEB</strong>os|avant<strong class="keylink">Go</strong>|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"</span>,<span style="color:#19177c">$user_agent</span> ))
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"mobile device detected"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">else</span>{
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"mobile device not detected"</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></code>

Output:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Mobile device detected
</span></span></code></code>

The above is the detailed content of Detect mobile devices with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete