Welcome to WebmasterWorld Guest from 54.145.13.215

Forum Moderators: incrediBILL

Message Too Old, No Replies

Cleaner way to display browser version?

How to show "Safari 5" instead of long string of info

     
8:48 pm on Oct 19, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 30, 2010
posts: 13
votes: 0


All scripts that I can find that detect user browser and version info will give me something like, for example, "Netscape 5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5" instead of "Safari 5". Not pretty. How do sites like [whatbrowser.org ] show it so nicely?
7:37 pm on Oct 22, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


That long string is simply the "user agent" string that the browser sends to identify itself. You don't need a lengthy script to get the user agent, it's just one line of code. eg. in PHP, $_SERVER['HTTP_USER_AGENT'] or in JavaScript, navigator.userAgent.

In order to get something like "Safari 5" you can write a script to intelligently parse the user agent for the appropriate parts you are looking for. This will work OK for the main browsers. If you need to dig deeper and try to identify all browsers / robots / crawlers then you probably need to look up the user agent in some kind of database.
8:09 pm on Oct 24, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 30, 2010
posts: 13
votes: 0


I'm a novice, I don't know how to parse out that info to show... how would I do that?
8:14 am on Oct 25, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Depending on the language of choice, you're perhaps better asking this in the appropriate forum... eg.

JavaScript (client-side in the browser):
[webmasterworld.com...]

PHP (Server-side)
[webmasterworld.com...]
12:32 pm on Oct 25, 2010 (gmt 0)

Full Member

5+ Year Member

joined:Dec 30, 2009
posts: 249
votes: 0


In pseudo-code:

if ($user-agent == 'some long string of crap #1') {
$user-agent = 'Safari 5';
}
elseif ($user-agent == 'some long string of crap #2') {
$user-agent = 'IE 6';
}

A smart programmer would use an associative array or at least a switch statement though.