in PHP

How To Use PHP To Get Visitors Browser

With the raise of popularity in using mobile devices to access websites, web designers need to start designing their websites so it can be used correctly on a mobile device. There are several ways you can do this, you can either use media queries, mobile apps or separate mobile sites.

CSS Media Queries

If you use CSS media queries then you can only change the layout of how the site looks on the different devices but it will not change the functionality. For example if you have a video website which will display the video in flash your iOS visitors will not be able to watch your videos. But if your using a mobile app or a separate site then you will know what the visitors mobile supports and will be able to change the format of the video to be watched in HTML5.

Mobile Apps

Mobile Apps are a good solution for your site, the only problem is that they only work on one operating system, so iOS users and android users will not be able to use the same app. This means that you will need to maintain multiple mobile apps, which can take up a lot of time and money. But it will give your users the most flexibility with using your site on their mobile.

Separate Mobile Site

The other option is to have a separate mobile site to serve your visitors depending on what device they are using, but in order to deliver them the correct functionality you need to be able to discover what device and browser the visitor is using. Below is PHP code that you can use to discover what device your visitor is using and what operating system they are running on. To discover the device and operating system your visitor is using all you need to do is use the global server variable with the parameter HTTP_USER_AGENT, with the PHP function get_browser(). The Get browser function will get all the information about the current visitors browser and operating system and will return it in either an object or an array. This function takes 2 parameters: - user_agent - This is the user agent to be analysed by the function, if this is NULL then it will use the current visitors user_agent.

  • return_array - If set to true then will return array.

$browser = get_browser(null, true);

This will return

    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] => 
    [vbscript] => 
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>

For get_browser to work correctly you must download the browscap.ini file and point your browscap configuration in your php.ini file to the location of browscap.ini. You can download browscap.ini here

Using Wordpress To Check Mobile Site

If you are using Wordpress and need to find out if your visitor is currently running Wordpress has developed a function which will return true if the visitor is using a mobile device. You can then use this function to change the stylesheet to use to display your data. Here is an example of how to use this function.

<?php if ( wp_is_mobile() ) {
    /* Display and echo mobile specific stuff here */
} ?>

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email