Page 1
Q: I've seen some pages that can detect what OS and browser you're using. How can I do this?
- Mack Rhinelander
A: Very easily. I'm sure that the user surfing on a Mozilla/4.03 (Macintosh; I; PPC, Nav) would also like to know how I did that.
Whenever a user connects to a server with an http:// address, her browser must announce certain things, like what data it wants, and where the data should be sent (the IP address). Then there's a bunch of optional information - including which browser is being used or which platform the browser's running on - that anyone can get from the browser, if they use a CGI or client-side script.
My friend alx wrote this script when he first started to program CGIs. All it does is run through a list of environmental variables (representing things like the user's platform and browser) commonly known to most servers. Now, you can add your own, but this is a good baseline. If you'd like the source, you can find it here.
Now that you know what the variables are, you need to access them from your Web page. This can be done from either a server-side script (I'll use Perl as an example) or a client-side script (like JavaScript or VBScript).
For a server-side script, you would have to either generate the whole page with a CGI, or use a server-side include within a static page. Regardless of how you accomplish it, the bit of code that accesses the variables will look something like this:
#!/usr/local/bin/perl
print "Glad to see that you're
using $ENV{HTTP_USER_AGENT}.
Perhaps you should go get a <a href=\"http://www.aleeanne.org.uk/
tuneup/\">tuneup!</a>\n";
Which would look like this on the page:
Glad to see that you're using Mozilla/4.03 (Macintosh; I; PPC, Nav), perhaps you should go get a tuneup!
In Perl, you can access a program's environmental variables with the %ENV array. To access a single variable, you would use it like this: $ENV{variable name}. That's all there is to it. Now, you might notice that the names used by browsers aren't always what you might expect. For instance, Netscape announces itself as "Mozilla," in deference to its still-official, but seldom-seen-since-the-IPO mascot.
Other browser manufacturers competing for market share noticed that many content providers would make really snazzy "Netscape-enhanced" pages, using all the Netscape extensions they could get their hands on, and then redirect other browsers to a really boring page. To combat that, Microsoft (for one) has started announcing its browser as "Mozilla/2.0 (compatible; MSIE 3.0; Windows 95)," in hopes that Web sites would just let it slip by.
Unfortunately, this just means that if you're setting up a script that does anything but print out the full User_Agent, you have to do some creative text processing so you can distinguish between the two. There was even a time when Microsoft's Internet Explorer let you type in any old browser string you wanted, but that just led to trouble.
On the client side, things are much the same. You can use any of several JavaScript objects to access this kind of environmental data. For instance, you could use the object, navigator.appName:
<script language="javascript">
document.writeln('You are using ' + navigator.appName + ' to read this page');
</script>
Which generates:
Of course, to do anything more interesting than that takes a bit of text processing and some if-then statements. Here is a script I wrote to test the JavaScript object. To view the source in Netscape 3.0, add view-source: before the URL in the location bar.
Well, those are the basics of accessing environmental variables in scripts. Using them, you can easily display what kind of browser a user has, or write conditional HTML scripts that send them to the appropriate page. Just remember to chmod a+x any Perl scripts, and always, always check with your friendly neighborhood webmaster to make sure it's OK to run scripts in your directory.
|