Home Frequently Asked Questions CGI

 

 


This page answers common questions related to Common Gateway Interface (CGI).

Questions

 
 
 
 
 
 
 
 

 


 

 

Q: When trying to run a perl/CGI script, I received the following message:

Error
This error is very likely due to a mistake in your program
.

 

What does this mean and what should I do about it?

 

A: This means that you have an error in your CGI script. Follow the steps below to troubleshoot and correct your problem:

 

 
  1. Find the error with the checklog utility. Type the following at the vulcan command line:
    checklog

    You will see all of the entries related to your account in the Apache error log. Find the error related to the specific script in question and write it down.

  2. If the error in question is Premature end of script headers then follow the instructions in this faq.

  3. Otherwise, search the internet for information related to your error. For instance, searching for your error in google will usually reveal that many other people received this error and that many solutions to the problem are available.

  4. If searching the internet is not providing you with the information you need, read all of this faq and see if anything here applies to your problem.

  5. If after reading this document and searching the internet you still cannot solve your CGI script problem, send your error message and any other relevant information to your Professor for guidance.

 

Q: I checked my log files and I found the Premature end of script headers error? What should I do?

 

A: The Premature end of script headers error is a very common error in the perl/CGI development world. Check all of the following to troubleshoot the error:

 
  1. Confirm that directory and file permissions are set correctly.
    Use the checkperm tool to inspect the permissions of your files.
    Example: If the user with username (user) runs the checkperm utility, the output of the command looks as follows:

  2. [user@vulcan user]$ checkperm
    drwxr-xr-x 19 user students 4096 Oct 12 18:36 /home/user
    drwxr-xr-x 4 user students 4096 Dec 3 2002 /home/user/public_html
    drwxr-xr-x 2 user students 4096 Oct 10 08:25 /home/user/public_html/cgi-bin
    /home/user/public_html/cgi-bin:
    total 48
    -rwxr-xr-x 1 user students 1690 Jul 24 2002 addForum.pl
    -rwxr-xr-x 1 user students 1586 Jul 24 2002 addPost.pl
    -rwxr-xr-x 1 user students 4077 Dec 3 2002 calprog.cgi
    -rwxr-xr-x 1 user students 459 Apr 15 2002 dbtest2.pl
    -rwxr-xr-x 1 user students 500 Jul 17 2002 dbtest3.pl
    -rwxr-xr-x 1 user students 1166 Jul 24 2002 default.pl
    -rwxr-xr-x 1 user students 1442 Jul 24 2002 forum.pl
    -rwxr-xr-x 1 user students 135 Nov 30 2001 newtest.pl
    -rw-r--r-- 1 user students 5663 Jul 17 2002 sqlnet.log
    -rwxr-xr-x 1 user students 600 Jul 16 2002 test_mysql.cgi
    -rwxr-xr-x 1 user students 600 Jul 16 2002 test_mysql.pl
  3. Note that all directories have the same permission set: drwxr-xr-x.
    Note that all *.pl and *.cgi files have the same permission set: -rwxr-xr-x.
    Confirm that the permissions in your directories are correct.
    Confirm that your username appears in the owner column -- where user is displayed in this example.
    Confirm that the group column is students.
    If the permissions or ownership of any of these files is wrong, a Premature end of script headers error may result.
    Use the following command to repair your permissions:
    chmod 755 /home/$USER/filename.pl
    where filename.pl is the name of the *.pl,*.cgi, or directory you wish to change.
    Use the following command to repair your owner and group settings:
    chown $USER:students /home/$USER/filename.pl where filename.pl is the name of the *.pl,*.cgi, or directory you wish to change.

  4. Confirm that you have the correct shebang. The shebang should look as follows: #!/usr/bin/perl

  5. Confirm that you output the appropriate headers somewhere beneath the shebang.
    print "Content-Type: text/html\n\n";

  6. Confirm that you are uploading your files in the ASCII format rather than the binary format. If you upload your files from a Windows machine to a UNIX machine in binary format, you will have a great deal of difficulty getting your code to work.

 

Q: How do I check the error log for errors generated by my CGI scripts?

 

A: Use the custom utility: checklog
When you the checklog tool from the command line, you will see output similar to the following:
 

