Learnthat.com Free Certification and Computer Online Courses
ThatNetwork.com Internet Media Network Givethat.com free gift ideas Definethat.com - Free technical definitions, define Free Romance Tips, Romantic Tips, Date Ideas Free POD publisher comparison print on demand
Learnthat.com Free A+ Certification, Computer Classes, Certification Courses, Business, Lifestyle, ApplicationsFree business tutorials and definitionsFree certification tutorials and definitionsFree computer and technical tutorials and technical definitionsFree Finance Tutorials and DefinitionsFree House Repair TutorialsFree Lifestyle and Fun Tutorials
  All Online CoursesNew Courses QuickTipsWord DefinitionsSubmit a TutorialLinks
÷ Resources
Home
free tech tutorials
Free Tutorials
free tech tutorials
Certifications
free tech tutorials
Definitions
free tech tutorials
Resources/Links
free tech tutorials
Forums
free tech tutorials
Distance Education
free tech tutorials
Highest Rated Tutorials
÷ Popular Tutorials
÷ New Tutorials

Beginning CGI Programming with Perl

 

Exercise 2.1. Reading and decoding the User-Agent field

The CGI program to determine which browser is calling your Web page has two basic steps. First, it must figure out which browser is accessing it. Then, it must return the correct Location headers based on the information figured out in step 1.

Because Netscape is the offending browser by going off on its own and implementing all those cool extensions that are so much fun to use, let's just deal with the Netscape browser. If Netscape were the only browser that could handle tables, this program would be complete. In practice, this code should deal with all the browsers that can and can't handle the HTML+ extensions.

The format of HTTP_USER_AGENT is illustrated by how these two popular browsers define their User-Agent request header:

  • Mozilla/1.1N (Windows; I; 16bit)
  • AIR_Mosaic (16bit)/v1.00.198.07

You can find out what types of browsers are looking at your Web page by looking in the server log files. These log files are discussed in further detail in Tutorial 10, "Keeping Track of Your Web Page Visitors."

The easiest thing to do is to split HTTP_USER_AGENT into fields and then compare them against browsers you know will work for your enhanced Web page. Listing 2.3 contains the Perl code to do this. As with all the code in these tutorials, I step through the new and relevant Perl code. You are not expected to know Perl. However, I hope you will feel comfortable enough with Perl by the time you complete these tutorials to write CGI programs of your own.


Listing 2.3. Perl code to return a Web page based on a browser.

01: #!/usr/local/bin/perl
02
03: @user_agent = split(/\//,$ENV{'HTTP_USER_AGENT'});
04:
05: if ($user_agent[0] eq "Mozilla"){
06:     @version = split(/ /,$user_agent[1]);
07:     $version_number = substr($version[0], 0, 3);
08:     if ($version_number < 1.1){
09:        print "Location: http://www.county-line-bbq/clbbq-plus.html.com\n\n";
10:     }
11:     else{
12:        print "Location: http:// www.county-line-bbq/clbbq-minus.html.com  \n\n";
13:     }
14: }
15: else{
16:     print "Location: http:// www.county-line-bbq/clbbq-minus.html.com \n\n";

 

by Frontsource.com.pk

Find more free Internet tutorials from Learnthat.com.

More Information:

Help us by rating this tutorial: [---] [--] [+] [++] [+++]
[---] lowest; [+++] highest

 

 

 


Learn: Free Tutorials - Links to Free Tutorials
Understand: Free Technical Definitions - ExplainThat Whitepapers
Use: Certification Section - Discussion Forum

Visit Our Network Sites:

DefineThat.com Technical Defintions Romancetips.com
Free Gift Ideas - GiveThat Planthat.com Free Unique Business Ideas
PublishOnDemand.net - Learn about Print on Demand (POD) - Self Publishing Seekthat.com - Technical Search Engine
ThatNetwork.com  

About Us : Contact Us : Advertise : Privacy Information

© 1998-2004 Learnthat.com