![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Refining the ProgramOur access.pl program doesnt make a very spiffy home page, but all of the elements are there for you to add this code to your own page. However, youll be annoyed to notice very soon that your access count is incremented every time you call up the page, too. If you test your Web pages thoroughlyand you certainly should!the counter can be boosted to quite unrealistic heights in no time at all. Fortunately, CGI provides a way to get around this problem. Remember the environment variables that you learned about in Table 3.1 in Skill 3? One of them contains the IP address of the Web site visitor who initiated the HTTP session. If you can read that address and compare it with your own, it is a simple matter to decide whether or not the count should go up. Perl makes it easy for you to automate the process. Checking a visitors IP address also provides the opportunity to introduce a new Perl concept: conditional expressions using the if statement, which well discuss in the next section. To Increment or NotRecall from Skill 3 that the Web server fills its environment with a great deal of information about the computer that has started up an HTTP session. This information is passed along to a Perl program through CGI. The environment variable that contains a visitors IP address is called REMOTE_ADDR, and you can get to it by using Perls @ENV array of environment variables: $VisitorAddress = $ENV{'REMOTE_ADDR}; $VisitorAddress now will contain an Internet address in the form nnn.nnn.nnn .nnn where n is some number. Lets get back into access.pl and add a line near the top to store your IP address.
Heres the code: # Second version. Creates or opens a file with a number # in it, increments the number, writes it back, then displays # the result in a message on a Web page. require "perl-cgi/html.pl"; # Get HTML header, ender. $CountFile = "counter.dat"; # Name of counter file. $PageTitle = "Web Page Access Counter"; # Web page title. $HomeBase = "198.66.21.24"; # My IP address. $VisitorAddress = $ENV{'REMOTE_ADDR}; # Visitor's IP address. Two new variables are initialized in this revision to access.pl, $HomeBase, in which your IP address is stored, and $VisitorAddress, which pulls the visitors IP out of the environment. As usual, you should replace the string given to $HomeBase with your own IP address. Comparing ConditionsYou should now be able to compare the incoming address with your own and decide whether the access count will be incremented or not. How? if is known as a conditional statement. A conditional statement allows you to compare two conditions and essentially go in one direction or another based on the results of the comparison. In Perl, if begins a code block similar to what you learned about with the while statement. It looks like this: if (This statement is true) { Execute; this block; of code; } The truth or falseness of the statement in parentheses usually is determined by the result of an equality. In other words, in the statement if (1 > 2), the comparison in parentheses is false (1 is not greater than 2, at least not in this dimension), so the code block would not be executed. Because scalar variables can contain either string or numeric information, Perl needs to know what kind of comparison is being made. As a result, two sets of operators are used to differentiate between the two types of data. Table 4.1 summarizes these relational operators.
IP addresses are numeric, but not in the form passed back to you in REMOTE_ADDR. You should use the string comparison operator, eq, to determine whether they match.
Change the line in access.pl that increments $Counter $Counter += 1; to say this: if ($VisitorAddress ne $HomeBase) { $Counter += 1; } Can you see how it works? If the visitors address is not equal to your address, then add 1 to the counter.
Now you wont be artificially boosting the number of visits recorded to your Web page. Running the CounterYou have run access.pl so far by typing a URL into your Web browser and having it tell the Web server to execute the code. Eventually, however, you will want to add this feature to your own Web page, which visitors visit by typing its URL into their browsers. How do you get access.pl to run automatically? Unfortunately, standard HTML doesnt provide a straightforward way of doing this. The only HTML method for jumping to a URL is through a hyperlink, and those usually have to be clicked, or otherwise specifically requested, by a user.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |