Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Short and Sweet Browser Detection

by hacker (Priest)
on Jun 07, 2002 at 17:16 UTC ( #172580=snippet: print w/ replies, xml ) Need Help??

Description: During compatibilty testing of a new site I'm working on, I needed a way to detect browsers and act accordingly. This short and sweet script does just that, and removes redundancy. (I'm also teaching myself hash and array references)
my @user_agent_list= 
                ({title=>'Internet Explorer', 
                        string=>'MSIE', 
                        url=>'http://www.microsoft.com/windows/ie/defa
+ult.asp'},
                 {title=>'Konqueror', 
                        string=>'Konqueror', 
                        url=>'http://www.konqueror.org/'},
                 {title=>'Galeon', 
                        string=>'Galeon', 
                        url=>'http://galeon.sourceforge.net/'},
                 {title=>'Opera', 
                        string=>'Opera', 
                        url=>'http://www.opera.com/'},
                 {title=>'Dillo', 
                        string=>'Dillo', 
                        url=>'http://dillo.cipsga.org.br/'},
                 {title=>'Mozilla', 
                        string=>'Mozilla\/5', 
                        url=>'http://www.mozilla.org/'},
                 {title=>'Netscape', 
                        string=>'Mozilla\/4', 
                        url=>'http://wp.netscape.com/download/'},
                );

print div({-id=>'browser'}, p("Browser: $ENV{'HTTP_USER_AGENT'}"),);

for my $browser (@user_agent_list) {
        if (($ENV{'HTTP_USER_AGENT'}) =~ /$browser->{string}/) {
                print p("You seem to be using", 
                        a({-href=>"$browser->{url}"}, 
                        $browser->{title}, ));
        }
}

Update
I just found a slight flaw in this design. Microsoft Internet Explorer on Win32 platforms displays a $ua->agent string of:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
..and that also matches what Netscape displays. Running this against those two browsers will print out two lines for each of them (IE matches Netscape and IE, and vice-versa for Netscape).

I'll try to find a way around this without having to use !~ matching.

Comment on Short and Sweet Browser Detection
Select or Download Code
Re: Short and Sweet Browser Detection
by KM (Priest) on Jun 07, 2002 at 17:27 UTC
    I would suggest making your structure a hash, so you don't have to loop. With a long list (like if you add Lynx, Chimera, iCab, DoCoMo, etc...) the looping is uneeded.
    my $user_agent = {"Mozilla" => {string => "Mozilla", url => "http://mo +zilla.org"}, "Opena" => # etc... };
    May serve you well. Just make sure the keys are the user agent info you want, and manipulate $ENV{USER_AGENT} to remove any OS info (assuming you don't want that).

    Cheers,
    KM

      But... You'll still need to loop through, trying a match with each until you find the user agent.

      Perhaps looking for the element in user_agent that contains a slash, then stripping the version number, and using that as an index into a hash. i.e. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" becomes "Mozilla/4.0", then "Mozilla" which is used as a key in the hash.

      But then that may be a bit of overkill unless you're planning on matching every possible browser/wap device. Even then, the list would be under a couple hundred.

        Just have the keys be what the USER_AGENT string will be. No looping, that's what exists() is for.

        Cheers,
        KM

Re: Short and Sweet Browser Detection
by chromosundrift (Novice) on Jun 16, 2002 at 11:56 UTC
    You really should AVOID even looking at user agent strings. It is possible to write cross browser html and javascript without checking for particular browsers. Failing to do this always makes the site more brittle and less maintainable.
      The script wasn't meant to determine what to show to the user, based on HTTP_USER_AGENT. It was used to simply detect their UserAgent, and display what was detected to the user. All of my code is 100% cross-browser compatible and works with every browser without having to make per-browser tweaks.

      Your point is still valid though, for others who may use that code to branch to different HTML output formats per browser.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://172580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-09-17 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (55 votes), past polls