Solved

Connecting CGI Perl script to other files

Posted on 2003-11-25
7
532 Views
Last Modified: 2013-12-25
Hi, I'm trying to implement a very basic guestbook on my webpage based on an example in a textbook. The HTML form works fine, but there is a problem with the Perl/CGI (extra print statements added to aid debugging):

#!/usr/bin/perl -wT
use strict;
use CGI qw(:all);
use Fcntl qw(:flock);

# Location of the guestbook log file.
my $gbdata="http://www.mysite.net/data/log.txt";
my $semaphore_file="http://www.mysite.net/temp/sample.sem";

# Function to lock (waits indefinitely)
sub get_lock {
   open(SEM, ">$semaphore_file")
       || die "Cannot create semaphore: $!";
      print "<br /> done open";
   flock SEM, LOCK_EX;
      print "<br /> done lock";
}

# Function to unlock
sub release_lock {
    close(SEM);
}

# This function saves a passed-in help desk HTML form to a file
sub save {
     print "<br />Saving";
     get_lock();
      print "1";

     open(GB, ">>$gbdata") || die "Cannot open $gbdata: $!";
      print "2";
     print GB "name: ", param('name'), "\n";
     print GB "e-mail: ", param('email'), "\n";
     print GB "wpage: ", param('wpage'), "\n";
     print GB "entry: ", param('problem'), "\n";
     close(GB);
     release_lock();
}

# This function displays the contents of the help desk log file as HTML,
# with minimal formatting.
sub display {
     print "<br />Displaying";
      print "<br />The name was ", param('name');
     open(GB, $gbdata) || die "Cannot open $gbdata: $!";
     while(<GB>){
        print "Another entry:";
          print "<b>$_</b><br />";  # The name
          my($email,$wpage,$entry);
          $email=<GB>;
          $wpage=<GB>;
        $entry=<GB>;
          print "$email<br />";
          print "$wpage<br />";
        print "$entry<br /><br />";
     }
     close(GB);
}

print header;
print "Guest book under development";
# The parameter 'submit' is only passed if this CGI program was
# executed by pressing the 'submit' button in the form in listing 18.7

if (defined param('submit')) {
     save;
      print "<br />Finished saving";
     display;
} else {
     print "<br />Doing display only";
     display;
}
# END OF CODE

The problem is that the program gets stuck at the following line in sub get_lock:    open(SEM, ">$semaphore_file")
Or, if I forget about locking by removing get_lock and release_lock, the program gets stuck at the following line in sub save:   open(GB, ">>$gbdata") || die "Cannot open $gbdata: $!";
In neither case is the message in the 'die' statement printed on the web-page.

So the problem is that the program fails to create the semaphore file or open the log file (for the guestbook messages). I have set permissions as instructed by my web-hosting company.

Anybody any idea what might be causing the problem? Many thanks in advance for your help.
0
Comment
Question by:million1977
7 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
Comment Utility
you probably don't want the "http://www.mysite.net" in your $gbdata or
$semaphore_file
0
 

Author Comment

by:million1977
Comment Utility
thanks, but that hasn't made any difference
0
 
LVL 5

Accepted Solution

by:
apparition earned 50 total points
Comment Utility
is the script being run in a cgi-bin directory?
if so then use
my $gbdata="data/log.txt";
my $semaphore_file="temp/sample.sem";

and create blank files "data/log.txt" and temp/sample.sem
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Assisted Solution

by:rkosai
rkosai earned 50 total points
Comment Utility
You will also want to change the permissions on the files/directories mentioned by apparition, make sure they're set to something like 766, so that your script can write to those locations.

Having the "http://" in your file path will not give the intended result; its better to code in a relative directory (either "../data/file.txt" if its outside your cgi-bin directory, or "data/file.txt").
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 50 total points
Comment Utility
use CGI::Carp   qw(fatalsToBrowser);
0
 

Author Comment

by:million1977
Comment Utility
Sorted the problem, I'm new to Perl & CGI so all your comments were useful.
Found ahoffmann's particularly helpful for debugging.
50 points to each of you
0
 

Author Comment

by:million1977
Comment Utility
Sorry about the hold-up people, the link to Split Points isn't working for me, have messaged the Community Support board so hopefully it'll be sorted soon.

apologies again
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now