...

View Full Version : Printing Contents of Array in External File to Browser



pppebble88
10-28-2009, 09:49 PM
I am trying to write the contents of an external file onto the web browser. The ultimate goal is to compare the entires in the text file to entries submitted through a form. If they match, perform an operation. When I run the perl command "perl -w submit.pl" in my command prompt, it works correctly. However, when I try to run it in a web browser, nothing displays. I have tried everything I can think of and can't get it working. I would appreciate some help. Thanks a lot.



# Standard header stuff
use CGI qw( :standard );
use strict;
print( header() );
print( start_html() );

checkFraud();

sub checkFraud {
my $filename = "../fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);

}
print ( end_html() );

oesxyl
10-28-2009, 11:00 PM
# Standard header stuff
use CGI qw( :standard );
use strict;

sub checkFraud {
my $filename = "../fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);

}


print( header() );
print( start_html() );
checkFraud();
print ( end_html() );


or



# Standard header stuff
use CGI qw( :standard );
use strict;
print( header() );
print( start_html() );
&checkFraud();
print ( end_html() );

sub checkFraud {
my $filename = "../fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);

}


you don't need to use print so many times. This will also work:


print header,
start_html,
checkFraud(),
end_html;




best regards

pppebble88
10-28-2009, 11:15 PM
Thanks for the help...Unfortunately, I am still getting the same results (nothing is displayed in the web browser, but it works fine in the command prompt)

Below is the complete perl file (I have a few other functions declared, but all of them function FINE, which is why I didn't originally include them...)

Thanks again for the continued help!



# Standard header stuff
use CGI qw( :standard );
use strict;


# Get inputs
my $name = param("name");
my $phone = param("phone");
my $email = param("email");
my $address = param("streetAddress");
my $city = param("city");
my $state = param("state");
my $zip = param("zip");
my $passwd = param("passwd");
my @events = param("events");
my $firstClub = param("firstClub");
my $comments = param("experience");
my $years = param("years");
my $validInput = 1;



#validateInfo();
#if($validInput == 1) {
# printHeader();
#}
#checkFraud();

sub validateInfo {
my $message1 = "";
my $message2 = "";
my $message3 = "";
if(($name eq "") ) {
$message1 = "You must enter a name!";
$validInput = 0;
}
if(!($email =~ /^\w.+\@\w+(\.org|\.net|\.com)$/i )){
$message2 = "You must enter a valid email addres (Eg. home\@home.net or home\@home.com or home\@home.org) ";
$validInput = 0;
}
if((!@events)) {
$message3 = "You must select at least ONE event";
$validInput = 0;
}
if(!$validInput) {
print h1("$message1" . "<br />" . "$message2" . "<br />" . "$message3");
print ("Click <a href='http://www.m111662.it350.cs.usna.edu/Lab08/register.html'>HERE</a> to go back and make the appropriate corrections <br />");
}

}

sub printHeader {
print h1("Registration Submitted!");
print ("Below are the details of your submission. Thanks a lot! <br />");
print ("<br />");
print ("Name: $name <br />");
print ("Phone: $phone <br />");
print ("Email: $email <br />");
print ("Address: $address <br />");
print ("City: $city <br />");
print ("State: $state <br />");
print ("Zip: $zip <br />");
print ("Password: $passwd <br />");
print ("Events: @events <br />");
print ("Is this your first time to be a member of a running club?: $firstClub <br />");
print ("Experience: $comments <br />");
print ("Years of running experience: $years <br />");
}

sub checkFraud {
my $filename = "../fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}

print( header() );
print( start_html() );
checkFraud();
print ( end_html() );


-W

FishMonger
10-28-2009, 11:33 PM
Have you checked the web server error log?

Have you verified that the permissions are set correctly?

Is the script it the cgi-bin directory?

What url are you specifying?

Add this with the "Standard header stuff"

use CGI::Carp qw(fatalsToBrowser);

oesxyl
10-28-2009, 11:44 PM
also check the path to the "../fraud-values.txt" file. I suspect that this is the problem.
it work for me from command line and as cgi script.

by the way, you don't have shbang in what you post here, I hope you have it in the original code
best regards

pppebble88
10-29-2009, 01:43 AM
Ok,

I have tried what yall have suggested and I get the following error:



Software error:

Couldn't open fraud-values.txt at d:\inetpub\it350\www.m111662.it350.cs.usna.edu\Lab08\submit.pl line 75.

The text file is in the same directory as the submit.pl script. Below is the exact code that is in the perl script. Thanks for all of the continued help!


#!/usr/local/bin/perl
# Standard header stuff
use CGI qw( :standard );
use strict;
use CGI::Carp qw(fatalsToBrowser);


# Get inputs
my $name = param("name");
my $phone = param("phone");
my $email = param("email");
my $address = param("streetAddress");
my $city = param("city");
my $state = param("state");
my $zip = param("zip");
my $passwd = param("passwd");
my @events = param("events");
my $firstClub = param("firstClub");
my $comments = param("experience");
my $years = param("years");
my $validInput = 1;



#validateInfo();
#if($validInput == 1) {
# printHeader();
#}
#checkFraud();

sub validateInfo {
my $message1 = "";
my $message2 = "";
my $message3 = "";
if(($name eq "") ) {
$message1 = "You must enter a name!";
$validInput = 0;
}
if(!($email =~ /^\w.+\@\w+(\.org|\.net|\.com)$/i )){
$message2 = "You must enter a valid email addres (Eg. home\@home.net or home\@home.com or home\@home.org) ";
$validInput = 0;
}
if((!@events)) {
$message3 = "You must select at least ONE event";
$validInput = 0;
}
if(!$validInput) {
print h1("$message1" . "<br />" . "$message2" . "<br />" . "$message3");
print ("Click <a href='http://www.m111662.it350.cs.usna.edu/Lab08/register.html'>HERE</a> to go back and make the appropriate corrections <br />");
}

}

sub printHeader {
print h1("Registration Submitted!");
print ("Below are the details of your submission. Thanks a lot! <br />");
print ("<br />");
print ("Name: $name <br />");
print ("Phone: $phone <br />");
print ("Email: $email <br />");
print ("Address: $address <br />");
print ("City: $city <br />");
print ("State: $state <br />");
print ("Zip: $zip <br />");
print ("Password: $passwd <br />");
print ("Events: @events <br />");
print ("Is this your first time to be a member of a running club?: $firstClub <br />");
print ("Experience: $comments <br />");
print ("Years of running experience: $years <br />");
}

sub checkFraud {
my $filename = "fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}

print( header() );
print( start_html() );
checkFraud();
print ( end_html() );

oesxyl
10-29-2009, 01:51 AM
ooops, windowsss, :)
I'm not familiar with this os, :)
anyway check what FishMonger suggested, cgi-bin, server logs, permission, url, read his post again.

best regards

FishMonger
10-29-2009, 03:26 AM
Since you're using inetpub, it would appear that you're using IIS, which I haven't used for a long time. And, since this looks to be a homework assignment, we won't be able to provide a complete answer. However, we still can give you a little guidance.

Try specifying the full path to fraud-values.txt.

pppebble88
10-29-2009, 03:38 AM
FishMonger,

Thanks for the info. This is actually a lab and we are allowed to use any sources available to us. I figured it was a "path" issue, but if I do not know the directory structure, how am I going to insert the full path to the .txt file?

Thanks.

FishMonger
10-29-2009, 03:45 AM
Well, you stated that the text file is in the same directory as the script, so in what directory did you put the script. BIG hint, look at your error message.

pppebble88
10-29-2009, 03:48 AM
The text file IS in the same directory as the script...I am wondering why it is adding the "www.m111662.it350.cs.usna.edu\Lab08\submit.pl" onto the path "d:\inetpub\it350\"...That is where I think the problem is...It is using both a full path in addition to the web site address...That is what the error message says...

I must be missing something. Thanks.

pppebble88
10-29-2009, 03:52 AM
GOT IT!!!

I used the following additions to my code:

use Cwd 'abs_path';
print abs_path($0);

to get the full path and then set the path to:

my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";

Awesome...Thanks for all of the help!

pppebble88
10-29-2009, 03:57 AM
Question:

Shouldn't this code match the value in $name to the currently entry being examined from the file?


sub checkFraud {
my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
if($currentLine eq $name) {
print ("FRAUD!");
}
}
close(INPUT_FILE);
}

oesxyl
10-29-2009, 06:35 AM
Question:

Shouldn't this code match the value in $name to the currently entry being examined from the file?


sub checkFraud {
my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";

open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
if($currentLine eq $name) {
print ("FRAUD!");
}
}
close(INPUT_FILE);
}
depend if $name is defined somewhere and what is inside. :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum