LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Unable to append to a file through perl/CGI (http://www.linuxquestions.org/questions/programming-9/unable-to-append-to-a-file-through-perl-cgi-800307/)

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

#!/usr/bin/perl -w
use strict;
use warnings;
print "Content-type: text/html\n\n";
print <<EOF;
<html>
<head>
<title>File Locking</title>
</head>
<body bgcolor = "#556677"></body>
<br><br>
<center><h1>Demonstrating File locking concept</h1></center>
EOF
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>";
  exit;
  }
}
flock(MYFILE,8);
close(MYFILE);
open(ASH, ">> $myfile");
flock(ASH, 2);
print ASH $employee;
print "<center>You are now in the list!\n</center>";
flock(ASH,8);
close(ASH);
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
Code:

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 04:57 AM.