Perl
  Home arrow Perl arrow Page 2 - Perl 101 (part 8) - Putting It To The ...
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 8) - Putting It To The Test
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2000-10-02

    Table of Contents:
  • Perl 101 (part 8) - Putting It To The Test
  • Adding Things Up
  • Visitors Welcome!
  • The Code...
  • ...And The Explanation
  • Going Backwards
  • Fortune Cookies
  • You Have Mail!

  • 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 8) - Putting It To The Test - Adding Things Up
    (Page 2 of 8 )

    The first tool that we're going to discuss today is the counter. The basic purpose of a Web site counter is to record the number of people who have visited a particular page.

    There are four simple steps involved in building a counter:

    Step One: Create a text file on the server, which will store the number of visitors.

    Step Two: When someone visits the site, this file is accessed, the number stored within it is obtained and incremented by one, and then displayed to the visitor.

    Step Three: The new number is written back to the file.

    Step Four: Go back to Step Two.

    Here's the code:
    #!/usr/bin/perl
    # counter.cgi - count visitors to page
    # open a file handle to read the contents of the file "counter.dat"
    # this file contains the number of visitors before the
    # current user.
    # make sure that you have permission to write to this file
    open (COUNT, "counter.dat");
    # assign the value that is stored in the file handle in a temporary variable
    $counter = <COUNT>;
    close (COUNT);
    # open the file handle to write the new number back to the file
    open (COUNT, "> counter.dat");
    # increment the counter variable by one
    $counter += 1;
    # write the new number
    print COUNT "$counter";
    close (COUNT);
    # now display the HTML code
    print "Content-Type: text/html\n\n";
    # print the new counter value
    print "<html>\n<body>\n<h1>Your are visitor number
    $counter.</h1>\n</body>\n</html>";
    

    And here's the output:
    
    Content-Type: text/html
    <html>
    <body>
    <h1>Your are visitor number 30.</h1>
    </body>
    </html>
    

    You can easily extend the functionality of this counter to store the IP address, the browser type, the referrer URL and other visitor statistics by making use of the environment variables we showed you last time.but we're going to leave that to you.

    This article copyright Melonfire 2000. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    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 3 hosted by Hostway