Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Detect Mac Browser

by Arbogast (Monk)
on Oct 02, 2003 at 19:20 UTC ( #296041=perlquestion: print w/replies, xml ) Need Help??

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

I want the user directed to one HTML page if the browser is a regular Apple, a different page if not an Apple. I am not responcible if they have an odd ball, non standard Apple Browser. Will this work??? Is there a better way??? I am unfamilliar with Apple, but am supposed to catch OS9 and OS X Apples. This CGI script seems to work.

#!/usr/bin/perl -wT use strict; use CGI qw(:standard); my $query = new CGI; my $browser = user_agent(); if ($browser =~ /apple|mac|safari/i) #I am a Mac { print $query->redirect(' +')} else #I am not a Mac { print $query->redirect(' +tm')};

Edit by thelenm: added code tags

Replies are listed 'Best First'.
Re: Detect Mac Browser
by simonm (Vicar) on Oct 02, 2003 at 19:54 UTC
    That pattern does look like it will catch most Mac users.

    (However, I'm unable to imagine a good reason for doing this; the various Mac browsers are different enough that the OS is rarely the biggest issue, and Safari is more like Linux's KHTML than it is like MacOS 9's Internet Explorer.)

      The poster doesn't mention this, but there are (sometimes) good reasons for doing OS-specific re-directs, but not for browser rendering reasons (shame on anyone who does that these days. In a world of nearly standards compliant browsers I'm hoping the days of browser detects are over). If you've got a Windows product and a Mac product why not direct your users to the appropriate version? Netscape does this when you go to download the latest version of Navigator... they do an OS-detect and show you the Mac version for download if you come in on a Mac, and the Windows version if you come in on Windows. Pretty clever, actually. :-)

      Gary Blackburn
      Trained Killer

        Yep, I must say that I was initially irritated when I read this. Because a lot of websites do this stupid "Check to see if your browser is IE or Netscape" and then dump you to a "We're sorry, you have a stooopid browser" page if not. The really irritating bit about this is that once past that initial check page, it works fine no matter what browser you're using...

        So yeah, upon reflection I can think of reasons for OS detect. But more often I have encountered bad reasons for the OS detect.


      The pattern seems to work for catching mainstream Mac browsers.

      Turns out the code WAS to send Mac Users to a different Flash Movie, (as several Monks correctly suspected) that worked around APPARANT bugs in Mac Flash Player. Not bugs that would crash the Mac Player, but make older Macs run in a jerky, non eye appealing manner.
      Therefore, there was a seperate Flash movie for the Apple player, so that both Mac and non Mac Users see the same movie.

      Nobody was denied content, or sent to any site asking them to upgrade/change browsers, unless they needed to install Flash player. Older Mac users were given access to a Flash Movie that allowed them to see same quality content as others.
Re: Detect Mac Browser
by Aristotle (Chancellor) on Oct 02, 2003 at 19:27 UTC
    If you're running Apache, it's probably easier and much more efficient to do this using a mod_rewrite rule. But you say it seems to and I don't see any reason it shouldn't, so..

    Makeshifts last the longest.

Re: Detect Mac Browser
by Lhamo Latso (Scribe) on Oct 02, 2003 at 20:00 UTC

    You could also use the $ENV{} hash to access the brower string. The user agent/browser info is stored in $ENV{HTTP_USER_AGENT}. Your code would change to :

    my $browser = $ENV{HTTP_USER_AGENT};

    I use this little program to see what is available on the different browsers that I test.

    #!/usr/bin/perl ## ## printenv -- print the environment ## print "Content-type: text/plain\n\n"; foreach $var (sort(keys(%ENV))) { $val = $ENV{$var}; $val =~ s|\n|\\n|g; $val =~ s|"|\\"|g; print "${var}=\"${val}\"\n"; }
      That's exactly the value he gets by calling's user_agent function.

      Makeshifts last the longest.

Re: Detect Mac Browser
by bart (Canon) on Oct 02, 2003 at 21:54 UTC
    You don't like iCab?

    In short: don't do this.. Do not assume because you don't know the browser, that people should be denied access. I've seen cases where people where denied access and being urged to "upgrade" because they were running MSIE 5.x, and not a more modern MSIE 4.0. Don't make me hate you.

Re: Detect Mac Browser
by Cody Pendant (Prior) on Oct 02, 2003 at 22:07 UTC

    There is of course a module for browser detection.

    For some reason I can't get to CPAN at the moment but it's there and it works well.

    But please do tell us why you're trying to detect the operating system of your site's visitors? The site doesn't seem to be offering software, for a start.

    I am not responcible if they have an odd ball, non standard Apple Browser.

    1. yes you are
    2. what on earth do you mean?

    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      Why??? Because the customer of the person I am writing the code for wants it that way.
      The web address has nothing to do with anything except testing the CGI script.
        Why??? Because the customer of the person I am writing the code for wants it that way.
        That's never a valid reason. That's a bit like saying "I was just following orders", and you know where that goes.

        I cannot ethically help you in your quest without a better explanation. And I would recommend you consider that you cannot ethically complete your task without more justification from your "boss".

        Write standard HTML, and you don't have to worry what platform I'm on. Please. Please don't send me to "UPGRADE YOUR BROWSER" hell just because I'm on an operating system of my choice.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        odd ball, non standard browser = any browser that is uncommon on a Mac. Perhaps my language was poor, I did not mean to imply uncommon browsers were bad.
Re: Detect Mac Browser
by Nkuvu (Priest) on Oct 02, 2003 at 20:37 UTC

    This string would send me to the non-Mac page if I'm using my Mac. My "odd ball, non standard Apple Browser" is Mozilla and/or Firebird (I alternate between the two). So it really depends on how accurate you want your redirects to be.

      Are you sure about that?

      Mozilla user agent strings often include platform information in parenthesis: "mozilla/5.0 (macintosh..."

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://296041]
Approved by Aristotle
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2023-09-28 20:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found