-   Programming (
-   -   Unable to append to a file through perl/CGI (

ashok.g 04-06-2010 05:48 AM

Unable to append to a file through perl/CGI
Hi folks,
Please look at the following code, where I'm trying to demonstrate the file locking concept through Perl/CGI.
Here is the script which I tried.

#!/usr/bin/perl -w
use strict;
use warnings;
print "Content-type: text/html\n\n";
print <<EOF;
<title>File Locking</title>
<body bgcolor = "#556677"></body>
<center><h1>Demonstrating File locking concept</h1></center>
my $myfile = "Employee.txt";
my $employee = "Manoj";
open(MYFILE, "$myfile");
flock(MYFILE, 1);
while(defined($a=<MYFILE>)) {
  if ($a=~m/^$employee$/) {
  print "<center>You are already in the list!\n</center>";
open(ASH, ">> $myfile");
flock(ASH, 2);
print ASH $employee;
print "<center>You are now in the list!\n</center>";
print "<body><html>";

Here, I'm not able to append the text and also unable to lock(shared lock) the file.
For your information, this is executing fine when I run the same as as

perl filelock.cgi
But when I run the same script in a browser, I'm not getting it fine.
Please assist me.

smoker 04-06-2010 12:48 PM

Check your error logs. You don't need use warnings, you have -w
And use is not for warnings anyway.

theNbomr 04-06-2010 07:44 PM

The script has a whole world of different capabilities when run as a logged-in user compared to running as a CGI under a HTTP server userID. One principle of security is to disallow writes in the document root, server root, and cgi-bin directory.
How do you run a script in a browser?

--- rod.

All times are GMT -5. The time now is 07:59 PM.