PDA

View Full Version : Help My Cgi Script And Tripod


disto
09-20-2005, 08:20 PM
ok the problem im using TRIPOD!!!

yes i know its most definatly not ideal and scripting is made very difficult...

i coded a counter script the way it worked was it used variables in the url to set a counter id then just opened/created a txt file called the 'id variable'...

i tried and tested my script on xitami and it worked great however tripod doesnt like it!!

can anyone help?


for a counter on an index page (the id being called 'index') the url was :
http://distouk.tripod.com/cgi-bin/count/count.pl?id=index

the count.pl file containts


---------------------------------

#!/usr/local/bin/perl

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$input{$name} = $value;
}

print "Content-type: text/html\n\n<html>";

$id = $input{'id'};
$txt = ".txt";


open(O1, "<count/opn1.txt");
$o1 = <O1>;
close(O1);

open(O2, "<count/opn2.txt");
$o2 = <O2>;
close(O2);

open(O3, "<count/opn3.txt");
$o3 = <O3>;
close(O3);

open(O4, "<count/opn4.txt");
$o4 = <O4>;
close(O4);

open(O5, "<count/opn5.txt");
$o5 = <O5>;
close(O5);

open(O6, "<count/beg.txt");
$beg = <O6>;
close(O6);

open(O7, "<count/end.txt");
$end = <O7>;
close(O7);

open(C2, "<count/$id$txt");
$lim = <C2>;
close(C2);


open(C1, "<count/$id$txt");
$num = <C1>;
close(C1);

$num++;

open(C1, ">count/$id$txt");
print C1 "$num";
close(C1);


if ($lim eq "") {
&lock;
}
&run;

sub run {
print "<link rel='stylesheet' href='http://distouk.tripod.com/stylesheet1.css' type='text/css'><span id='count'>you are visitor: $num</span>";
exit;
}


sub lock {
open(C3, ">>count/log.pl");
print C3 "$beg<tr><td>$id</td><td>$end\nopen($id$o1$id$o2$id";
print C3 " = <$id$o3$id$o4\n$o5$id$end";
print C3 "\n$beg</td></tr>$end\n\n";
close(C3);
print "<link rel='stylesheet' href='http://distouk.tripod.com/stylesheet1.css' type='text/css'><span id='count'>*you are visitor: $num</span>";
exit;
}
---------------------------------------------

can anyone help?

disto
09-20-2005, 08:24 PM
print "Content-type: text/html\n\n<html>";

this line never appeared on my xitami script i entered it because previous counters i had built worked when this line was entered before the display of a counter


i get an error message this can be viewed here (http://distouk.tripod.com/cgi-bin/count/count.pl?id=index)

FishMonger
09-21-2005, 04:55 PM
I don't know anything about xitami or tripod but lets take a look at your script.

First, the error message you're receiving appears to be print statement from within your script but you haven't shown us that portion of the script, so I can't say exactly what needs to be done to fix that part.

You should be using the CGI module for retrieving/reading the QUERY_STRING. The method you're using, at least within the Perl world, has been depreciated for about 10 years.

You need to add error handling on the opening of the filehandles. You haven't shown what you're reading-in from those 8 files or how they got written to, but I'm willing to bet that you could combine that info into a single CSV file.
#!/usr/local/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;
my %input = $q->Vars;

my $id = $input{'id'};
my $txt = ".txt";

print $q->header;

open(O1, "<count/opn1.txt") or die "can't open count/opn1.txt $!";
my $o1 = <O1>;
close(O1);

open(O2, "<count/opn2.txt") or die "can't open count/opn2.txt $!";
my $o2 = <O2>;
close(O2);

etc
etcUsing a single CSV file instead of the multiple files.
open(CSV, "<count/csv.txt") or die "cant open CSV file $!";
my ($o1, o2, $o3, $o4, $o5, $beg, $end, $lin, $num) = split(/,/, <CSV>);
close CSV;
Finally, this could be greatly simplified by using the File::CounterFile module.
http://search.cpan.org/~gaas/File-CounterFile-1.04/CounterFile.pm

disto
09-21-2005, 08:38 PM
ah right ok!!!

well i played around with the script and concluded it had something to do with the sub and the & part of the script...
i have it sorted now heres the script if your interested!!!!



---------------------------------
#!/usr/local/bin/perl

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$input{$name} = $value;
}

$id = $input{'id'};


print "Content-type: text/html\n\n<html>";
print "<link rel='stylesheet' href='http://distouk.tripod.com/stylesheet1.css' type='text/css'><span id='count'>";


if ($id eq "0") {open(C0, "<c0.txt");$num = <C0>;close(C1);$num++;open(C0, ">c0.txt");print C0 "$num";close(C0);print "you are visitor: $num</span>";exit;}
if ($id eq "img") {open(C1, "<c1.txt");$num = <C1>;close(C1);$num++;open(C1, ">c1.txt");print C1 "$num";close(C1);print "you are visitor: $num</span>";exit;}
print "<b>Counter Error!</b></span>";
------------------------------------------------

FishMonger
09-21-2005, 10:35 PM
This can still use additional improvements, but here's an better version of your script.
#!/usr/local/bin/perl

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

my $id = param('id');

my %input = (
'0' => 'c0.txt',
'img' => 'c1.txt',
);

print "Content-type: text/html\n\n<html>";
print "<link rel='stylesheet' href='http://distouk.tripod.com/stylesheet1.css' type='text/css'><span id='count'>";


if (defined $input{$id}) {
open(C0, "$input{$id}") or die "can't read from $input{$id} $!";
my $num = <C0>;
close(C0);
$num++;
open(C0, ">$input{$id}") or die "can't write to $input{$id} $!";
print C0 $num;
close(C0);
print "you are visitor: $num</span>";
exit;
}
else {
print "<b>Counter Error!</b></span>";
}