S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

My website uses nested tables out the wang. The problem is that older browsers lock when they try to open some of the pages. I'm thinking of writing a script that would handle redirecting browsers that passed testing, those that cannot, and those that are unknow to 4 possible locations:
$failed = "http://failaddress"; $passed = "http://passaddress"; $untested = "http://untestedaddress"; $unknown = "http://unknownaddress";
Here's my idea. I want to create a hash in a hash (which I never have dealt with before) to create a USER_AGENT table. Something like:
my %agents = ( MSIE50NT => { string => "Mozilla/ +4.0 (compatible; MSIE 5.0; Windows NT; DigExt) ", browser => "Microsoft In +ternet Explorer 5.0", developer => "Microsoft", url => "http://www.m +icrosoft.com", tested => "y", passed => "y", MSIE55NT => { string => "Mozilla/4. +0 (compatible; MSIE 5.5; Windows NT; DigExt) ", browser => "Microsoft In +ternet Explorer 5.5", developer => "Microsoft", url => "http://www.m +icrosoft.com", tested => "y", passed => "y",

...and so on, and so on. Then I can do a test to see if the browser was tested and if it passed and redirect to the approprate page.

Does this seem like a good idea? And if so, does anyone want to help me make one? Am I looking to buy a extra large bottle of Advil? Should I just put this one down with a silver bullet? Or better yet, does one already exist? TIA

======================
Sean Shrum
http://www.shrum.net

  • Comment on Need help protecting the user - detecting and redirecting browsers the ADVANCED way...
  • Select or Download Code

Replies are listed 'Best First'.
Re: Need help protecting the user - detecting and redirecting browsers the ADVANCED way...
by thpfft (Chaplain) on Mar 01, 2002 at 10:57 UTC

    HTTP::BrowserDetect is pretty good. It's just an oop implementation of netscape's old 'sniffer' javascript, but nicely done, offering methods like $browser->is_ie5plus or $browser->is_robot.

Re: Need help protecting the user - detecting and redirecting browsers the ADVANCED way...
by particle (Vicar) on Mar 01, 2002 at 13:24 UTC
    i would suggest inverting your logic to use three hashes, my (%passed, %failed, %untested); (i'm not sure you need a fourth). once you get the browser type, test for existence of the key in the three hashes. if it doesn't exist in the third, add the key, then redirect. a lengthier example might look like:

    my (%passed, %failed, %untested); %passed = { MSIE50NT => 1, MSIE55NT => 1, URI_REDIRECT => 'http://passaddress/', }; # set %failed and %untested my $br_detect = 'palm'; # as found by HTTP::BrowserDetect if( defined $passed{$br_detect} ) { redirect( $passed{URI_REDIRECT} ) +} if( defined $failed{$br_detect} ) { redirect( $failed{URI_REDIRECT} ) +} defined $untested{$br_detect} ? '' : %untested{$br_detect} = 1; redirect( $untested{URI_REDIRECT} ); sub redirect { # your redirect logic here... }

    ~Particle ;Þ

      Say a user attempts to view a page and the browser is detected to have not passed. I would like to be able to list (for ( keys %passed )) the passed browsers along with formal browser names, versions, and URL info.

      I am seeing something like this forming:

      my %passed = { # MSIE 5.0 on MS NT 4.0 1 = ( HTTP_USER_AGENT => "Mozil +la/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)", NAME => "MS Internet + Explorer" VERSION => "5.0" PLATFORM => "NT" MANUFACTURER => "Microsoft" URL => "http://w +ww.microsoft.com"

      Does this seem like the correct way to go with this idea? How would I structure the line to display the info like:

      Here is a list of browsers that have passed testing:
      
         + MS Internet Explorer 5.0 / NT (http://www.microsoft.com)
      ...

      being that the info is a hash in a hash in a hash?

      ======================
      Sean Shrum
      http://www.shrum.net

        since you're building this data structure manually, you have the ability to keep it in a config file, and do or eval it when neccessary. if you have unique values, for instance HTTP_USER_AGENT, you can use this as the key to the hash. i have an example below.

        if you don't have unique values, or require some combination of values for a unique key, you'll have to modify the data structure a little bit, to make sure your keys are unique.

        in any case, a read through perldsc will give you insight on working with complex data structures like hashes of hashes.

        my %passed = { "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"=> { NAME => "MS Internet Explorer", VERSION => "5.0", PLATFORM => "NT", # more specialized info here }, # more HTTP_USER_AGENT strings here... URI_REDIRECT => 'http://passaddress/', }; # if $test_key is the unique browser identifier you're looking for # you may print like... my $message = join( ' ', $passed{$test_key}->{NAME}, $passed{$test_key}->{VERSION}, '/', $passed{$test_key}->{PLATFORM), # etc. );

        ~Particle ;Þ