A Simple Script

They have: 850 posts

Joined: Jul 1999

To use this script, you need to fill in the dirrerent html's in the array named @html.
Than to use it you would link to
yoursite.com/cgi-bin/scriptname.cgi?html=NAMEHERE . I tested it , and it worked for me.

Code Sample:

#!/usr/local/bin/perl
#

##########
#making array with all the .html's
@html=("yahoo.html","other.html","somthing.html");
$errorhtml="error.html";
$printed=0;


##########
#getting form input
&GetFormInput;

sub GetFormInput {

(*fval) = @_ if @_ ;

local ($buf);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
}
else {
$buf=$ENV{'QUERY_STRING'};
}
if ($buf eq "") {
return 0 ;
}
else {
@fval=split(/&/,$buf);
foreach $i (0 .. $#fval){
($name,$val)=split (/=/,$fval[$i],2);
$val=~tr/+/ /;
$val=~ s/%(..)/pack("c",hex($1))/ge;
$name=~tr/+/ /;
$name=~ s/%(..)/pack("c",hex($1))/ge;

if (!defined($field{$name})) {
$field{$name}=$val;
}
else {
$field{$name} .= ",$val";


}


   }
}
return 1;
}

$input=$field{'html'};
$input="$input.html";

##########
#Checking to see if any of the html's in the array equal the inputed html
foreach $page(@html)
{
if($input eq $page)
{
print "Location: $page";
$printed=1;
}
} #end of foreach $page(@html)

##########
#printing the error html if nothing was found

if($printed==0)
{
print "Location: $errorhtml";
}

------------------
A 'jiffy' is an actual unit of time for 1/100th of a second.

[This message has been edited by robp (edited 15 December 1999).]

They have: 161 posts

Joined: Dec 1999

Adhering more closely to the specs given, I offer this potential solution:

Code Sample:

#!/usr/bin/perl -Tw

use strict;
my $file = $ARGV[0];  # yahoo, for instance

# this regex "detaints" the $file variable,
# since we don't want to let it be anything
# dangerous
($file) = $file =~ /^(\w+(?:\.\w+)?)$/;

# $file will be defined if it's ok
print("Location: error.html\n\n"), exit
  unless defined $file;

# add the default .html extension
$file .= ".html" if $file !~ /\.\w+$/;

# this assumes, like you said, that the HTML
# files are in the same directory as the CGI
# program -- be sure your server allows this
print("Location: $file\n\n"), exit
  if -e $file;

print "Location: error.html\n\n";

__END__

------------------
--
MIDN 4/C PINYAN, NROTCURPI, USNR

[This message has been edited by japhy (edited 15 December 1999).]

They have: 472 posts

Joined: Oct 1999

Can someone here write a simple Perl script for me?

Let's say someone enter
"awards.cgi?yahoo", then the script will look for a file called "yahoo.html" in the same directory.

The query is the filename, the default extension will be html.

And if someone types "awards.cgi?yahoo", but the script can't find yahoo.html, then I would like the script to load an error.html.

All .html will be in the same directory as the CGI.

Thanks.

------------------
Goodbookmarks.com - Daily updates of good and honest website, plus a daily joke!
Submit Your Site Now!

They have: 1,587 posts

Joined: Mar 1999

i could write something like that and i'm brain dead when it comes to cgi compared to some of these guys.

------------------
Get paid to surf, email, and everything else online! 75 hrs a month at 55+ cents an hour! Beats AllAdvantage Easily. CLICK 4 DETAILS

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

They have: 1,587 posts

Joined: Mar 1999

there's 2 possible solutions, don't if they've beent tested though.

------------------
Get paid to surf, email, and everything else online! 75 hrs a month at 55+ cents an hour! Beats AllAdvantage Easily. CLICK 4 DETAILS

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

They have: 472 posts

Joined: Oct 1999

I'm sorry robp, but your code didn't work.

For your information, I'm entering the URL directly into the browser.

------------------
Goodbookmarks.com - Daily updates of good and honest website, plus a daily joke!
Submit Your Site Now!

They have: 103 posts

Joined: Apr 1999

Here's a small script, half the size of the others, that actually works simply.

Code Sample:

#!/usr/bin/perl
if(-e "$ENV{QUERY_STRING}.html")
{
  print "Location: $ENV{QUERY_STRING}.html";
}
else
{
  print "Location: error.html";
}
exit;

------------------
Gil Hildebrand, Jr.
The Web Development Genius of the 21st Century
Boardzilla Programmer

Gil Hildebrand, Jr.
Internet Consultant
New Orleans, LA

They have: 850 posts

Joined: Jul 1999

Not sure why mine didn't work, I just tried it again using
server.com/Cgi-Bin/test.cgi?html=yahoo

and it seemed to work fine. Might as well use Gil's though, nice and short.

------------------
A 'jiffy' is an actual unit of time for 1/100th of a second.

[This message has been edited by robp (edited 15 December 1999).]

They have: 161 posts

Joined: Dec 1999

Gil's is fine as long as you're only looking for HTML files.

In retrospect, the program doesn't need taint checking, since you're not using the filename in a system command.

Code Sample:

#!/usr/bin/perl -w

use strict;
my $file = $ARGV[0];

$file .= ".html" if $file !~ /\.\w+$/;
print "Location: ", (
  -e $file ? $file : "error.html"
), "\n\n";

------------------
--
MIDN 4/C PINYAN, NROTCURPI, USNR

They have: 103 posts

Joined: Apr 1999

I was just trying to keep it simple for him japhy..that's the way I would've done it though if I needed it for myself..

------------------
Gil Hildebrand, Jr.
The Web Development Genius of the 21st Century
Boardzilla Programmer

Gil Hildebrand, Jr.
Internet Consultant
New Orleans, LA

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.