PDA

View Full Version : CGI script for list. Have questions on improvement.


dvdch
03-24-2006, 03:25 AM
Hey. I have a CGI script that lets a user choose a website from a list and then opens that website in a new browser window.
The script is located at http://www.stormrazor.com/select2.html

My questions are:

1) Is there a more current or efficient script out there that will do the same thing?

2) Does anyone know much cgi server resources this script would use if thousands of users used it per day?
My hosting service mentioned that they would limit cgi access to websites that used too much of their server's cgi resources.

3) Is there a php script that would do the same thing? My hosting service didn't mention any limitations on php scripts.

The cgi script is listed below. I got it from someone named Ian Rochat


==============================================


if ($ENV{'REQUEST_METHOD'} eq "POST") {
# Content Length should never be very big, quit if it's more than 8192 bytes
if ($ENV{'CONTENT_LENGTH'} > 8192) { exit(2) }
read(STDIN,$form_data,$ENV{'CONTENT_LENGTH'});
}
else {
$form_data = $ENV{'QUERY_STRING'};
}

%VARS = split(/[=&]/,$form_data);
$VARS{'URL'} =~ s/%(..)/pack("c",hex($1))/ge;

print "Location: $VARS{'URL'}\n\n";

return(0);

==============================================
The HTML code for the webpage is below. I used the target="_blank" tag to open the links in a new browser window.

<HTML>
<HEAD>
<TITLE>Select</TITLE>
</HEAD>

<BODY>

<center>
<p>&nbsp;

<FORM method="GET" action="/cgi-bin/selector.cgi" target="_blank">

<SELECT name="URL">
<OPTION SELECTED value="http://www.google.com/">Google</OPTION>
<OPTION value="http://www.yahoo.com">Yahoo</OPTION>
<OPTION value="http://www.altavista.com">AltaVista</OPTION>
<OPTION value="http://www.dogpile.com">Dogpile</OPTION>
<OPTION value="http://www.lycos.com">Lycos</OPTION>
</SELECT><INPUT name="Submit" type="submit" value="Go">
</FORM>

</center>
</BODY>
</HTML>


==============================================

Any help would be appreciated.

Thanks,
David


.

mlseim
03-24-2006, 03:55 AM
Maybe something like this?

http://lab.artlung.com/dropdown/

You can put Javascripting in your dynamically created page.

FishMonger
03-24-2006, 04:18 AM
For what you're needing, I'd agree with mlseim, use javascript.

As far as your perl script, I'd recommend replacing the method you're using to read/parse the form with this:

use CGI ':cgi-lib';

%VARS = Vars;
print "Location: $VARS{'URL'}\n\n";

dvdch
03-24-2006, 09:29 AM
I'm trying to avoid using javascript since a lot a people are now turning it off by default.

Anyone knows if this script uses a lot of resources?


........

mlseim
03-24-2006, 02:29 PM
Here's the PHP locate to a URL:

<?php
/* Redirect to a different URL */
$url = $_POST["URL"];
header("Location: $url");
exit;
?>

Where you bring in the variable from your form.
In your case, it's URL (capitalized).

FishMonger
03-24-2006, 06:48 PM
So far, we only answered part of your question. Here's the rest.

In this case, there is no real difference in the amount of resources used by either Perl or PHP, which is almost nil. Also, both Perl and PHP can be installed/compiled into apache, or not. So, assuming the scripts were written properly, each would use roughly the same amount of "cgi resources" and your provider would have the same level of restrictions.

It boils down to personal choice...do you perfer Perl or PHP. I prefer Perl.

dvdch
03-24-2006, 10:50 PM
Thanks for answering the cgi resources part. I'm going to fool around with the php suggestion this weekend.

I might just stick with the cgi script since it seems to be running just fine. I'll try modifying it with FishMonger's suggestion.

KevinADC
03-26-2006, 08:01 AM
nobody here can tell you how much resources that script would use if thousands of users used it per day. All servers can be setup different and the amount of resources the same script uses on server A vs. server B is something only the administrators of the server will know. My guess though, is that script will use next to no resources at all, but it is poorly written and could potentially be used to consume all the server resources by any knucklehead that has basic cgi knowledge. I would also not use fishmongers suggestion as the imported function (cgi-lib) is essentially for compatibility with old (very old now) perl 4 scripts. I would use CGI and import the :cgi functions and set the POST_MAX and DISABLE_UPLOADS variables for extra security.

use CGI qw/:cgi/;
$CGI::POST_MAX= 8192;
$CGI::DISABLE_UPLOADS = 1; # no uploads
my $url = param('URL');
#I would run some validation on $url here
print "Location: $url\n\n";
return(0);

not sure why the return(0) is on the end of the script if that is the entire script. Seems like it need not be there. The above will use a bit more resources than the stock script but the added security features of CGI.pm should be taken into consideration.

FishMonger
03-26-2006, 11:32 AM
the imported function (cgi-lib) is essentially for compatibility with old (very old now) perl 4 scriptsThat is not correct. It does bare the same name as the old cgi-lib.pl, but the function that is used to provide backwards compatibility is ReadParse() and is imported like this:

CGI::ReadParse();

http://search.cpan.org/~lds/CGI.pm-3.17/CGI.pm#COMPATIBILITY_WITH_CGI-LIB.PL

I gave that example for 2 reasons. 1) David's code was putting the from field(s) into a hash and I wanted to do the same. 2) It's the quickest method to import the info into the hash. I probably should have included the OO method, since that's what I use in my scripts and is only 1 additional line.

use CGI;

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

KevinADC
03-26-2006, 09:09 PM
That is not correct. It does bare the same name as the old cgi-lib.pl, but the function that is used to provide backwards compatibility is ReadParse() and is imported like this:

CGI::ReadParse();

http://search.cpan.org/~lds/CGI.pm-3.17/CGI.pm#COMPATIBILITY_WITH_CGI-LIB.PL

I gave that example for 2 reasons. 1) David's code was putting the from field(s) into a hash and I wanted to do the same. 2) It's the quickest method to import the info into the hash. I probably should have included the OO method, since that's what I use in my scripts and is only 1 additional line.

use CGI;

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


I could be wrong, but I am under the impression that :cgi-lib is used to import the functionality of the older cgi-lib module. You can see in the CGI module:

':cgi-lib' => [qw/ReadParse PrintHeader HtmlTop HtmlBot SplitParam Vars/],

these are some of the old cgi-lib functions (I guess the ones Linclon thought needed inclusion). Using :cgi-lib imports the old ReadParse function as well as some others, Vars included.

I know the CGI.pm documentation leaves some to be desired (it is a big module after all doing lots of stuff) and the :cgi-lib options are not well documented I guess because they are old, but if you look into the CGI.pm code itself there are couple of nuggets of information not covered in the docs. :)

dvdch
03-30-2006, 03:03 AM
Hey, would it help if I found out exactly what version of perl my hosting service is using?
Seems to me that you guys could modify/optimize the script for that specific version.

Let me know,

David

KevinADC
03-30-2006, 03:39 AM
$] is the perl version variable:

print "Perl version: $]";

but the script you posted is so small there is nothing to optimize.