home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_5 / count.pl < prev    next >
Encoding:
Perl Script  |  1996-06-15  |  1.8 KB  |  73 lines

  1. #!/usr/bin/perl
  2.  
  3. # Access the environment variable by name
  4.  
  5. $script_name = $ENV{'SCRIPT_NAME'};
  6.  
  7. # Access the counter information indicating whether this script was
  8. # previously called and how many times.
  9.  
  10. $path_info = $ENV{'PATH_INFO'};
  11.  
  12. # Start generating HTML output with the required MIME HTML type.
  13.  
  14. print "Content-type: text/html\n\n";
  15.  
  16. # Print HTML header information.
  17.  
  18. print <<EOH;
  19. <HTML>
  20. <HEAD>
  21. <TITLE>CGI Script How-To: Test Script</TITLE>
  22. </HEAD>
  23. <BODY>
  24. EOH
  25.  
  26. print "<H1>CGI Script How-to<BR>determine the path of the script being executed</H1>\n";
  27.  
  28. # Test the path info variable. If it is not set then the script has been
  29. # called for the first time and will print the appropriate message.
  30.  
  31. if (!$path_info)
  32. {
  33.     print "<H2>This is the first time you called this script</H2>\n";
  34.     $count = 1;
  35. }
  36.  
  37. # Otherwise the path info variable has been set. In this case we want to
  38. # increment the counter and say how many times the script has been called.
  39.  
  40. else
  41. {
  42.     ($count = $path_info) =~ s!^/!!;    # strip off the leading slash
  43.     $count++;                        # increment the counter
  44.     print "<H2>You have called this script $count times</H2>\n";
  45. }
  46.  
  47. # Test the script name variable and use it's value in a link back to itself.
  48. # Selecting this link will call the script again, but this time with the extra
  49. # path information indicated by "/$count" which will pass the counter to the
  50. # script and be stored in the PATH_INFO environment variable.
  51.  
  52. if ($script_name)
  53. {
  54.     print qq!<A HREF="$script_name/$count">Cick here to call the script again</A>!;
  55. }
  56.  
  57. # If the script name is not defined then print a message.
  58. # This means the script can't refer back to itself.
  59.  
  60. else
  61. {
  62.     # Otherwise we can't link to the script so say it
  63.     printf("I don't know who to call\n");
  64. }
  65.  
  66. # Print closing HTML tags.
  67.  
  68. print "</BODY></HTML>\n";
  69.  
  70. #
  71. # end of count.pl
  72. #
  73.