While executing a perl script, it showed the following errors. Can anybody tell me what's wrong with the bottom script? Thanks a lot.

The server encountered an unexpected condition which prevented it from fulfilling the request.
The script did not produce correct headers. It must at least print out a Content-Type header, like so:
Content-Type: text/html
The header the script produced was:
HTTP/1.1 200 OK

And the script i used to print this out is:

#!/usr/bin/perl
use LWP 5.64;
use CGI qw(:standard -nph);
use strict;
use DBI;
my $ua = LWP::UserAgent->new(agent =>
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; T312461)' ,
'cookie_jar' => {file => "./lwpcookies.txt", autosave => 1, ignore_discard => 1}
);
#$ua->timeout(10);
$| = 1;
my $verbose=0;
my $recip=0;
my $is_category_text;
my $random_number = rand();
my $c_file="count_".$random_number.".txt";
#open(FILE1,">$c_file");
#Database connection object
my $dbh = DBI->connect("dbi:mysql:db_hhh:localhost", "hhh", "123456",{RaiseError=>1, AutoCommit=>1, ShowErrorStatement=>1}) or print "failed to connect to database\n";
my $query = new CGI();
my %form;
print header();
print <<EOF;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="none">
<title>Cheap Submission to Thousands of Free Directories</title>
<link href="../substyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="820" border="0" align="center" cellpadding="10" cellspacing="0" bgcolor="#F8F8E9">
<br />
<p class="outputtop" align="center"><b>Warning</b>: Depends on your internet connection, this process might take around one hour.<br>Please be patient and don't close the browser until the submission is completed.</p>

EOF

print $query->header();

Major Major, it's not working.

BTW, the script works fine in hostmonster server, but it's not working in another server at ipower.com. Can anybody help me fix this?

add this line anywhere in the script and see if it returns more specifc information about the error:

use CGI::Carp qw/fatalsToBrowser/;

KevinDC, it's same problem. The title of the browser showed 500 Internal Server Error.

make sure to upload the perl script in ASCII text mode (see you ftp applications help files if necessary) and that the permissions are set if necessary, normally a perl script is chmod 755, you can also do that with your ftp application.

That's not the reason, I tried that but still get the same problem. I tried to add a link like print "content-type: text\html\n\n"; Without the dynamic scripts, it will print out the texts, but with the dynamic scripts like connecting to database, it will stop working and showing network error.

Well, you have this line in the code you posted:

my $dbh = DBI->connect("dbi:mysql:db_hhh:localhost", "hhh", "123456",{RaiseError=>1, AutoCommit=>1, ShowErrorStatement=>1}) or print "failed to connect to database\n";

if the "or" condition is evaluated then the script will error out because you have not printed an HTTP header before trying to print "failed to connect to database\n";.

Print the HTTP header before the above line of code runs.

KevinADC, that's not the reason, since I tried to remove all the db connection with only the quoted scripts left and still the same error.

And if I add the line print "content-type: text/html" before the print header(); the result is like this http://www.cheapsubmit.net/cgi-bin/nph-dirsub2.pl

#!/usr/bin/perl
use LWP 5.64;
use CGI qw(:standard -nph);
use strict;
use DBI;
my $ua = LWP::UserAgent->new(agent =>
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; T312461)' ,
'cookie_jar' => {file => "./lwpcookies.txt", autosave => 1, ignore_discard => 1}
);
#$ua->timeout(10);

$| = 1;
my $verbose=0;
my $recip=0;
my $is_category_text;
my $random_number = rand();
my $c_file="count_".$random_number.".txt";
#open(FILE1,">$c_file");

print header();

print <<EOF;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="none">
<title>Cheap Submission to Thousands of Free Directories</title>
<link href="../substyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="820" border="0" align="center" cellpadding="10" cellspacing="0" bgcolor="#F8F8E9">
<br />
<p class="outputtop" align="center"><b>Warning</b>: Depends on your internet connection, this process might take around one hour.<br>Please be patient and don't close the browser until the submission is completed.</p>


EOF

try this:

#!/usr/bin/perl
print "Content-type: text/html\n\nHello World";

see if that simple script will work.

Are you sure you want or need to use the -nph option?

Yes, it's working with that simple script.

Do you mean the standard -nph cause the problem?

Yes, it's working with that simple script.

Do you mean the standard -nph cause the problem?

Try removing the -nph option and see if that clears up the problem. If you don't know what the -nph option does, chances are you don't need to use it.

KevinADC, the problem maybe caused by the script. If I remove this line: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />, and then add print "content-type: text/html\n\n"; beofre the print header(); it can print out except that it will print something like apache server time ... content-type....

Looks like we have to print out the content-type line first.

I just can't understand why the script works fine in my server at hostmonster, and why it's not working on another server at ipowerweb. Except changing the database connection username and password, I did nothing to the script, and then the script is not working on another server. Any comments for this problem? Most of the servers shall install the same perl module, right?

Why scripts work on one server and not another is difficult to impossible know why unless you know everything about what software is installed, what operating system the server uses, and etc.

But did you try removing the -nph option or not? That is mainly, if not specifically, for IIS servers, not Unix or Linux servers. -nph prints the headers in a different way, but I don't know the details, just try removing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.