Programming PHP How to detect Mobile Device in PHP and jQuery

How to detect Mobile Device in PHP and jQuery

How to detect Mobile Device in PHP - Techbriefers

In our daily development life, there are many times we need to code something specifically for a certain device category. To accomplish such a task, we need to detect the device. The topic I am going to discuss today is a simple answer to the question- How to detect Mobile Devices in PHP and jQuery. This topic will include the codes on How to detect Mobile Devices in PHP and jQuery.

You can see on many websites that they have a desktop version and also a different simplified mobile version of the same website. This makes it simpler for mobile internet users. But to achieve this we have to detect a mobile device or a device category on which the website is opened.

Why do we need to detect a mobile device or platform on the server side?

Detection of mobile-device on the server is the best way. It will prevent loading unnecessary content that can make the website heavier for mobile devices. Content that is not relevant for mobile devices should not even be called on those devices. This will make a website faster by loading only the required and suitable content that we want to show on a mobile device.

There is another way to detect the mobile device, ie. by using jQuery/JavaScript. I will explain that later. Firstly, I will tell you how to identify a mobile device in PHP with examples.

Although responsive web designing is a way to look a website completely suitable for mobile devices by using CSS media queries. However, it will not prevent loading unnecessary content to load on the mobile device that is needed only for the desktop version of the website.

How to detect a mobile device in PHP

To detect a mobile device on a PHP-powered website, we can use PHP GLOBAL Variable $_SERVER. ‘HTTP_USER_AGENT’ index of $_SERVER variable contains information about the user end. Furthermore, I will mention the code to detect specific devices in PHP.

Code to detect a mobile device in PHP

I am creating a function is_mobile() to detect whether the device is a mobile device or not. This function returns true if the device is a mobile device, and false otherwise.

Now calling is_mobile() function to detect the mobile device.

How to detect a mobile device platform/ OS in PHP

Check for the Android device in PHP
Code to check the Android device in PHP

Similarly, you can check the BlackBerry device in PHP

Code to check the BlackBerry device in PHP

Furthermore, you can use $_SERVER[‘HTTP_USER_AGENT’] to detect the browser of the user too. For example – in the below code, I am detecting for Chrome browser.

Code to check the Chrome browser in PHP

$_SERVER['HTTP_USER_AGENT'] has different values for different platforms, browsers, and web servers.

Leave a Reply