[user@vulcan user]$ checklog
[Sun Oct 12 17:59:45 2003] [error] [client 99.98.97.96] File does not exist: /home/user/public_html/stupid
[Mon Oct 13 14:19:34 2003] [error] [client 172.17.100.200] Options ExecCGI is off in this directory: /home/user/public_html/default.pl
[Mon Oct 13 14:20:21 2003] [error] [client 172.17.100.200] script not found or unable to stat: /home/user/public_html/cgi-bin/defaul1t.pl
[Mon Oct 13 14:20:32 2003] [error] [client 172.17.100.200] File does not exist: /home/user/public_html/cgi-bin/default.p
[Mon Oct 13 14:20:53 2003] [error] [client 172.17.100.200] File does not exist: /home/user/public_html/cgi-bin/error.html

 

Q: How do I confirm that I am uploading my CGI scripts in the ASCII format in SSH?

 

A: In your Secure Shell client:
 
  1. Click edit-->settings



  2. Scroll to and expand File Transfer and then click on Mode



  3. Add *.pl and *.cgi as ASCII file types.

 

Q: What is the shebang statement? What should the shebang statement be for my CGI scripts on vulcan?

 

A: #!/usr/bin/perl

 

Q: What permissions do my CGI scripts require in order to execute properly?

 

A: The permissions required to run CGI scripts from your cgi-bin are as follows:
 

drwxr-xr-x 19 user students 4096 Oct 12 18:36 /home/user
drwxr-xr-x 4 user students 4096 Dec 3 2002 /home/user/public_html
drwxr-xr-x 2 user students 4096 Oct 10 08:25 /home/user/public_html/cgi-bin
/home/user/public_html/cgi-bin:
-rwxr-xr-x 1 user students 1690 Jul 24 2002 addForum.pl

 

Note that user should be your username.
Note that each directory and file requires 755 permissions.
Note that each file is owned by user and belongs to the students group.
Run the checkperm utility to confirm that:

 
  1. All permissions are 755.

  2. The owner and group settings for all directories and files are set to your username and the students group.

 

Q: I developed programs, scripts, pages, etc, on a Windows PC. Why won't they work when I move them to a UNIX machine?

 

A:

UNIX is different from PC Operating Systems like Windows. Java is portable: Operating System environments are not.

If you are experiencing problems with files you moved from a windows machine to a UNIX machine, check the following:

 

File System differences

  Windows machines generally reference files as follows:
c:\java-application\JavaFile.class

UNIX does this differently:
/home/s00-c000-s00/javaserver/WEB-INF/classes/JavaFile.class

Therefore, if you have any code or HTML that depends upon the specific location of other files, be sure to change this code to suit the UNIX environment. There is no c: drive on a UNIX machine!

Suggestion: Use relative addressing whenever possible.

 

 

Case Sensitivity

 

Windows ignores the case of the characters you use in many contexts.
A windows system does not differentiate between:

 
  • DEFAULT.HTM
  • Default.Htm
  • default.htm
   

UNIX, on the other hand, is case sensitive. UNIX would allow three files named DEFAULT.HTM, Default.Htm, and default.htm to exist in the same directory.

 

Therefore, confirm that your code or HTML uses case consistently.

 

 

Q: Where can I find some sample CGI scripts?

 

In addition to the large number of online references, we have two sample scripts here.

 

CGI script that connects to an Oracle database

#!/usr/bin/perl

use DBI;
use CGI qw/:standard/;

$dbh=DBI->connect ("DBI:Oracle:host=apollo;sid=ORA1;port=1521","user","password") or die "Did not connect to database";

$sth=$dbh->prepare("select Name, Notes from people");
$sth->execute;

print "Content-Type: text/html\n\n";

while (($Name,$Notes) = $sth->fetchrow()) {
print $Name;
print " ";
print $Notes;
print "
";
}
$sth->finish;
$dbh->disconnect;

 

CGI script that connects to a MySQL database

#!/usr/bin/perl

use CGI;
use DBI;
print "Content-Type: text/html\n\n";
my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, $sth);

$databaseName="DBI:mysql:database_name";
$databaseUser="user";
$databasePw="password";

$dbh=DBI->connect($databaseName, $databaseUser, $databasePw)||die "Connect failed: $DBI::errstr\n";

$sth = $dbh->prepare("select * from people")|| die "prepare: $$stmt: $DBI::errstr";

$sth->execute || die "execute: $$stmt: $DBI::errstr";
while (($name, $notes) = $sth->fetchrow()){
print $name;
print " ";
print $notes;
print "
";
}
$sth->finish();
$dbh->disconnect();



If you have any material (questions or answers) that may be useful here, send them to csis@pace.edu