Display User-Agent ID on a website

Tutorial categories

This simple PHP tutorial will show what you have to do to display a web browsers User-Agent ID on a website. To make this tutorial work, you'll need webspace on a Linux based server with Apache installed on it, or you can test it locally on your computer with XAMPP installed.

Create a new PHP file called "useragent.php" for example or you can name it whatever you want. Now enter the following code into this newly created php file. This code will get your browsers User-Agent ID and display it in your website.

<?php echo $_SERVER['HTTP_USER_AGENT'];?>

Now open your web browser ( if you haven't done this already ) and enter the URL to the file. If you've done everything right you should see your web browsers User-Agent ID which is different for every browser and also every release version. Here are a couple examples what you might see.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Opera/9.24 (Windows NT 5.1; U; en)
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/523.12.9 (KHTML, like Gecko) Version/3.0 Safari/523.12.9

You can combine this code snippet with some text so your website visitors, and yourself, will know what's this information about. Here is an example text with the PHP code to show a visitors User-Agent on your website.

<p>You're currently visiting this website using <?php echo $_SERVER['HTTP_USER_AGENT'];?></p>

Here is the result of the output this simple code snippet will show for the browser you're currently visiting this website.

You're currently visiting this website using CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

Now you've seen how this code snippet works, all you have to do is to copy/paste it into your websites and use it for whatever purpose you want.

Download this PHP tutorial by Igor Klajo

Other PHP tutorials

Add to social bookmarking websites