?
Solved

Connecting CGI Perl script to other files

Posted on 2003-11-25
7
Medium Priority
?
550 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 84

Assisted Solution

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

Author Comment

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

Accepted Solution

by:
apparition earned 200 total points
ID: 9826723
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 3

Assisted Solution

by:rkosai
rkosai earned 200 total points
ID: 9827014
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 200 total points
ID: 9830205
use CGI::Carp   qw(fatalsToBrowser);
0
 

Author Comment

by:million1977
ID: 9859269
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
ID: 9885008
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

752 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