Welcome to WebmasterWorld Guest from 107.20.20.39

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)



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)

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



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)



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)

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



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)

5+ Year Member



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.