Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems Running Cgi Scripts 1

Status
Not open for further replies.

jsawyer

MIS
Jan 1, 2003
5
US
I have just set up my apache web server so that I can use the cgi-bin. I made a little hello world script and every thing sorks fine. I then Downloaded a script and tried to run it from my web browser. I get this error:

[Thu Jan 23 21:12:09 2003] [error] [client 10.10.1.10] Premature end of script headers: /var/
I then downloaded another scrip and got the same error. could there be something wrong with my apache configuration that prevents me from running certain scripts? below is an example of wone of the scripts that I downloaded.

$sURLFilter = '';
$sIPFilter = '';
$sHostFilter = '';
$sCounterFilter = '';
$bAutoCreate = 0;


#---------------------------------------------------------------------------

{
local( $__COUNTER_DAT ) = 'livecntr.dat';

print "Content-type: text/plain\nPragma: no-cache\nCache-control: no-cache\nExpires: Mon, 28 Apr 1997 00:01:00 -0500\n\n";

if( $ARGV[0] )
{
local( $nCount ) = 0;
local( $Temp );

if( '' ne $ARGV[4] )
{
$ENV{'HTTP_REFERER'} = $ARGV[4];
}

if( $ARGV[1] && ( '0' ne $ARGV[1] ) )
{
$__COUNTER_DAT = $ARGV[1] . '.lcd';
}

if( -w "$__COUNTER_DAT" )
{
local( $bInc ) = 0;

if( open( F, &quot;+<$__COUNTER_DAT&quot; ) )
{
flock( F, 2 );
seek( F, 0, 0 );

( $Temp, $nCount ) = split( /\s/, <F> );

if( 'i' eq $ARGV[0] )
{
local( $bFiltersOk ) = 1;

if( $sURLFilter && ( lc( $ENV{'HTTP_REFERER'} ) !~ $sURLFilter ) )
{
$bFiltersOk = 0;
}
if( $bFiltersOk &&
$sIPFilter && ( $ENV{'REMOTE_ADDR'} =~ $sIPFilter ) )
{
$bFiltersOk = 0;
}
if( $bFiltersOk &&
$sHostFilter && ( $ENV{'REMOTE_HOST'} =~ $sHostFilter ) )
{
$bFiltersOk = 0;
}
if( $bFiltersOk &&
$sCounterFilter && ( $__COUNTER_DAT =~ $sCounterFilter ) )
{
$bFiltersOk = 0;
}

if( $bFiltersOk )
{
seek( F, 0, 0 );

++$nCount;

print F &quot;Total: $nCount\n&quot;;

$bInc = 1;
}
}

flock( F, 8 );
close( F );
}

}
elsif( $bAutoCreate )
{
if( ( !( -e &quot;$__COUNTER_DAT&quot; ) ) && open( F, &quot;>$__COUNTER_DAT&quot; ) )
{
print F &quot;Total: 0\n&quot;;
close( F );
}
}

print &quot;c0=$nCount\n&quot;;
if( 'v' eq $ARGV[0] )
{
print &quot;vr=99AX\n&quot;;
}
}
}
 
I don't find any syntax problems with the code you listed above. Nor, do I think apache has anything to do with the problem. If you can run a simple 'hello_world', then your apache setup is probably sufficient. More likely, there is a file dependency or other system interaction that your program lacks sufficient authority to perform. Make sure the cgi daemon has sufficient permissions to create/open/read all associated files.

Additionally, you could add a little intelligent error handling to the code with CGI::Carp 'fatalsToBrowser'...

Here is a very small example of sending errors to the browser..
Code:
#!/usr/local/bin/perl
use CGI;
use CGI::Carp 'fatalsToBrowser';

die &quot;JUNK\n&quot;;


If you employ the 'fatalsToBrowser' trick, you'll get much more meaningful error messages to the browser....

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
GOboatin,

I'm having problems getting a script to execute as well. Could you explain a little bit about this?
#!/usr/local/bin/perl
use CGI;
use CGI::Carp 'fatalsToBrowser';

die &quot;JUNK\n&quot;;

If I add this to my perl script on my unix box are you saying I should go to the website, click on the link to this script and I should get detailed errors relating to my problem?

TIA
 
Code:
#!/usr/local/bin/perl
use CGI;
use CGI::Carp 'fatalsToBrowser';

die &quot;JUNK\n&quot;;
[code]

