Home » Tutorials » Mobile Web Development » Detect Your Visitor’s Mobile Browser

Detect Your Visitor’s Mobile Browser

Detect Your Visitor’s Mobile Browser

The most significant challenge for mobile web designer is to deal with varying screen size. The mobile technology is always changing, such as the screen resolution, size, browser and etc. Luckily for web designers, because they able to detect the visitor’s mobile browser and serve the appropriate mobile version for visitor. The following will show you PHP method of detecting visitor’s browser.

Detect Mobile Browser By Using PHP

First of all, we need to detect the browser type of your visitors using, then based on the information we redirect them to the specific URL. In order to get the information of the browser, we need to get the information of “User Agent“. “User Agent” is like a identity of the browser, it tells the name, version and etc of the browser. So how are we going to retrieve the information from “User Agent”? Fortunately, we can access it by using the PHP super global variable array called $_SERVER[]. Please see below:


and the code above will output as something like:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4>

Next we need to use another PHP function called preg_match() to perform a regualr expression match and redirect your visitor to the specific URL according to the browser type. Following indicates that if your visitor’s browser is Android then will redirect to “mobile-android.html”

$user_agent = $_SERVER['HTTP_USER_AGENT'];

//we perform a regular expression search in user agent
//the "i" after the pattern delimiter indicates a case-insensitive search
if(preg_match("/Android/i", $user_agent)) {

//if is Android, then redirect to the following url
header("Location: mobile-android.html");


That’s it!

Now you are able to detect your visitor’s mobile browser by using PHP. If you want to display specific stylesheet for specific devices, please check out onlyWebPro previous tutorial:

Android, iOS Development Tutorials