Databases Reference
In-Depth Information
print "Content-type: text/html; charset=UTF-8\n\n";
The charset tells the browser that the server will send text using the UTF-8 character set.
Example 18-1 is the complete script to generate the HTML page.
Example 18-1. A first CGI Perl script that just displays some text
#!/usr/bin/perl
print "Content-type: text/html; charset=UTF-8\n\n";
print "
<html>
<title>My first Perl CGI script</title>
<body>
<b>Hello, World!</b><br />
This is <b>very</b> interesting!
</body>
</html>
";
For CGI scripts to work properly, they must print the Content-type line before sending
any other data to the browser. Save this file as HelloWorld.cgi.pl in the appropriate
location under the web server document root as discussed earlier.
The CGI approach requires the web server to execute the program and display the
results; this means that the web server should have the necessary access permissions to
read and execute the Perl scripts. On Linux or Mac OS X systems, permissions are
assigned by three categories:
• The user who owns the file ( user )
• Users in the group that the file is in ( group )
• Everyone else ( other )
The Apache web server typically runs as the user nobody , who isn't in any group, so if
you want the web server to be able to run a file, you'll need to give permission for
everybody (the o permission, which stands for “other”) to read and execute the script.
For example, you can set appropriate permissions for the file HelloWorld.cgi.pl by typ-
ing the command:
$ chmod u=rwx,g=rx,o=rx HelloWorld.cgi.pl
We discuss permission settings in “Restricting access to files and directories” in Chap-
ter 2. The permissions for the XAMPP web server directories on Windows are less
stringent, and a nonprivileged user can place files in the cgi-bin directory for delivery
by the server.
Ensure your web server is running, then open the file in a web browser. On a Linux
system or a Windows system using XAMPP, the script can be accessed at http://host
name/cgi-bin/HelloWorld.cgi.pl , while on a Mac OS X system using XAMPP, the address
to use is http://hostname/xampp/HelloWorld.cgi.pl . The hostname is the name of the
 
Search WWH ::




Custom Search