Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Perl-CGI Apache Server Error

Perl-CGI Apache Server Error

Perl-CGI Apache Server Error

I am totally new to UNIX, LINUX, Apache and CGI. This is the first time I left the
comfort of Cold Fusion on NT...

I wrote a Perl script that allows the user to add links to a page from
another (data entry) page.

When the user presses the submit button the page DOES update with the new
link, but instead of giving a thank you message I get a Internal Server
Error with no number.

I talked to the Internet provider and they agree that the perl script works
but they are not sure why we are getting a server error. I suggested that
the server (Apache) or the OS (LINUX) needed to be configured differently
but the provider doesn't think so since they are not having problems
elsewhere (Good Point).

I set all files and directories to 777, 666, and tried some others. Besides
the script does update the page, it's after the page updates that I get
this error.

It seems that maybe I need to tell the server where to go when it's done
updating the page.
But I checked other perl CGI scripts and they never seem to have anything
other then a print statement with HTML at the end of the process just like
my script.

THIS IS THE PERL SCRIPT... (second.pl)

# get form input, split each form field into @pairs array
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

# clean up each array element
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\n/ /g; # replace newlines with spaces
$value =~ s/\r//g; # remove hard returns
$value =~ s/\cM//g; # delete ^M's
$FORM{$name} = $value;

# add links to this file
$out = "../test/edit/LegalPicks.html";

# open file for append, add links
open(OUT, ">>$out");

# This locks the file so no other CGI can write to it at the
# same time...

# Reset the file pointer to the end of the file, in case
# someone wrote to it while we waited for the lock...

print OUT 'Other Stuff99'."\n";
print OUT "<a href=\"http://lightsphere.com/\";>foo</a>"."\n";

close OUT;

print <<EndHTML;
content-type text/html
<html><head><title>Thank You</title></head>
<h2>Thank You!</h2>
Thank you for your feedback.<p>
<a href="index.html">Return to our home page</a><p>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<title> Add Links Form</title>


<form method="post" action="../../cgi-bin/second.pl">

<td><input type="text" size="40" maxlength="40"></td>
<td><input type="text" size="40" maxlength="40"></td>
<td><textarea name="requiredMessage" wrap="VIRTUAL" rows="15" cols="50"></textarea></td>
<input type="submit" name="addLink" value="AddLink">


Any help would be great


RE: Perl-CGI Apache Server Error

I don't know if it's going to be this simple, but... You've got code to start off the page output that looks like this:

print <<EndHTML;
content-type text/html

Don't you need to have a blank line after the content-type and before the <html>? Also, should the content-type be printed as:

content-type: text/html

ie, a ":" after the "content-type".

You might want to take a look at the CGI module that's provided with perl. This provides a lot of functions for dealing with CGI input and HTML output that can save a lot of grief in the long run :)

"perldoc CGI" should display the perl online manual pages about the module itself.

Hope this helps.

RE: Perl-CGI Apache Server Error

( I have not played with FLOCK much, but.....)
it appears that you are trying to 'flock' a file handle that does not exist. The output file you are trying to 'lock' is attached to handle 'OUT'. You then try to lock and seek on handle 'OUTF'. I think the 'open', 'flock', 'seek', 'print', and 'close' need to be operating on the same handle.

Good Luck

RE: Perl-CGI Apache Server Error


In addition to the missing colon, your print content-type identifier needs to end with at least one blank line so the browser recognizes the end of the header. I'd print it separately, before your print <<EndHtml; line, like this:

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

Hope this helps.

RE: Perl-CGI Apache Server Error

I have this same problem, and it's not a problem with your perl program. It appears that there's some kind of configuration necessary for Apache to run perl CGI... I can make a simple bash script that prints a web page, and it runs fine in CGI. I can make a perl program that does the same thing, and it won't run under Apache. This same perl program runs fine under Pi3Web on Win95. Perhaps you can only do perl under Apache with mod_perl. I'm going to try that. Bobby Martin bobbymartin@NOSPAMhotmail.com

RE: Perl-CGI Apache Server Error

Well, I finally fixed my problem.  It seems it was due to a combination of file artifacts from copying the file from a dos file system to a linux file system, and some configuration issues with Apache.

Here's what I had to do to solve the problem:
1) create a new file under linux and type in the exact same script again
2) change the script name from *.pl to *pl  (notice the lack of a .)

The reason you have to retype the file (I think) is that in DOS the carriage returns are 0xA 0xD while in Linux they're just 0xA.  You will see that manifest as ^M at the end of each line, but even after removing the ^M I still had the problem.  The dos-to-unix text conversion utility (don't remember the name offhand) might fix your problem, rather than having to type the file again.  After I typed in the new file I diffed it with the old (from DOS) file, and every line showed different even though there were no visible differences in the lines.

As for the .pl -> pl change, that is because my server returns the output from a .pl file as a .pl file for some reason, instead of as CGI output.  I suspect I have my server misconfigured somehow.  As a temporary solution, changing the extension from .pl to no extension solved the problem.

Email me at bobbymartin@NOSPAMhotmail.com if you have any questions or suggestions about my Apache configuration.

RE: Perl-CGI Apache Server Error

the dos-to-unix conversion utility is.....
dos2unix    original_name      new_name <enter>

keep the rudder amid ship and beware the odd typo

RE: Perl-CGI Apache Server Error

Just in case it helps at all, I remember having troubles getting Apache working with CGI on an HP-UX machine due to the ScriptAlias directories not being set up correctly. I presume that the linux version of Apache is configured in the same way and so could cause 404 script/page not found errors.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close