Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m Advertisement # Mobile Browser Detection Difficulty:BeginnerLength:MediumLanguages: When creating mobile web apps like our wordpress theme series, it is important to be able to detect a mobile browser and serve the appropriate mobile version. Many readers requested information on how to do this in the comments, so here it is! First, we will cover a wordpress solution, then a PHP solution, and then a popular existing solution. By the end of this tutorial, you will be exposed to the whole spectrum of options and well equipped to handle this scenario! ### In MyTouch With wordpress, we have a lot of tools out there already to help us, including plugins. One terrific plugin out there is MobilePress. MobilePress allows wordpress users to automatically use a mobile theme for mobile users, but more importantly, the plugin allows custom mobile themes to be uploaded and utilized. This will allow us to upload the theme we created and serve it to only the mobile browsers that visit the site. Upload MyTouch In order to use a second mobile theme for a site, you first need to define the directory where you will put your mobile themes in MobilePress. If you click on MobilePress on the wordpress admin side panel, you will see an input field for this directory. By default, the directory is wp-content/mobile-themes. We will stick with that. Upload your theme to this directory, and then click on MobilePress > Themes and select our theme. Click on the theme to enable it for the default mobile browser. Then click the Default Browser dropdown. Select iPhone browser and click change. Then click on the myTouch theme again to select it for the iOS Safari browser. The Default Browser is for all mobile devices besides the iPhone. iPhone Testing Android Testing Now we can serve the appropriate theme to all users! But what if you're not running WordPress? ### Detecting Mobile Browsers Using PHP There is no PHP function like is_mobile() or anything (although there is a get_browser() function, but that's a different article!). What we need to do is detect the browser type. Then, based on that information, we will redirect the user to a specific URL. Every browser has a little information attached to it that the server detects. This is called the user agent. The user agent is basically the name of each browser. No, not just the first name, like Internet Explorer or Safari, but the entire name that tells the browser's story. This includes OS version, platform, etc. As you'd probably guess, we can do a lot with a user agent. We could direct a user based on his or her operating system or even just their version of operating system. This is how some websites, for example a software download page, will automatically give you the correct version of their software. How to Get Browser Type It turns out that we can access the browser's user agent through a super global variable array called$_SERVER[]. The server array allows us to gain a lot of information, but what we want is the browser agent:

This variable will give us a lot of information, but will vary from browser to browser. Here is mine:

Can you tell what browser I'm using? Safari? Nope, Chrome. Sorry, I gave you a tricky one! Chrome's user agent is a bit tricky as it is based on Apple's WebKit. Now that we have this variable, what are we going to do with it?

The Procedure

Before we go any further, we need to think about what exactly we want to do. We want to detect the user agent using the global variable above, but then what? A simple IF statement to check if the user agent above matches a list of user agents would work. If it does, we can simply redirect the user to the mobile version.

Regular Expressions:

The problem with the user agent is it's so unique to each person's setup. There are many hundreds of different versions of browsers, many different operating systems, and multiple versions of every one of those systems and platforms. There are literally thousands of possible combinations that a user agent could be. It would simply be inefficient, non-inclusive, and inflexible if we tried to exactly match strings the old fashion way. Instead, we will use the smart and agile way. We will use regular expressions. Now, if you cringed at the mention of the phrase "regular expression," that is okay. You understand the ninja-like power that regular expressions give us. This is not a tutorial about regular expressions. You need a whole series for that. Like this one by the great Jeffrey Way. Luckily, we don't need anything complicated for this situation, so it won't be too difficult.

preg_match()

Preg_match() is a PHP regular expression function that searches for a certain pattern like a string in a larger string. Preg_match is very capable, but we will only focus on the basics. This is all you need to worry about:

The nice thing about this is it uses regular expressions so we have a ton of flexibility. If we want to search case insensitive we can:

If we want to search for whole word matches, we can do that too:

Putting this together with an IF statement

We now need to put this together with an IF statement to check for results. This is pretty simple:

As you can see, the above only searches for iPhones. When an iPhone is detected, the script will execute the echo statement.

Extending the Search

The more comprehensive we get, the more phone devices we will cover, but we really are just creating a giant laundry list of mobile devices here. Luckily, we don't need to repeat the function. That's what I'll show you next. By simply adding a pipe character (i.e. | ), we can create essentially an OR statement:

Although we account for a huge majority of mobile browsers by just listing those three devices, the rest of the small remaing percentage of mobile usage is spread out among many other phones. For now, instead of listing out hundreds of phones, we will leave it at that. You will see later in this tutorial how we will address this problem.

Redirect

Once the browser is detected as mobile, we need to redirect to a mobile page. We can simply use the PHP header function for this. All you have to do is specify a location. Here is the complete script:

The exit just makes sure we don't execute anything else before the redirect.

### Integrating DetectMobileBrowser.com

Now that you know the theory behind browser detection and redirection, we can talk about other options. DetectMobileBrowser.com is a great resource. Not only do they provide a web service, but they also provide an open source solution for almost any language you'd need it in. The best part though is they took the time to create a much larger pattern of mobile browser potential strings to search through. This makes the list much more thorough.

### Web Service

If you don't mind having a 3rd party link, using the simple web service is a great solution. You can direct users to the easy to generate link, and the site will send them to a different URL depending on if it is a mobile browser or not. It's very simple in structure. http://detectmobilebrowser.com/desktopulr|mobileurl

If you don't want to use a 3rd party link, you can download a script in your language of choice by clicking on Download Scripts. This will reveal 10 different options. If you look at the source, you will see that this list is definitely as comprehensive as it gets, and is a very similar method to ours. From there, you can implement it on your own server or site.

After you download, implement it on your server like you would the one we created. The only thing you need to change is the redirect URL:

### Wrap Up

Now that we covered mobile browser detection, the theory behind it, and some popular solutions, you should be well equipped to implement a similar system in your own projects! I hope you found this tutorial informative and useful, and let me know if you have any questions or remarks in the comments section!