How to Check If a Visitor to your Website is from a Mobile or PC in PHP?

Ever want to check if a particular visitor to your website is from a mobile device or desktop using PHP?┬áthen here’s how you can do that. $_SERVER in PHP, a super global variable or an array that contains information about the server’s hostname, remote IP address, HTTP user agent, script locations, commandline arguments, web server details etc…It means, we are just going to use $_SERVER array to find the HTTP_USER_AGENT of a visitor. To do that, let’s create a simple function named ‘is_mobile‘ and use ‘preg_match‘ to check for the patterns such as iPhone, Android, Mobile, Windows Phone etc…and return true upon match.

Create a function is_mobile as below:

php $_SERVER tutorial

function is_mobile() {
 if (preg_match("/Mobile|Android|BlackBerry|iPhone|Windows Phone/", $_SERVER['HTTP_USER_AGENT'])) {
 return true;

Now, in a IF cond:

 //Mobile specific code
 //Desktop specific code

Using WordPress? You can simply copy and paste is_mobile() into ‘functions.php‘ file and call it from anywhere inside the WordPress directory.

Related Content
vps germany
Disclaimer: The content published in this article is the views of the author only. Techglimpse does not gurantee accuracy, completness or validity. If you believe the content on this post violates your copyright, please send us a mail for removal. Read more.