Solved

Connecting CGI Perl script to other files

Posted on 2003-11-25
7
541 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
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 50 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Assisted Solution

by:rkosai
rkosai earned 50 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

11 Experts available now in Live!

Get 1:1 Help Now