Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Printing Contents of Array in External File to Browser

    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.

    Code:
    # 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() );

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    # 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

    Code:
    # 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:
    Code:
    print header,
       start_html,
       checkFraud(),
       end_html;

    best regards
    Last edited by oesxyl; 10-28-2009 at 11:03 PM.

  • Users who have thanked oesxyl for this post:

    pppebble88 (10-29-2009)

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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!

    Code:
    # 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

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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"
    Code:
    use CGI::Carp qw(fatalsToBrowser);

  • Users who have thanked FishMonger for this post:

    pppebble88 (10-29-2009)

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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.

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

    best regards
    Last edited by oesxyl; 10-28-2009 at 11:47 PM.

  • Users who have thanked oesxyl for this post:

    pppebble88 (10-29-2009)

  • #6
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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!

    Code:
    #!/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() );

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    pppebble88 (10-29-2009)

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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.

  • #9
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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.

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #12
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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!

  • #13
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Question:

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

    Code:
    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);
    }

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by pppebble88 View Post
    Question:

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

    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •