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', 

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", 
                        $browser->{title}, ));
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.

Replies are listed 'Best First'.
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"}, "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).


      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.


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.