Home arrow Perl Programming arrow Page 2 - Perl 101 (part 8) - Putting It To The Test

Adding Things Up - Perl

In the concluding article in the Perl 101 series, everythingyou've learnt so far is put to the test when you develop some real-worldCGI applications - a counter, a guest book and a form mailer.

TABLE OF CONTENTS:
  1. Perl 101 (part 8) - Putting It To The Test
  2. Adding Things Up
  3. Visitors Welcome!
  4. The Code...
  5. ...And The Explanation
  6. Going Backwards
  7. Fortune Cookies
  8. You Have Mail!
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
October 02, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: