Solved

I got CGI Header Error whe i execute my perl script

Posted on 2008-06-21
9
887 Views
Last Modified: 2013-12-25
I have a perl script to get a database value and print on browser.This script is running successfully Linux Server.But when i run this script in windows server it gives HTTP header error.i use WINDOWS 2003 IIs 6.0 web server and Perl 5.8.8 and mysql 5.0.
#!C:/bin/perl.exe  -w
use DBI;
#definition of variables
$db="music";
$host="localhost";
$user="username";
$password="pass";
 
 
print "Content-type:text/html\n\n";
 
#connect to MySQL database
my $dbh   = DBI->connect ("DBI:mysql:database=$db:host=$host",
                          $user,
                          $password) 
                          or die "Can't connect to database: $DBI::errstr\n";
 
#prepare the query
my $sth = $dbh->prepare( "SELECT * FROM trackinfo");
 
#execute the query
$sth->execute( );
## Retrieve the results of a row of data and print
print "\tQuery results:\n================================================\n";
 
while ( my @row = $sth->fetchrow_array( ) )  {
     print "@row\n";
}
warn "Problem in retrieving results", $sth->errstr( ), "\n"
        if $sth->err( );
 
exit;

Open in new window

0
Comment
Question by:imidms
9 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 21837840
Are you sure that IIS is configured to execute Perl scripts?

Do you have other Perl scripts that are working as expected, or do they all have this problem?

Have you checked the IIS log to see if it provides any clues to the problem?

Change the beginning of the script to this and report back with the exact error message.

#!C:/bin/perl.exe  -w
 
use CGI::Carp qw/FatalsToBrowser/;
use DBI;
#definition of variables

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 21838437
Best to use the CGI module as some IIS setups require NPH.

Change the top of your script to
use DBI;
use CGI::Carp 'fatalsToBrowser';
use CGI ':standard';
 
print header;

Open in new window

0
 

Author Comment

by:imidms
ID: 21843605
Thanks for your reply,
Actually some the programs are working but most of the programs is  simply print the same header error.
could you suggest  possible configuration errors and server errors.Also some program which is using session are gives some error

mkdir C:/: File exists at C:/Program Files/SWsoft/Plesk/Additional/Perl/site/lib/CGI/Session/Driver/file.pm line 27


#!C:\Program Files\SWsoft\Plesk\Additional\Perl\bin\perl.exe
use CGI qw/:standard/;
use LWP::UserAgent;
use HTTP::Request qw(GET POST);
use HTTP::Headers;
 
use CGI::Cookie;
use CGI::Session;
 
use CGI::Carp qw(fatalsToBrowser);
 
my $q=new CGI;
 
my $c=new CGI::Cookie;
 
my $cIP = $ENV{'REMOTE_ADDR'} ;
 
# replace this value with license key
my $license_key ="mykey";
my $ua = LWP::UserAgent->new(timeout =>10);
 
my $h = HTTP::Headers->new;
 
$h->content_type('application/x-www-form-urlencoded');
 
my $request = HTTP::Request->new('POST','http://maxmind.com:8010/a',
                                 $h,"l=$license_key&i=$cIP");
my $res = $ua->request($request);
 
my $content = $res->content;
#print "Content-type: text/html\n\n" ;
############ again send request	#######
if($content eq "")
{
 sleep(1);
 
 $request = HTTP::Request->new('POST','http://maxmind.com:8010/a', 
                               $h,"l=$license_key&i=$cIP");        
 $res = $ua->request($request);                                    
                                                                   
 $content = $res->content;                                         
}
if($content eq "")
{
 sleep(1);
 
 $request = HTTP::Request->new('POST','http://maxmind.com:8010/a', 
                               $h,"l=$license_key&i=$cIP");        
 $res = $ua->request($request);                                    
                                                                   
 $content = $res->content;                                         
}
if($content eq "")
{
	$content="Server-Error";
}
 
my $session = "" ;
 
my $sid = "" ;
 
$sid = _Checkcookie();
 
if($sid eq "")
{
 $session = new CGI::Session("driver:File", $q, {Directory=>'/tmp'});
 
 my $server = $ENV{'HTTP_HOST'};
 
 my $path = "/";
 
 $sid = $session->id();
 
 print "Set-Cookie: CGISESSID\=$sid; path\=$path;domain\=$server\n" ;
}
else
{
 $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
}
 
my $cFlag = 0 ;
 
my $referer=$q->param('referer');
 
my $albumid=$q->param('albumid');
 
