Solved

I got CGI Header Error whe i execute my perl script

Posted on 2008-06-21
9
878 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
This is an entirely separate issue from your original question.  It would be best to separate these issues into 2 separate questions.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
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
Comment Utility
#!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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now