The normal where's the perl line...
[red]#!/usr/local/bin/perl[/red]


This loads the CGI module with it's standard methods.
[red]use CGI;[/red]


This loads an additional method(fatalsToBrowser) from the CGI module bundle.
[red]use CGI::Carp 'fatalsToBrowser';[/red]
Loading 'fatalsToBrowser' tells the CGI module that you want your errors to go to the browser.  If you have a syntax error or file permission problem or other fatal problem in your code, then those errors will show up in  the browser.

This is simply causing an error, thus illustrating what CGI::Carp 'fatalsToBrowser'; does...
[red]die &quot;JUNK\n&quot;;[/red]

The web server expects the CGI code to produce a compliant http header.  If you left out the &quot;use CGI::Carp 'fatalsToBrowser'&quot; line, and did the 'die', then the web server (which expects the CGI code to produce a compliant http header) will gripe and show you a server error, not the perl interpreters error.  

With the &quot;use CGI::Carp 'fatalsToBrowser'&quot; line, the perl is smart enough to recognize that the code has misbehaved and it wraps the error message in just enough http stuff to make the web server happy and get the error to the browser..... ergo - fatalsToBrowser. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I have to ask the obvious - have you configured Apache to accept a script with a .cgi[/] file extension? There's always a better way...
 
Goboating,

Thanks again for the info. Just have one more question if you don't mind. I edited the cgi file on my Unix system and entered the commands you listed. The section of my webpage this script is suppose to execute is a &quot;Site Search&quot;. Now will I see the errors when I go to the web page and click on the Site Search? Or will I see the errors somewhere else? I do not see any errors when attempting to open the site search, I just get the &quot;page not found&quot;.

Jsawyer - Sorry for intruding on your thread.

TIA,
Zoey
 
You're not intruding.... we're all one big happy confused family here....;-)

The general flow....
With a browser, you make an http request of a web server.
The web server parses your request, identifies the appropriate page to return and then sends its http response (the web page).

If the request is for a cgi page, then the web server has to find the program to run, run it, and funnel the output from the program back to the requestor (browser).

If you have problems in your code, you would see the errors show up when you ask for the cgi to be run.

In order for the CGI::Carp 'fatalsToBrowser' trick to work, the web server must first find and try to run your code. Given the error you are getting, the web server is not finding your code (or it is configured to give very vague error messages). Most likely, the web server is not finding your code were you think it should. I would examine the location of the cgi code carefully (it should be in a cgi-bin) and make sure that the <form action=''> tag in the site search page is really pointed at the location of the site search code.
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks for the info. I don't believe I have a <form action=&quot;> tag anywhere. Would you be able to give me insight on where to place that statement? I assume I would have to put this statement in the html code and point it to the cgi script. The script is located in the cgi-bin and I have perl on the unix server.

Thanks for all your help.

Zoey
 
Yes. You will need the action tag in your html input form and it must point at the cgi code in your cgi-bin. You can see a concise explanantion of this stuff in faq452-653.

After reading the faq, please come back with any questions. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
The <form action=&quot;...&quot;>... tags are where you put your search box to enter your search terms. This may be either used to call the CGI, or output by the CGI to call itself. Using a link is generally appropriate as well. However, if you just put the address of the CGI in your URL and it is not found, then you are using the wrong location. Even if it returned an error, there would be something other than &quot;page not found&quot;. If it is in a cgi-bin as you said, then the address should be something like Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Thanks for all your help. I did try to add a form statement to the html code (not the cgi script, but from frontpage), and I read the FAQ so I have some real good info to follow now. I am still unable to get this cgi script to run. I have rechecked the cgi file and cannot find anything wrong. However, at the end of the compilation I do get this error, &quot;sub compilequery failed print main: : stderr &quot;Ice2: query compilation failed\n&quot;; (Can anyone make sense of this error).

This cgi script is suppose to setup a site search area of my helpdesk where I can enter info into the search field and it will find it. This script to my knowledge has never worked. It almost acts like it is not even an executable. Do you know of any faqs or web sites that talk about search scripts?



Thanks again all.
 
A search script is no different in most regards than any other. If you received an error from the script itself, then it is definitely being interpreted, so the executable bit is set. I've never seen an error quite like you recieved, but it sounds like a custom one. Do a search through the script for &quot;failed print&quot; or &quot;compilequery failed&quot;, and see what code is surrounding that statement. If you post it here, we might be able to help debug it. Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top