my $action=$q->param('action');
 
my $shopping_info=$session->param('shopping_info');
 
my %shopping_info=%{$shopping_info};
 
$shopping_info{geo}=$content; 
 
if($referer eq "dinamalar"){
$shopping_info{referer}="dinamalar";
}
else{$shopping_info{referer}="";}
 
$session->param("shopping_info",\%shopping_info);
 
$session->expire('+12h');
 
if($action eq "direct"){
print $q->redirect(-uri=>'http://www.imi.in/cgi-bin/elearning/query/el_alb_details.pl?albumid=$albumid&action=geo',
                           -nph=>1,
                           -status=>302);
#print "Status: 302 Moved", "\r\n","Location: http://74.55.90.196/cgi-bin/elearning/query/el_alb_details.pl?albumid=$albumid&action=geo", "\r\n\r\n";
exit;
}
 
#print 'Status: 302 Moved', "\r\n",'Location:/cgi-bin/elearning/query/el_show_cat.pl', "\r\n\r\n"; 
 
print $q->redirect(-uri=>'http://www.imi.in/cgi-bin/elearning/query/el_show_cat.pl',
                           -nph=>1,
                           -status=>302);
 
exit;
 
sub _Checkcookie{
 
my %cookies=fetch CGI::Cookie;
 
my $name=$cookies{CGISESSID};
 
my @temp=split(/CGISESSID\=/,$name);
 
my @temp1=split(/;/,$temp[1]);
 
return $temp1[0];
}

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 28

Expert Comment

by:FishMonger
ID: 21843730
This is an entirely separate issue from your original question.  It would be best to separate these issues into 2 separate questions.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 21843791
It appears that both of these separate issues have a couple things in common.  The first being that they are not running under strictures i.e they're missing

use strict;

Also, it's better to use/load the warnings pragma instead of  the -w switch as you have in your first code snippet.

use warnings;

You haven't specified the exact error message that you're receiving, but if you're receiving a "500 internal server error then that's caused by the script sending output, usually an error message, before outputting the headers.  If you add the line that I and Tintin suggested to the beginning of your script(s) (use Tintin's, mine had a slight typo), it should output a more descriptive error message in the browser.
0
 

Author Comment

by:imidms
ID: 21846033
#!C:\Program Files\SWsoft\Plesk\Additional\Perl\bin\perl.exe
use CGI ':standard';
use LWP::UserAgent;
use HTTP::Request 'GET POST';
use HTTP::Headers;
use warnings;

use Cwd;

use CGI::Cookie;
 
use CGI::Carp 'fatalsToBrowser';

my $q=new CGI;

my $c=new CGI::Cookie;
 
my $cIP = $ENV{'REMOTE_ADDR'} ;

# replace this value with license key
my $license_key ="mykey";
 
my $ua = LWP::UserAgent->new(timeout =>10);

my $h = HTTP::Headers->new;

$h->content_type('application/x-www-form-urlencoded');

my $request = HTTP::Request->new('POST','http://maxmind.com:8010/a',
                                 $h,"l=$license_key&i=$cIP");
my $res = $ua->request($request);

my $content = $res->content;
#print "Content-type: text/html\n\n" ;
############ again send request      #######
if($content eq "")
{
 sleep(1);

 $request = HTTP::Request->new('POST','http://maxmind.com:8010/a',
                               $h,"l=$license_key&i=$cIP");        
 $res = $ua->request($request);                                    
                                                                   
 $content = $res->content;                                        
}
if($content eq "")
{
 sleep(1);
 
 $request = HTTP::Request->new('POST','http://maxmind.com:8010/a',
                               $h,"l=$license_key&i=$cIP");        
 $res = $ua->request($request);                                    
                                                                   
 $content = $res->content;                                        
}
if($content eq "")
{
      $content="Server-Error";
}

 
my $cFlag = 0 ;

my $referer=$q->param('referer');

my $albumid=$q->param('albumid');

my $action=$q->param('action');

 
my %shopping_info=%{$shopping_info};

$shopping_info{geo}=$content;
 
if($referer eq "dinamalar"){
$shopping_info{referer}="dinamalar";
}
else{$shopping_info{referer}="";}
 
if($action eq "direct"){
print $q->redirect(-uri=>'http://74.55.90.196/cgi-bin/elearning/query/el_alb_details.pl?albumid=$albumid&action=geo',
                           -nph=>1,
                           -status=>302);
 exit;
}


print $q->redirect(-uri=>'http://74.55.90.196/cgi-bin/elearning/query/el_show_cat.pl',
                           -nph=>1,
                           -status=>302);

exit;

Please debug this code.if you find any error please let me know.
 
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21846376
Add this as the second line in your program(the first beginning with #!), which was suggested earlier.  

use CGI::Carp 'fatalsToBrowser';

Open in new window

0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 21847270
Line 81 has a problem.  After you add the CGI::Carp line that we've suggested, take a close look at problematic line.
0
 

Author Comment

by:imidms
ID: 21852714
Thank you so much that problem has sorted but i have some other problem in session module.i have use session in the same program But it says permission denied error

#!C:\Program Files\SWsoft\Plesk\Additional\Perl\bin\perl.exe
use CGI::Carp 'fatalsToBrowser';
use CGI ':standard';
use LWP::UserAgent;
use HTTP::Request 'GET POST';
use HTTP::Headers;
use warnings;
use Cwd;
use CGI::Session;
use CGI::Cookie;
 

my $q=new CGI;

my $c=new CGI::Cookie;
 
my $cIP = $ENV{'REMOTE_ADDR'} ;

# replace this value with license key
my $license_key ="mykey";
 

my $ua = LWP::UserAgent->new(timeout =>10);

my $h = HTTP::Headers->new;

$h->content_type('application/x-www-form-urlencoded');

my $request ='';

$request=HTTP::Request->new('POST','http://maxmind.com:8010/a',
                                 $h,"l=$license_key&i=$cIP");
my $res = $ua->request($request);

my $content = $res->content;
#print "Content-type: text/html\n\n" ;
############ again send request      #######
if($content eq "")
{
 sleep(1);

 $request = HTTP::Request->new('POST','http://maxmind.com:8010/a',
                               $h,"l=$license_key&i=$cIP");        
 $res = $ua->request($request);                                    
                                                                   
 $content = $res->content;                                        
}
if($content eq "")
{
 sleep(1);
 
 $request = HTTP::Request->new('POST','http://maxmind.com:8010/a',
                               $h,"l=$license_key&i=$cIP");        
 $res = $ua->request($request);                                    
                                                                   
 $content = $res->content;                                        
}
if($content eq "")
{
      $content="Server-Error";
}


 my $session = "" ;

my $sid = "" ;

$sid = _Checkcookie();
 
if($sid eq "")
{
 $session = new CGI::Session("driver:File", $q, {Directory=>'C:/Inetpub/vhosts/imi.in/cgi-bin/tmp'});

 my $server = $ENV{'HTTP_HOST'};

 my $path = "/";

 $sid = $session->id();

 print "Set-Cookie: CGISESSID\=$sid; path\=$path;domain\=$server\n" ;
}
else
{
 $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
}


my $cFlag = 0 ;

my $referer=$q->param('referer') || '';

my $albumid=$q->param('albumid') || '';

my $action=$q->param('action') || '';
 
my $shopping_info=$session->param('shopping_info');

my %shopping_info=%{$shopping_info};

$shopping_info{geo}=$content;
 
if($referer eq "dinamalar"){
$shopping_info{referer}="dinamalar";
}
else{$shopping_info{referer}="";}

$session->param("shopping_info",\%shopping_info);
 
$session->expire('+12h');

 
if($action eq "direct"){
print $q->redirect(-uri=>'http://74.55.90.196/cgi-bin/elearning/query/el_alb_details.pl?albumid=$albumid&action=geo',
                           -nph=>1,
                           -status=>302);
print 'Status: 302 Moved', "\r\n",'Location:http://74.55.90.196/cgi-bin/elearning/query/el_alb_details.pl?albumid=$albumid&action=geo', "\r\n\r\n";

 exit;
}

print 'Status: 302 Moved', "\r\n",'Location:http://74.55.90.196//cgi-bin/elearning/query/el_show_cat.pl', "\r\n\r\n";

exit;

 sub _Checkcookie{

my %cookies=fetch CGI::Cookie;

my $name=$cookies{CGISESSID};

my @temp=split(/CGISESSID\=/,$name);

my @temp1=split(/;/,$temp[1]);

return $temp1[0];
}

My error

mkdir /: Permission denied at C:/Program Files/SWsoft/Plesk/Additional/Perl/site/lib/CGI/Session/Driver/file.pm line 27

Actually i have use File driver to  store session ID so i store in 'C:/Inetpub/vhosts/imi.in/cgi-bin/tmp' path but it says permission denied problem.

Please help on this

Regards
Rajiv
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question