Detecting the users browser using PHP

How to detect a Users browser?

Browser detection in PHP is pretty easy.

Note: If anything from below doesn’t work for you, then see the troubleshooting tips at the end of this page.

For detecting the browser, we’re using PHP’s built-in function ‘get_browser()

This function accepts 2 arguments: The User Agent and a boolean value for whether to return the result in an array or not.

The Program:

Troubleshooting:

    1. Open your ‘php.ini‘ file from your PHP installation.
      For XAMPP users, it is located in the folder: ‘C:\xampp\php‘. Open the file in Notepad.
    2. Find ‘browscap‘ in it. (Tip: Do CTRL+F for quick find.)
      You will find something like browscap = “<browscap.ini_path_here>”

      For XAMPP users, it should be:  browscap = “C:\xampp\php\extras\browscap.ini”
      Although it might be placed in a different folder in newer versions.

    3. Verify that your browscap.ini exists in the path mentioned above.
      If it is not there then find browscap.ini in your computer or download it from here or search for the latest 1 here.
    4. Either place the browscap.ini file in the folder specified in your php.ini file.
      Or, place the browscap.ini file in your desired folder and point your php.ini file to it.
    5. Restart your Apache/XAMPP/Wamp or whichever client you are using.
    6. Try the above program again.

 

The following two tabs change content below.

Lalit Mali

Lalit is a technology enthusiast, a programming lover and currently an Android fan.