Perl
  Home arrow Perl arrow Page 6 - Perl 101 (part 7) - CGI Basics
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (part 7) - CGI Basics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 4
    2000-09-25

    Table of Contents:
  • Perl 101 (part 7) - CGI Basics
  • Meet Donald Duck
  • Open Sesame
  • Perl And CGI
  • A Cure For Low Self-Esteem
  • GETting Your Form To Work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Perl 101 (part 7) - CGI Basics - GETting Your Form To Work
    (Page 6 of 6 )

    The CGI environment comes with a set of pre-defined variables that can assist in the task of developing server-side scripts. Here's a list of the important ones:

    $ENV{REMOTE_ADDR} The address of the client machine
    $ENV{REMOTE_HOST} The host name of the client machine
    $ENV{REQUEST_METHOD} The method used by a form to request data
    $ENV{HTTP_USER_AGENT} The client browser
    $ENV{QUERY_STRING} The query string passed if the GET method is used

    Our next few examples will give you some idea of how these, and similar variables, can be used within your scripts.
    #! /usr/bin/perl
    $ip = $ENV{REMOTE_ADDR};
    $browser = $ENV{HTTP_USER_AGENT};
    print "Content-Type: text/html\n\n";
    print "<html><body><font face=Arial>Your IP address is $ip and your browser
    is $browser</font></body></html>";
    

    And now, when you browse to this page, you'll see some information on your IP address and browser version.

    Our next example demonstrates how a Perl/CGI script can be used to process data submitted via a form. Here's the form...
    <html>
    <head>
    <basefont face=Arial>
    </head>
    <body>
    <form action=http://localhost/readform.cgi method=GET>
    Enter your first name: <input type=text length=20 name=name><br>
    <input type=submit value=Submit>
    </form>
    </body>
    </html>
    

    ...and here's the CGI script that receives and processes the data.
    #!/usr/bin/perl
    # readform.cgi
    # reads form data and generates a page
    # for GET data
    if ($ENV{'REQUEST_METHOD'} eq "GET")
    {
    $yourname=$ENV{'QUERY_STRING'};
    }
    # for POST data
    else
    {
    $yourname = <STDIN>;
    }
    # Remove spaces if any
    $yourname =~ s/\+/ /g;
    # split form data and store in hash
    %details = split (/=/, $yourname);
    # generate page
    print "Content-Type: text/html\n\n";
    print "<html><body>";
    while (($name, $value) = each %details)
    {
    print "Thank you for your submission, $value!\n";
    }
    print "</body></html>";
    

    The script above will accept data from the HTML form and store it in a variable called $yourname. The manner in which data is submitted by the form(GET or POST) decides the manner in which it is assigned to the variable; the QUERY_STRING environment variable is used to make this decision.

    If the text entered into the form contains spaces, the spaces are replaced with + characters when the form is submitted - this needs to be reversed via a regex. For example, if you enter the name "Luke Skywalker" into the form, the URL string will look like this:

    http://localhost/cgi-bin/readform.cgi?name=Luke+Skywalker

    Next, the name-value pairs are split apart on the basis of the = sign, and are assigned to their respective places in the hash %details. This hash is then used to print the name in the result page, which is also generated by the same script.

    Thus, the CGI environment can be used to accept data from one Web page, process it or transfer it to another, and generate a new page containing dynamically-generated output. This is the basis of using CGI to create dynamic Web sites.

    In the next issue of Perl 101, we'll delve deeper into CGI, with a look at some simple CGI programs that can be used to track hits on your Web site, or store visitor comments. Don't miss it!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway