Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Short and Sweet Browser Detection

by KM (Priest)
on Jun 07, 2002 at 17:27 UTC ( #172584=note: print w/replies, xml ) Need Help??

in reply to Short and Sweet Browser Detection

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).


Replies are listed 'Best First'.
Re: Re: Short and Sweet Browser Detection
by jeffenstein (Pilgrim) on Jun 07, 2002 at 18:55 UTC

    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.


        Then you would have to have the complete USER_AGENT string for each possible browser, which isn't really viable. Each string contains (usually) information about both the browser name, version and the OS. That is a lot of strings.


        Ah, the regexp; the good old hammer. Tell you what. I spent 30 seconds looking in my web log (which does *not* get any significant amount of traffic), and just from this fast survey, I could pick out this list easily:

        Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt; DTS Agent Mozilla/3.0 (compatible; Indy Library) Mozilla/5.0 Galeon/1.2.1 (X11; Linux i686; U;) Gecko/20020417 Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) Mozilla/4.5 (compatible; iCab 2.7.1; Macintosh; I; PPC) Mozilla/5.0 (Linux 2.4.7-10 i586; U) Opera 6.0 [en] Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.01 [en]
        Sure looks easy to write a regexp that picks out the important part, yes? When should we pick something inside the paranthesis? Outside? What if the paranthesis doesn't close? Is it MSIE or Opera? I'm sure it is easy; please show me.

        You have moved into a dark place.
        It is pitch black. You are likely to be eaten by a grue.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://172584]
shmem often walks around searching his glasses. In order to have a better sight, I put them on.
[Discipulus]: I'm advantaged: berrillio is always on my noose
Discipulus looks for his glasses just to defeat his thirst

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-05-20 16:29 GMT
Find Nodes?
    Voting Booth?