Apache Server for Windows Little Black Book:Making Your Site Dynamic Through CGI
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
To access the contents, click the chapter and section titles.
Apache Server for Windows Little Black Book
(Publisher: The Coriolis Group)
Author(s): Greg Holden with Matthew Keller
ISBN: 1576103919
Publication Date: 01/01/99
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
Creating A Simple Perl Script
Once you have installed Perl and mod_perl, you can start writing and testing your own scripts with Apache. The following script might look simple, but it introduces you to lots of important concepts:
How to invoke Perl and start a program
How to store input from the keyboard in Perls standard input <STDIN> file
How to print using the standard output file
What constitutes a line of Perl script
What a variable is
How library functions work in Perl scripts
How comments work in Perl
Follow these steps to create the script:
1. Open Notepad.
2. Start Apache (assuming you have installed mod_perl as described in the previous section).
3. Once you have a new blank text-editing window open, enter the following:
#!C:/perl/bin/perl.exe
$inputline = <STDIN>;
print ($inputline);
Dont worry at this point about whether you should leave blank spaces around the equal sign operator (=) or after the word print. Perl is very liberal about blank spaces. For instance, the following lines are all acceptable in Perl:
$inputline = <STDIN>;
$inputline=<STDIN>;
$inputline = < STDIN >;
WARNING! Make sure you dont press the spacebar on your keyboard before or after any of the important symbols that function as Perl operators, such as the dollar sign ($), which is used before a scalar variable, and the semicolon (;), which denotes the end of a line. If you do, youll see a syntax error such as $ inputline = <STDIN>;.
4. Choose File|Save to save your changes. Save the file in the directory you have designated as the location for CGI scripts. Use a file-name extension that you have described with Add-Handler or AddType (see the preceding sections, Specifying CGI File-Name Extensions and Adding New CGI File-Name Extensions).
5. Test your script by launching a Perl interpreter and typing perl myscript.pl.
A detailed examination of the preceding code will help you know exactly whats going on:
The first character, #, tells the Perl interpreter that this is a comment and not part of the actual code to be interpreted.
#! is called a sha-bang. It is a special designator that denotes the location of the Perl interpreter and any switches it needs to run with. At this point, the interpreter is launched and begins parsing the code.
The rest of the line gives the path that tells a computer where to find the Perl application (or executable) that is needed to interpret the script.
$inputline = <STDIN>; is the first line that actually functions as Perl code. A line of code in Perl is called a statement. Just as a sentence consists of nouns, verbs, conjunctions, and symbols, a Perl statement consists of bits of data called tokens.
Library functions are built into the Perl language. When you enter the function print, Perl knows that you want to print the argument(s) that follow. For instance, in the first code snippet in Step 3, the argument $inputline is passed to the call to print.
<STDIN> is the counterpart to <STDOUT>. Both function as containers for data that is entered when you run a program. STDIN is empty until the user starts typing.
The following sections present some scripts that perform more practical Web site functions.
Generating Environment Variables
Apaches module mod_cgi enables the server to set a variety of environment variables that provide a CGI script with the information it needs. Most of the variables are included in the official CGI specification, which can be found at hoohoo.ncsa.uiuc.edu.
Once you have installed Perl and configured your server to run CGI programs, you can write a script like the one shown in this section to access the variables and print them. You can print to the Perl application window, or you can send a Web page back to the client who is connected to your Web site.
The following script will print built-in server information to the browser window in the form of a Web page that appears when a user connects to your site and that contains information that is generated on the fly.
Follow these steps to create the script and test it out:
1. Open Notepad.
2. Enter the first line of the script, which should point to the location of Perl on your own system:
#!C:/perl/bin/perl.exe
3. Press Return to enter a blank space for clarity.
4. Enter the following (these commands create a Web page that will present the environment variables you want to capture):
print "Content-type: text/html\n\n";
print"<BR>";
while (($key, $val) = each %ENV) {
print "$key = $val<BR>";
}
print"<BR>";
print "</body></html>";
5. Save your file with the file-name extension you use for Perl CGI scripts (the one you named with the AddHandler or AddType directive), such as .cgi or .pl. Locate the file in the directory where you want Apache to execute CGI scripts.
6. Start up your Perl application.
7. Launch your Web browser and access the script by entering a URL such as:
127.0.0.1/cgi-bin/variables.pl
The browser should show a page that displays your current environment variables.
The section Server-Related Variables lists the environment variables that Apache sets to provide information that the CGI needs to execute properly.
Previous
Table of Contents
Next
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.
Wyszukiwarka
Podobne podstrony:
Tu 154 wylądował, rozerwała go bomba szokująca książka151 25151 15 (3)154 13154 23MS3U2PDZPYEL7CEGIZ3KR3MXVFUCEDYI53HJY151 156 7mp43uhlml3v6grqw4usdl6usks7pprhrkvmq2i151 01 (2)12 (154)Rachunek za Smoleńsk o nieubezpieczonych pasażerach lotu TU 154154 07 (2)151 ind151 09 (2)154 01 (3)Wycofać Tu 154 z eksploatacji151 11 (3)151 16 (2)więcej podobnych podstron