CGI/PERL DOESN'T WORK

Status
Not open for further replies.

Mindverse

Smack-Fu Master, in training
93
Okay, I am learning perl/cgi and I wanted to write a simple script that checked if you typed in HELLO or not. If you did it would print HELLO on the screen and if you didn't it should print WRONG.

Here is the code for my form on the page:

<form action="cgi-bin\Equality.cgi" method="POST" name="GOOF">

<input type="text" length="20" name="title">


<input type="submit" value="Hmmm?">

</form>

And here is the code for my .cgi file:

#!/usr/local/bin/perl
my $Inner;
$Inner=<STDIN>;
$Inner =~ s/%(..)/pack("c",hex($1))/ge;

if($Inner=="HELLO"){

print"HELLO";

}else{
print"WRONG";
}


BTW, this is on my computer that is why the slashes are \ instead of /. Anyway when I submit my information it asks me if I want to download the file and wont run the script.

PLZ, WHAT IS WRONG!?!?!?!?! :confused: :eek:
 
BTW, this is on my computer that is why the slashes are \ instead of /. Anyway when I submit my information it asks me if I want to download the file and wont run the script.

Are you running a webserver on your PC or are you simply pointing your browser at the html form and expecting it to run? If you are doing the latter you need to install and configure a webserver on your machine; cgi scripts won't work without one.

If you are already running a server, your server is misconfigured and isn't aware that the file you are running is a server-side executable.

In order to properly diagnose things we need to know your software environment.

PS: putting code in code tags works wonders for preformatted text :)
 

Mindverse

Smack-Fu Master, in training
93
For some reason none of my stuff works on tripod and it is supposed to, but it keeps saying script error after I took your suggestions.

First of all if I had a form that consisted of a submit button and a text box and I wanted to see if what they typed in the box was the password.

Could I write the following to take in the text in the text box:

$foo=<STDIN>;

Now I have a feeling there are more characters in there then what the user typed.

More Questions:

1.) So, how would I go about checking what's in the text box and then printing out HTML that says YOU GOT IT WRITE if it is right?

2.) When I start HTML code in the .cgi file do I start it like this:

Content-Type: text/html\n\n
<> ... HTML STUFF ...<>
END

...More Perl Stuff...
if not then can you show me how to insert something that writes html to a page using perl/cgi?

thx
 

petrov

Ars Tribunus Militum
2,714
There are so many things wrong with the code that you posted, but first off, you should make sure that your perl script is executable, and in a directory that's configured to execute CGI scripts. You do the first by executing something like 'chmod +x Equality.cgi' from the command line. You do the second by putting a .htaccess file in your cgi-bin with the contents of 'Options +ExecCGI' or modifying your httpd.conf file, and changing the Options line for the cgi-bin directory. See http://httpd.apache.org/ for more information.

Originally posted by Mindverse:
<form action="cgi-bin\Equality.cgi" method="POST" name="GOOF">
this will only work if your webserver is running on windows. You should probably make this:
<form action="cgi-bin/Equality.cgi" method="POST" name="GOOF">
and it should work on both.

<input type="text" length="20" name="title">

<input type="submit" value="Hmmm?">
</form>

And here is the code for my .cgi file:
#!/usr/local/bin/perl
my $Inner;
there is no need for the my() unless you are running under "use strict" which would probably be a good idea.
$Inner=<STDIN>;

you should put this into a while loop. Something like "while ($line = <STDIN> ) { $Inner .= $line } Some browsers submit POST's that span across multiple lines, which is perfectly valid HTTP.

$Inner =~ s/%(..)/pack("c",hex($1))/ge;

this is an odd statement. I'm not sure that you need it for this simple example. I'm assuming that you are actually trying to do something like:
$Inner =~ s/\%([0-9A-F]{2})/chr(hex($1))/ge;
which is a bit more robust, but still unnecessary for this simple example. You only need this if you are getting non-alphabetic data in your input.

if($Inner=="HELLO"){
The correct syntax for string comparisons uses "eq" and not "==" and looks something like:
if ($Inner eq 'HELLO') {
which is going to fail anyways, as you will actually get something like "title=HELLO" from the browser as your <input> is named as "title", and if you have other fields in your <form>, it'll be joined together with ampersands. Rather than straight up equality, you are better using a regular expression, something like:
if ($Inner =~ /title=HELLO/) {

print"HELLO";

}else{
print"WRONG";
}
you are going to need to print out a "Content-type: text/html\n\n" before you can generate any other output or you'll get a 500 level error message from the web server.

BTW, this is on my computer that is why the slashes are \ instead of /. Anyway when I submit my information it asks me if I want to download the file and wont run the script.

are you not using a web server? You need to have something like apache, or IIS, or PWS installed on your computer to run CGI. Even if this is running on http://localhost/, you should still be able to use forward slashes, even on a windows web server.

--sam
 
Status
Not open for further replies.