How solve this error

Discussion in 'Perl Misc' started by mukunthini@gmail.com, Apr 23, 2007.

  1. Guest

    I'm new to Perl and am trying to get a simple counter script to work.
    It seems to be able to read the counter.dat file ok but I can't get
    it to write the new file count to the file. When im executing the
    script in command line it's working well and updating the couner.dat
    also to new value. but when im running the file in web browser it's
    reading the counter value.The counter get's incremented just fine, But
    I cannot get the thing to write back out to the counter.dat file with
    the new value. My file permissions seem to be ok. It reads the
    counter.dat file but will not write to it. and in the error log its
    giving the following error
    "hi.pl: Cannot open for writing: Permission denied at /var/www/cgi-
    bin/hi.pl line 13."
    im using Fedora linux, apache 2.0 and perl 5.6.2 .......and mozilla
    firefox browser to execute this file
    Anybody ever seen this happen?
    here is the code:

    #!/usr/local/bin/perl
    use CGI::Carp qw(fatalsToBrowser);
    use CGI qw/:standard/;
    print "Content-type: text/html\n\n"; # Web page as default output
    open (COUNT, "counter.dat"); # Open for read using COUNT file
    handle
    $hitcount = <COUNT>; # Read in file to get current count
    value
    close COUNT; # Close the data file
    $hitcount = $hitcount + 1; # Increment count value
    open (COUNT, "> counter.dat") or warn "Cannot open $file for writing:
    $!"; # Open data file for write
    flock(COUNT, 2);
    print COUNT "$hitcount"; # Write updated count value
    close COUNT; # Close the data file
    print "$hitcount"; # Display updated count on the page
    exit;

    thankx in advance.....!
    regards
    mukunthini
     
    , Apr 23, 2007
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > I'm new to Perl and am trying to get a simple counter script to work.
    > It seems to be able to read the counter.dat file ok but I can't get
    > it to write the new file count to the file. When im executing the
    > script in command line it's working well and updating the couner.dat
    > also to new value. but when im running the file in web browser it's
    > reading the counter value.The counter get's incremented just fine, But
    > I cannot get the thing to write back out to the counter.dat file with
    > the new value. My file permissions seem to be ok. It reads the
    > counter.dat file but will not write to it. and in the error log its
    > giving the following error
    > "hi.pl: Cannot open for writing: Permission denied at /var/www/cgi-
    > bin/hi.pl line 13."


    In view of that error message, what makes you think your permissions
    are ok?

    > im using Fedora linux, apache 2.0 and perl 5.6.2 .......and mozilla
    > firefox browser to execute this file
    > Anybody ever seen this happen?
    > here is the code:
    >
    > #!/usr/local/bin/perl


    No "strict"? No "warnings"?

    > use CGI::Carp qw(fatalsToBrowser);
    > use CGI qw/:standard/;
    > print "Content-type: text/html\n\n"; # Web page as default output
    > open (COUNT, "counter.dat"); # Open for read using COUNT file
    > handle


    Why aren't you checking the result of open()?

    > $hitcount = <COUNT>; # Read in file to get current count
    > value
    > close COUNT; # Close the data file
    > $hitcount = $hitcount + 1; # Increment count value
    > open (COUNT, "> counter.dat") or warn "Cannot open $file for writing:
    > $!"; # Open data file for write


    Here you *are* checking the success of open(), but the error message
    is misleading. You are opening "counter.dat", but in the error
    message you refer to "$file", which is nowhere set in your code.

    [rest of code snipped]

    Correct your code and go back to permission checking, that is
    likely the problem.

    Anno
     
    -berlin.de, Apr 23, 2007
    #2
    1. Advertising

  3. Guest

    Dear Anno,
    Thaks alot for your reply
    i've changed the codes as you mentioned, when i run in web browser it
    is displaying "1". that is from the last print cmd. under error log /
    var/log/httpd the following 2 errors displayed.
    [Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
    21:31:52 2007] hi.pl: Cannot open counter.dat for reading: No such
    file or directory at /var/www/cgi-bin/hi.pl line 7.
    [Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
    21:31:52 2007] hi.pl: Cannot open counter.dat for writing: Permission
    denied at /var/www/cgi-bin/hi.pl line 14.
    when i executed from terminal it gives this error " [Wed Apr 25
    21:51:00 2007] hi.pl: Cannot open counter.dat for reading: No such
    file or directory at hi.pl line 7 " and the new file "counter.dat" is
    created under working directory and "1" is stored in that file.
    Again if i execute the same form web browser now it is dispalying "2"
    and in the log only writing error displayed.so it is reading from the
    file, not writing the incremented value. when i execute again the same
    value "2" is displaying in web browser.
    But when i executing from terminal is working reading and wring new
    value to that file.

    #!/usr/local/bin/perl
    use CGI::Carp qw(fatalsToBrowser);
    use CGI qw/:standard/;
    print "Content-type: text/html\n\n";
    use strict;

    open (COUNT, "counter.dat") or warn "Cannot open counter.dat for
    reading: $!";
    my $hitcount = <COUNT>;
    close COUNT;

    $hitcount = $hitcount + 1;

    open (COUNT, "> counter.dat") or warn "Cannot open counter.dat for
    writing: $!";
    flock(COUNT, 2);
    print COUNT "$hitcount";
    close COUNT;

    print "$hitcount";

    exit;

    i have given read and write permission to both file and the directory
    (cgi-bin and counter.dat) (chmod 777)....

    So, is this error happened due to apache configuration mistake? do i
    need to change the httpd.conf file?
    or is that web browser (mozilla) doesnt have permission to write file?
    how can i overcome this? pls help me....

    regards,
    Mukunthini
     
    , Apr 25, 2007
    #3
  4. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Dear Anno,
    > Thaks alot for your reply


    Which reply do you mean? It's been almost three days. In the
    meantime, I've read hundreds of questions similar to yours
    and answered a few. Pleas quote some context when you reply.

    > i've changed the codes as you mentioned, when i run in web browser it
    > is displaying "1". that is from the last print cmd. under error log /
    > var/log/httpd the following 2 errors displayed.
    > [Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
    > 21:31:52 2007] hi.pl: Cannot open counter.dat for reading: No such
    > file or directory at /var/www/cgi-bin/hi.pl line 7.


    Okay, so the file doesn't exist where it is expected. You should
    check for that anyway and react accordingly.

    > [Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
    > 21:31:52 2007] hi.pl: Cannot open counter.dat for writing: Permission
    > denied at /var/www/cgi-bin/hi.pl line 14.


    Clearly, whoever is running the web server doesn't have write
    access to that directory.

    > when i executed from terminal it gives this error " [Wed Apr 25
    > 21:51:00 2007] hi.pl: Cannot open counter.dat for reading: No such
    > file or directory at hi.pl line 7 " and the new file "counter.dat" is
    > created under working directory and "1" is stored in that file.


    > Again if i execute the same form web browser now it is dispalying "2"
    > and in the log only writing error displayed.so it is reading from the
    > file, not writing the incremented value. when i execute again the same
    > value "2" is displaying in web browser.
    > But when i executing from terminal is working reading and wring new
    > value to that file.
    >
    > #!/usr/local/bin/perl
    > use CGI::Carp qw(fatalsToBrowser);
    > use CGI qw/:standard/;
    > print "Content-type: text/html\n\n";
    > use strict;
    >
    > open (COUNT, "counter.dat") or warn "Cannot open counter.dat for
    > reading: $!";
    > my $hitcount = <COUNT>;
    > close COUNT;
    >
    > $hitcount = $hitcount + 1;
    >
    > open (COUNT, "> counter.dat") or warn "Cannot open counter.dat for
    > writing: $!";
    > flock(COUNT, 2);
    > print COUNT "$hitcount";
    > close COUNT;
    >
    > print "$hitcount";
    >
    > exit;
    >
    > i have given read and write permission to both file and the directory
    > (cgi-bin and counter.dat) (chmod 777)....


    A risky thing to do. Apparently it hasn't had the desired effect
    because the web server still doesn't have write access.

    > So, is this error happened due to apache configuration mistake? do i
    > need to change the httpd.conf file?


    That is way off topic in clpm. I don't know.

    > or is that web browser (mozilla) doesnt have permission to write file?
    > how can i overcome this? pls help me....


    The *browser* has nothing to do with it. CGI happens on the web
    server.

    You'll have to discuss this in a news group that is about web server
    configuration. It's outside Perl territorry.

    Anno
     
    -berlin.de, Apr 25, 2007
    #4
  5. Joe Smith Guest

    wrote:
    > it gives this error " [Wed Apr 25
    > 21:51:00 2007] hi.pl: Cannot open counter.dat for reading: No such
    > file or directory at hi.pl line 7.
    >
    > open (COUNT, "counter.dat") or warn "Cannot open counter.dat for
    > reading: $!";


    You're not using an absolute pathname for the file.
    Since the CGI standard does _not_ specify what the current working directory
    will be while the script is running, you need to use the full name.

    my $file = "/home/mukinthini/public_html/counter.dat";
    open my $COUNT,'<',$file or warn "Cannot open $file for reading: $!";

    -Joe
     
    Joe Smith, Apr 26, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ajith Nair
    Replies:
    0
    Views:
    1,636
    Ajith Nair
    Aug 17, 2005
  2. Miguel Dias Moura

    How do i solve this error?

    Miguel Dias Moura, Dec 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,851
  3. Erland
    Replies:
    1
    Views:
    392
    =?Utf-8?B?RGFsZQ==?=
    Apr 13, 2006
  4. bruce
    Replies:
    0
    Views:
    498
    bruce
    Jul 21, 2008
  5. bruce
    Replies:
    0
    Views:
    628
    bruce
    Jul 21, 2008
Loading...

Share This Page