php detect browser

Strongly recommended book
Hi Guys,

So how do you detect a browser using PHP. Well PHP is our saviour this time out of the blues :)

PHP has inbuilt function called get_browser()

Wow! that made my job a bit easier with that function. But yeah you will need to configure browscap to make it work. Check out a expert from php.net.

You will need access to your php.ini to configure browscap. More information can be found here

http://www.php.net/manual/en/misc.configuration.php#ini.browscap

here is basic usage of this function

 

  1. $browser = get_browser(null, true);
  2. print_r($browser);

 

From php.net

In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.
browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.
While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

 

Second option is to use $_SERVER['HTTP_USER_AGENT']

If you try echoing above i.e.

  1. echo $_SERVER['HTTP_USER_AGENT'];

Output will be similar to the one shown below. Based on the browser you are using output will differ

  1. Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7

 

Third and most brilliant option atleast for a guy with small brain like me is to use maintained library such as

http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

 

I hope this helps

 

 

 

 

Search Terms:

Jaspreet Chahal

Self-confessed Max Payne and Howzat Cricket game addict. Big fan of Dharmendra, Keanu reeves & Lawrence "Larry" Page. I love my Wife, my son and bit of geeky stuff. I started falling in love with Blog'in last year. Writing is tending out to be fun. I write on situation I was in thus my post always travel around web development stuff. I hope you enjoy my posts.

More Posts - Website

Follow Me:
TwitterFacebookPinterestYouTube

Speak Your Mind

*

CommentLuv badge