CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
My script sometime It works - sometime I got 500 ERROR - Please tell my Why and ....

 



kiho
Deleted

Jul 2, 2000, 9:41 AM

Post #1 of 4 (303 views)
My script sometime It works - sometime I got 500 ERROR - Please tell my Why and .... Can't Post

Hello
I have a simple script - It works BUT SOMETIME I got ERROR 500 with it. I dont know how to fix. Please review it and show me what should I do now. Thanks my friend

This is a Perl code : Click here if you want to visit my site : http://www.vnsky.net - I am using HOST Hypermart.net

*********************************************
#!/usr/local/bin/perl

$ss = "http://www.vnsky.net/";
$s_web = "http://www.vnsky.net/search.cgi\?q=";
$s_viet = "http://www.vnsky.net/links/cgi-bin/search.cgi";
$s_download = "http://vnsky1.hypermart.net/download/cgi-bin/search.cgi";
$s_perl = "http://www.vnsky.net/freeperl/cgi-bin/search.cgi";

# Get the form variables

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Break em up into a format the script can read

@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;
$FORM{$name} = $value;
}


if ($FORM{action} eq 'web') {
&web;
exit;
}

if ($FORM{action} eq 'download') {
&download;
exit;
}

if ($FORM{action} eq 'vietnam') {
&vietnam;
exit;
}

if ($FORM{action} eq 'Perl') {
&perl;
exit;
}

print "Content-type: text/html\n";
print "Location: $ss\n\n";


sub web {
print "Content-type: text/html\n";
print "Location: $s_web$FORM{key}\n\n";
}

sub vietnam {
print "Content-type: text/html\n";
print "Location: $s_viet\?query=$FORM{key}\n\n";
}

sub download {
print "Content-type: text/html\n";
print "Location: $s_download\?query=$FORM{key}\n\n";
}

sub perl {
print "Content-type: text/html\n";
print "Location: $s_perl\?query=$FORM{key}\n\n";
}

***********************************************


Cretep
User

Jul 2, 2000, 9:23 PM

Post #2 of 4 (303 views)
Re: My script sometime It works - sometime I got 500 ERROR - Please tell my Why and .... [In reply to] Can't Post

Give this a try. I removed the mime's as you dont want to print text/html you want to redirect the browser. I also made all the if statments into one single block.
---------------------
#!/usr/local/bin/perl

$ss = "http://www.vnsky.net/";
$s_web = "http://www.vnsky.net/search.cgi\?q=";
$s_viet = "http://www.vnsky.net/links/cgi-bin/search.cgi";
$s_download = "http://vnsky1.hypermart.net/download/cgi-bin/search.cgi";
$s_perl = "http://www.vnsky.net/freeperl/cgi-bin/search.cgi";

# Get the form variables

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Break em up into a format the script can read

@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;
$FORM{$name} = $value;
}


if ($FORM{'action'} eq 'web') {
&web;
exit;
}elsif ($FORM{'action'} eq 'download') {
&download;
exit;
}elsif ($FORM{'action'} eq 'vietnam') {
&vietnam;
exit;
}elsif ($FORM{'action'} eq 'Perl') {
&perl;
exit;
}else {
print "Location: $ss\n\n";
}

sub web {
print "Location: $s_web$FORM{key}\n\n";
}

sub vietnam {
print "Location: $s_viet\?query=$FORM{key}\n\n";
}

sub download {
print "Location: $s_download\?query=$FORM{key}\n\n";
}

sub perl {
print "Location: $s_perl\?query=$FORM{key}\n\n";
}
---------------------
Good luck

Peter


rGeoffrey
User / Moderator

Jul 2, 2000, 11:07 PM

Post #3 of 4 (303 views)
Re: My script sometime It works - sometime I got 500 ERROR - Please tell my Why and .... [In reply to] Can't Post

Here is one guess on what might be causing the Internal Server Error (500).

When it dies, is it by any chance usually on the 'web' version. Looking at these three addresses, the other two are down the 'cgi-bin' tree while the 'web' version is not. On many systems scripts will only work in the 'cgi-bin' so the 'web' version should die.

$s_web = "http://www.vnsky.net/search.cgi\?q=";
$s_viet = "http://www.vnsky.net/links/cgi-bin/search.cgi";
$s_perl = "http://www.vnsky.net/freeperl/cgi-bin/search.cgi";

But on another topic, here is a shorter version of your code...
--------cut here------------------
#!/usr/local/bin/perl

my $ss = "http://www.vnsky.net/";

my %FORM = &Read_Form ();

my %where = (
web => "http://www.vnsky.net/search.cgi\?q=",
vietnam => "http://www.vnsky.net/links/cgi-bin/search.cgi?query=",
download => "http://vnsky1.hypermart.net/download/cgi-bin/search.cgi?query=",
'Perl' => "http://www.vnsky.net/freeperl/cgi-bin/search.cgi?query=",
);

if (exists $where{$FORM{action}}) {
print "Location: " . $where{$FORM{action}} . $FORM{key} . "\n\n";
} else {
print "Location: $ss\n\n";
}

sub Read_Form
{
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Break em up into a format the script can read

@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;
$FORM{$name} = $value;
}
}
--------cut here------------------
Some notable changes include:

Getting the values for %FORM has been moved into a subroutine. This is a very common activity and really should be in a subroutine. One good choice is to use CGI.pm and use that version. I personally avoid CGI.pm like the plague because this function is the only feature I need and the module has too much overhead. If you also you want to use your own, you should make a personal CGI_Mine.pm with this function and use it from all your cgi perl scripts. There are a great many alternate CGI modules on CPAN for you to choose from.

The other big change is that I added a %where hash that has the URLs for the scripts you will be forwarding to. The 'web' version was the only one with the part between the '?' and the '='. The 'query=' has been added to the other 3 to make life consistant.

Using this hash, we can collapse 4 if statements and 4 subroutines from 32 lines to 9. And a single else case handles everything else which is sent to $ss, and we don't need any exit statements in the middle of the code.


kiho
Deleted

Jul 3, 2000, 8:14 PM

Post #4 of 4 (303 views)
Re: My script sometime It works - sometime I got 500 ERROR - Please tell my Why and .... [In reply to] Can't Post

Thank you so much ! It looks better than before

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives