homepage Welcome to WebmasterWorld Guest from 54.161.246.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

This 44 message thread spans 2 pages: 44 ( [1] 2 > >     
Newbies need help with Perl script please
daro2008

5+ Year Member



 
Msg#: 3698091 posted 4:35 pm on Jul 14, 2008 (gmt 0)

Hi all,

I am very new here today. I have a problem with running a perl script which results in displaying the following message:

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request

The Perl Script for my small Guest book is:

#!/usr/bin/perl

# That is the path to PERL just above It MUST be first in the script

# The following accepts the data from the form

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@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;
}

# The following sends the email

open (MESSAGE,"¦ /usr/sbin/sendmail -t");

print MESSAGE "To: $FORM{submitaddress}\n";
print MESSAGE "From: $FORM{surName}\n";
print MESSAGE "Reply-To: $FORM{email}\n";

print MESSAGE "Subject: Feedback from $FORM{lastName} at Itwebxpert\n\n";
print MESSAGE "Surname: $FORM{lastName}\n";
print MESSAGE "First name: $FORM{firstName}\n";
print MESSAGE "email: $FORM{email}\n";
print MESSAGE "Sex: $FORM{sex}\n";
print MESSAGE "The user wrote:\n\n";
print MESSAGE "$FORM{message}\n";
close (MESSAGE);

&thank_you;
}

#The following creates the Thank You page display

sub thank_you {

print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Thank You Page</title>\n";

#link to external style sheet file: style1.css
print "<link rel=\"StyleSheet\" name=\"style\" href=\"../styles/style2.css\" type=\"text/css\">";

print "</head>\n";
print "<body>\n";
print "<h1><center>Your message has been sent</center></h1><hr>";
print "<h2 align=\"center\">Thank You!</h2>\n";
print "\n";
print "<h3 align=center>Your feedback is greatly appreciated?</h3><hr><br>";
print "<center><a class=\"declink\" href=\"../personal/personal.html\">Back to home page</a></center>\n";
print "</body>\n";
print "</html>\n";
exit(0);
}

I check the path to the Perl server is correct, and the path from my HTML guest book form to this script file is also correct. The Script work previously in my previous webhosting server, but it does not work at the current webhosting server. The technician of the current webhosting said there is something wrong with the script, but I denied as it worked before. Just to make sure the script is correct, could anyone here check if there is any thing missing from the script above.

Your kind help is very appreciated.

Regards

Daro2008

 

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 8:18 pm on Jul 14, 2008 (gmt 0)

welcome to WebmasterWorld [webmasterworld.com], Daro2008 !
you should start with your server error log and look for clues there.
there is something that is causing an error and it will be recorded in the log.
there is also a server directive, most likely in the server config file or in the .htaccess file in the directory of your script, that specifies a page to display whenever an error occurs.
the ErrorDocument does not exist where it was specified, so it causes the second error, a 404 Not Found error.
you should also see this error logged in your server error log and it should even show the url of the requested error document.

this means you need to fix either the config/.htaccess or the location of the error document.
then you will be able to see and fix the error in the script or environment that threw the initial error...

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3698091 posted 8:43 pm on Jul 14, 2008 (gmt 0)

I'd suggest you replace
#!/usr/bin/perl
with
#!/usr/bin/perl -w

just to rule out unix/windows-linebreaks-trouble.
people often can't access errorlogs on shared hosting environments. you might also want to try putting
use CGI::Carp qw(fatalsToBrowser);
right after #!/usr/bin/perl -w.

I don't think it should cause a problem (but silently not send email) -- but, did you check if the path to sendmail? you have "/usr/sbin/sendmail" set up right now ...

daro2008

5+ Year Member



 
Msg#: 3698091 posted 9:52 pm on Jul 14, 2008 (gmt 0)

Thank Janhar,

Yes, the path to the email server is correct, and the path to sendmail is also correct. I still think that the problem is with the Perl server. It is strange to me!

I will try #!/usr/bin/perl -w to see if it is resolved?

Many thanks for your help.

daro2008

5+ Year Member



 
Msg#: 3698091 posted 9:59 pm on Jul 14, 2008 (gmt 0)

Hi Pra,

I am not the owner of the server, so i cannot have access to log file: config/.htaccess.

the technician of this server claim that his Perl server run perfectly see link below:

[edited]

Could you please advise if any thing is wrong with the above?

[edited by: phranque at 11:04 pm (utc) on July 14, 2008]
[edit reason] No urls, please. See TOS [webmasterworld.com] [/edit]

spander

5+ Year Member



 
Msg#: 3698091 posted 10:20 pm on Jul 14, 2008 (gmt 0)

Make sure the file permissions for your cgi script are set to 755

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 11:08 pm on Jul 14, 2008 (gmt 0)

the .htaccess file is in you root directory.
you should be able to access this one if they allow server config overrides.
if you can't get access to your error log, you should look for a new host.
maybe your server technician will inform you of the configured location for ErrorDocument.

perl_diver

5+ Year Member



 
Msg#: 3698091 posted 11:27 pm on Jul 14, 2008 (gmt 0)

There are no syntax errors in the perl code you posted. But, word of advise, do not use the guestbook script. It will be abused so fast you will regret the day you put it on your server. No security what so ever and bots will start spamming your server. Its a disaster waiting to happen.

daro2008

5+ Year Member



 
Msg#: 3698091 posted 11:59 pm on Jul 14, 2008 (gmt 0)

Thank you for all!

Phranque, there is on .htaccess in my root directory, but there is no logs inside when I view/edit it. Below is the result of the Perl server test given by the technician:

Server Settings
Date/Time: Monday 14 July 2008 : 19 h 58 mn 03 s

Operating System: linux
Operating System Version: 2.6.21.5-hostnoc-3.1.7-libata-grsec-32
Hardware: i686 (PC)
Host Name:
Last time rebooted: 84 days, 4 hour(s) 56 minute(s)
Average Usage: The Last minute: 0.04
The last 5 minutes: 0.11
The last 15 minutes: 0.16
Connected Users: 0
Software Paths :
sendmail : /usr/lib/sendmail
/usr/sbin/sendmail
whois : /usr/bin/whois

gpg : /usr/bin/gpg

tar : /bin/tar

gunzip : /usr/bin/gunzip
/bin/gunzip
gzip : /usr/bin/gzip
/bin/gzip
sh : /bin/sh

Perl Info:
Installed Perl Version: 5.008008
/usr/local/bin/perl installed?: YES, version :
This is perl, v5.8.8 built for i386-linux-thread-multi

/usr/bin/perl Installed?: YES, version :
This is perl, v5.8.8 built for i386-linux-thread-multi

Installed Perl Modules :
List of all installed Perl modules on this server: CLICK HERE
(This may be long....,Please wait)
Check if a module is installed AND is running: Module's exact Name (without ".pm") :

Examples: CGI or LWP::Simple or DBD::mysql
Variables:
DOCUMENT_ROOT:
Website Root /home/dollarho/public_html/admin
GATEWAY_INTERFACE:
CGI info CGI/1.1
HTTP_ACCEPT:
Types de documents acceptés par le navigateur-visiteur */*
HTTP_ACCEPT_ENCODING: gzip, deflate
HTTP_ACCEPT_LANGUAGE:
Language Used en-gb
HTTP_CONNECTION:
HTTP Connection Keep-Alive
HTTP_HOST:
HTTP of the requesting host
HTTP_REFERER:
Referring URL
HTTP_UA_CPU: x86
HTTP_USER_AGENT:
Your Browser Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
PATH:
Répertoires de base, de recherche de prg /usr/local/bin: /usr/bin: /bin
QUERY_STRING:
Things that follow the ? in a URL
REMOTE_ADDR:
Your IP Address
REMOTE_PORT:
Port used for this request 3917
REQUEST_METHOD:
Type of the request (GET/POST/HEAD) GET
REQUEST_URI:
URI of the request /cgi-bin/servertester.pl
SCRIPT_FILENAME:
CGI path in your server /home/dollarho/public_html/admin/cgi-bin/servertester.pl
SCRIPT_NAME:
URI du CGI en cours d'éxécution (avec wrapper éventuel) /cgi-bin/servertester.pl
SERVER_ADDR:
SERVER_ADMIN:
Administrator's Email webmaster@
SERVER_NAME:
Site's Name
SERVER_PORT:
Server Port 80
SERVER_PROTOCOL:
Server Protocol HTTP/1.1
SERVER_SOFTWARE:
Nom et version du logiciel serveur web Apache/1.3.41 (Unix) mod_gzip/1.3.26.1a mod_log_bytes/1.2 mod_bwlimited/1.4 mod_auth_passthrough/1.8 FrontPage/5.0.2.2635 mod_ssl/2.8.31 OpenSSL/0.9.8b

Can you check if there is anything wrong in the above test?

Many thanks for your all inputs.

Cheers

[edited by: phranque at 1:12 am (utc) on July 15, 2008]
[edit reason] specifics [/edit]

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3698091 posted 1:56 am on Jul 15, 2008 (gmt 0)

Welcome aboard daro2008 . . . . a few things . . . .

1.
sendmail : /usr/lib/sendmail
/usr/sbin/sendmail

(Correct me if I'm wrong) this appears to be two locations to sendmail, or one is an alias, you can try changing it to the first one.

2. One common error on uploading scripts: if you are using a program like WS_FTP to upload this script, be sure it's in ASCII mode.

3. Another one I see frequently: PC end of line characters and Unix end of line characters are different. If the FTP program does not correctly convert the PC EOL's to Unix, this will cause an error. This is especially true if you use Notepad to edit your scripts. The best solution is to use a program like HomeSite to save your files in Unix format.

4. Check your script extension. Some servers are only configured to execute scripts with .cgi or .pl extensions, and often exclude one or the other.

5. I totally, completely agree w. perl_diver on this one, trust me, you don't need this kind of headache.

Let me give you one scenario: using this script, as posted, will allow a potential hacker to do something like this:

To: email1@example.com \n BCC: spam1@example.com,spam2@example.com,.....

Note the newline \n after the to address, followed by the BCC. In reality, they do something more complex to create the newline, but it's enough to exemplify. What I've done here, is created MY OWN BCC field using your headers.

I've only put two "spam" addresses. Imagine this X 1000. What happens?

BCC means "blind carbon copy." You get one email, and don't "see" any of the addresses in BCC. Because it's a BCC, you don't even know you have a problem, which is just what the spammers want.

1000 unsuspecting email users on example.com get an email. example.com says, "AH! Someone's spamming us from yoursite.com! Block all emails from yoursite.com!"

They then complain to your ISP that someone on their servers is spamming them. It's now the ISP's problem, and they will pull your plug in an instant. Your web site goes down until you find a new home for it.

This is only one of the MANY holes in this simple script. I learned of this early on in my career the hard way, and wished a million times over someone had bonked me up-side the head and told me what I was getting into.

We're telling you now. Find something else. Best of luck . . . .

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 3:13 am on Jul 15, 2008 (gmt 0)

you can also try putting your own ErrorDocument directive in the .htaccess file and then display the error that way:
[httpd.apache.org...]

perl_diver

5+ Year Member



 
Msg#: 3698091 posted 5:54 am on Jul 15, 2008 (gmt 0)

Not to mention the unfiltered message the user can send in the email to the recipient. Could be a URL/URI that links to a virus or other malicious code. Its amazing that this type of script is still floating around.

daro2008

5+ Year Member



 
Msg#: 3698091 posted 11:37 am on Jul 15, 2008 (gmt 0)

Thank you all.

perl_diver - There are no syntax errors in the perl code you posted. But, word of advise, do not use the guestbook script. It will be abused so fast you will regret the day you put it on your server. No security what so ever and bots will start spamming your server. Its a disaster waiting to happen.

What is an alternative option to the Guest Book you suggest would be a safe way to get a feedback from users/visitors?

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

rocknbil - I change the path to sendmail between two above, and use .pl or .cgi vice versa, there is no luck. What would be your best advice in the above senario. If there is nothing wrong with my perl codes (it did work in the previous hosting server), and technician claimed that his Perl server runs perfectly, what would your best advice?

Cheers

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 2:14 pm on Jul 15, 2008 (gmt 0)

figure out how to get the error displayed first.
then solve the error.
otherwise you are just guessing at the problem you are trying to solve.

daro2008

5+ Year Member



 
Msg#: 3698091 posted 3:08 pm on Jul 15, 2008 (gmt 0)

Hi phranque,

I clicked the above link you provided, but with my little knowledge of Perl I don't know how to create a directive to display event/error logs in .htaccess file. Can you give some guidance please.

thanks

perl_diver

5+ Year Member



 
Msg#: 3698091 posted 6:42 pm on Jul 15, 2008 (gmt 0)

check out the guestbook script from this site:

[nms-cgi.sourceforge.net...]

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 10:50 pm on Jul 15, 2008 (gmt 0)

try this.

create a file hello.cgi in your server root directory:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

# add any new code after this print statement
print "Content-type: text/html\n\n";

hello_html_doc;

sub hello_html_doc {
print "<html>\n";
print "<head>\n";
print "<title>Hello, World!</title>\n";
print "</head>\n";
print "<body>\n";
print "hello, world!\n";
print "</body>\n";
print "</html>\n";
}


check the file ownership and permissions against your other script.
typically it should be executable by the server process.
get http://example.com/hello.cgi

if that works, start adding your code until it breaks.

daro2008

5+ Year Member



 
Msg#: 3698091 posted 11:36 pm on Jul 15, 2008 (gmt 0)

Hi phranque,

It did not work. the error message is the same as before:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@example.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

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

I tried to place the file you suggested in both root directory or in cgi-bin directory. I think it is a problem with the server which is denied by the technician?

Any advice?

[edited by: phranque at 12:22 am (utc) on July 16, 2008]
[edit reason] examplified url [/edit]

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 12:21 am on Jul 16, 2008 (gmt 0)

do the location, permissions and ownership of the hello.cgi test script match those of your technician's test script?

perl_diver

5+ Year Member



 
Msg#: 3698091 posted 6:36 am on Jul 16, 2008 (gmt 0)

try:

#!/usr/local/bin/perl

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 7:40 am on Jul 16, 2008 (gmt 0)

does the shebang line in the hello.cgi test script match the one in your technician's test script?

daro2008

5+ Year Member



 
Msg#: 3698091 posted 10:35 am on Jul 16, 2008 (gmt 0)

phranque,

do the location, permissions and ownership of the hello.cgi test script match those of your technician's test script?

What you mean by that?

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

I set the file attributes of hello.cgi/hello.pl to 775; transferred it as ASCII using CuteFTP 8.0 professional; placed it in either root directory or cgi-bin folder, but there is no luck. The same error message is received as above.

Any advice?

daro2008

5+ Year Member



 
Msg#: 3698091 posted 10:49 am on Jul 16, 2008 (gmt 0)

perl_diver,

I also tried to use the Perl path as you suggested and as indicated in the Technician's test, but there is no luck. The same error message is received.

I am very blind!

Please advise what to do?

Cheers

daro2008

5+ Year Member



 
Msg#: 3698091 posted 10:53 am on Jul 16, 2008 (gmt 0)

Here is the test Perl file I tried:

#!/usr/local/bin/perl
use CGI::Carp qw(fatalsToBrowser);

# add any new code after this print statement
print "Content-type: text/html\n\n";

hello_html_doc;

sub hello_html_doc {
print "<html>\n";
print "<head>\n";
print "<title>Hello, World!</title>\n";
print "</head>\n";
print "<body>\n";
print "hello, world!\n";
print "</body>\n";
print "</html>\n";
}

Any advice?

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3698091 posted 11:28 am on Jul 16, 2008 (gmt 0)

have you tried the -w switch?
maybe you're saving the file in windows - the server runs on unix and the shebang (first line, stating which program should be used to interpret the file) is expected to end in \n, not \r\n as it would in windows. that way, it'd use "#!/usr/local/bin/perl\r" as the shebang, trying to execute "/usr/local/bin/perl\r" which will fail unless you have a very kind webhost who hacked it in.
if you add -w (or any other perlswitch for that matter), it's an argument for the interpreter and perl will silently ignore the possible \r, thus working even with windows-linebreaks.
don't rely on ASCII-upload. try it.
Is the cgi-bin a "real" cgi-bin or did you just create a folder and named it that?

daro2008

5+ Year Member



 
Msg#: 3698091 posted 12:04 pm on Jul 16, 2008 (gmt 0)

Hi Janharders,

I created cgi-bin folder based on the Perl path for the Perl server.

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3698091 posted 12:16 pm on Jul 16, 2008 (gmt 0)

what do you mean with "based on the perl path"?
in general, a cgi-bin is an ordinary directory but it has a ScriptAlias set up, so that every file in that directory will be treated as a script and executed upon request.
If you created the folder but it's not scriptaliased, then files will not be executed (unless the setup allows scripts to be executed everywhere and distinguishes them from normal files via the extension - .pl or .cgi are very commonly used). in that case, your "cgi-bin"-directory is a normal directory, just as if you named it "scripts" instead.
look for a pre-setup cgi-bin. often, it is on the same level as the htdocs, so you have a directory htdocs or html and a second directory cgi-bin, that, allthough not within the htdocs-directory is aliased to /cgi-bin/.
also, you should look at your webhosts website, they usually have a FAQ answering questions about how to install cgi-scripts etc.

daro2008

5+ Year Member



 
Msg#: 3698091 posted 12:38 pm on Jul 16, 2008 (gmt 0)

Hi janharders,

There is no cgi-bin folder created by the Webhosting server. I have been to the Cpanel which indicates that the path to the Perl server is: usr/bin/perl

I am very blind. I think the problem with the Server, but I don't know what it is.

Any advice is very appreciated.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3698091 posted 5:26 pm on Jul 16, 2008 (gmt 0)

this is the path to the perl interpreter.
therefore, the first line of all your perl scripts should be:
#!/usr/bin/perl

daro2008

5+ Year Member



 
Msg#: 3698091 posted 5:43 pm on Jul 16, 2008 (gmt 0)

Phranque,

Yes, I put in the first line of my perl srcipt the following:

#!/usr/bin/perl

Cheers

This 44 message thread spans 2 pages: 44 ( [1] 2 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved