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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with my script

Status
Not open for further replies.

optionalreaction

Programmer
Feb 27, 2005
7
GB
Can anyone tell me what is wrong with this???...

#!/usr/bin/perl

########################################
## INITIALIZE CGI ######################
########################################

use lib qw(.);
use CGI;
use CGI::Carp qw(fatalsToBrowser);
$cgi = new CGI;

########################################
## MAIN ################################
########################################

$DATAFILE = "hitcount.dta";
$val = 0;

if (open(RECORD, "$DATAFILE"))
{
$val = $_;
close(RECORD);
}

$val++;

if (open(RECORD, ">$DATAFILE"))
{
print(RECORD $val);
close(RECORD);
}

# output to webpage
print $val

########################################
## EXIT ################################
########################################

exit;

########################################
## END OF FILE #########################
########################################

... I am calling is with a <!--exec cgi="blah blah blah"--> command.

Any help much appreiciated,
Keith
 
even though you call it through an SSI tag you still have to print a header in the cgi script before printing something out to the browser. You might also find the virtual tag works if the exec tag does not work at all.

I have a feeling this:

$DATAFILE = "hitcount.dta";

should be:

$DATAFILE = "hitcount.dat";

Try it like this:

Code:
#!/usr/bin/perl

#use lib qw(.); # <-- not needed from what I can see
use CGI qw/:standard/; #<-- you only need standard
#use CGI::Carp qw(fatalsToBrowser);#<-- only uncomment for debugging
#$cgi = new CGI;

$DATAFILE = 'hitcount.dta';

open(RECORD, "$DATAFILE") or die "Unable to open file: $!";
$val = <RECORD> || 0;#<-- initialize counter if there is no value
close(RECORD);

$val++;

open(RECORD, ">$DATAFILE") or die "Unable to open file: $!";
flock(RECORD, 2);#<-- lock the file if your system supports it   
print RECORD $val;
close(RECORD);

print header();
print $val

exit;
 
make sure to put a semi-colon after:

print $val;

its missing in my post above and your post, without the semi-colon I doubt it will print to the screen.
 
I think the problem is with SSIs. I cannot get any kind of out put at all; I don't think the script is being called by the <--#exec cgi command.

Is there anything I could try?

Keith
 
are you putting the SSI tags in the proper document? Most servers only parse SSI tags in .shtml or .shtm documents.

Are you using the correct path to your script in the SSI tag?

<!--#include virtual="/cgi-bin/yourscript.cgi" -->

or whatever the path is relative to the directory the document is in.
 
I renamed the .htm to .shtml and now I get [an error occured when processing this directive] when using #include virtual, nothing with #exec cgi.

Is there any way I can call a CGI script from a webpage without using .shtml as the extention (all my files use .htm ATM)?

And, what does [an error occured when processing this directive] mean?

Thanks for your help so far,
Keith
 
When I call this ...

Code:
#!/usr/bin/perl

use lib qw(.);
use CGI;
use CGI::Carp qw(fatalsToBrowser);
$cgi = new CGI;

print header();
print "Hello World";

exit;

... I get nothing.

Keith
 
Code:
[Mon Feb 28 13:16:00 2005] test.pl: Undefined subroutine &main::header called at test.pl line 8.
Content-type: text/html

<h1>Software error:</h1>
<pre>Undefined subroutine &amp;main::header called at test.pl line 8.
</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message 
and the time and date of the error.

</p>

I get this, check your error logs, or run the script on the command line perl script.pl, and see what output you get


Try this
Code:
#!/usr/bin/perl
#use lib qw(.);    dunno what this is being used for
use CGI;
use CGI::Carp qw(fatalsToBrowser);
$cgi = new CGI;

print $cgi->header();
print "Hello World";

exit;

HTH
--Paul

cigless ...
 
That's it! I got it going. It looks like this...

Code:
#!/usr/bin/perl

########################################
## INITIALIZE CGI ######################
########################################

use CGI;
use CGI::Carp qw(fatalsToBrowser);
$cgi = new CGI;

########################################
## MAIN ################################
########################################

$DATAFILE = "hitcount.dta";
$val = 0;

if (open(RECORD, "$DATAFILE"))
{
	$val = <RECORD> || 0;
	close(RECORD);
}

$val++;

if (open(RECORD, ">$DATAFILE"))
{
	flock(RECORD, 2);
	print(RECORD $val);
	close(RECORD);
}

# output to webpage
print $cgi->header();
print $val;

########################################
## EXIT ################################
########################################

exit;

########################################
## END OF FILE #########################
########################################

... and it was called with this...

Code:
<html> 
<body>

<!--#include virtual="/cgi-bin/hitcount/hitcount.pl"-->

</body> </html>

... in a file with .shtml as the extention.

Many thanks,
Keith
 
I also had to upload in ASCII format ('Premature end of script headers' error in log)

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top