[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Browser Redirect based upon OS



At 08.18 -0500 1998.11.25, Carl Johan Berglund wrote:
>At 23.00 +0100 98-11-24, Kevin Diffily wrote:
>>I have been searching all of the major and obscure CGI lists in vain for a
>>browser redirection based upon someones Operating System.  Since I am new
>>to Perl I do not feel confident enough to try this from scratch.  Does
>>anyone have any scripts and or ideas.  They will be greatly appreciated.
>
>Perl stores the name of the OS in the $^O variable. You'll just have to
>check that to know.
>
>#!perl
>
>$^O =~ /Win/ and $location = 'windows/index.html';
>$^O =~ /Mac/ and $location = 'macos/index.html';
>$location ||= 'unix/index.html';
>print "Location: $location\n\n";
>
>__END__

No, he means browser redirection on the server side based on the client
software.  For that you need to check he unreliable $ENV{HTTP_USER_AGENT}
variable.  My browser of choice for the moment has the value "Mozilla/4.5
(Macintosh; U; PPC)".  The LWP::Simple module has a user agent of
"LWP::Simple/5.31", which contains no platform information.  I can hack my
Netscape with ResEdit to say "Pudgzilla/6.0 (Amiga; U; 68000)".

Browser redirection based on OS is a risky business.  Just make sure that
you have a reasonable default page.  In fact, Perl Cookbook has a nice way
of doing it with a switch statement in Recipe 19.8 (which I found not when
looking for browser redirection code, but when looking for a switch
statement).

#!/usr/bin/perl
# os_snipe - redirect to a Jargon File entry about current OS
$dir = 'http://www.wins.uva.nl/%7Emes/jargon';
for ($ENV{HTTP_USER_AGENT}) {
    $page  =    /Mac/            && 'm/Macintrash.html'
             || /Win(dows )?NT/  && 'e/evilandrude.html'
             || /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html'
             || /Linux/          && 'l/Linux.html'
             || /HP-UX/          && 'h/HP-SUX.html'
             || /SunOS/          && 's/ScumOS.html'
             ||                     'a/AppendixB.html';
}
#print "Location: $dir/$page\n\n";
print "Location: $dir/$page\015\012\015\012";
__END__

Note that there is a default location in case no User Agent matches.  Also
note that "Mac" is very ambiguous ... is it Mac OS X?  Is it MachTen?
Think carefully about your patterns.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch