Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Re: CGI file writing

by mcogan1966 (Monk)
on Oct 29, 2003 at 13:59 UTC ( #303002=note: print w/replies, xml ) Need Help??

in reply to Re: CGI file writing
in thread CGI file writing

Original text removed due to programmer error.

I goofed, and forgot to remove the # from the beginning of the line. My bad, worked fine. Thanks.

But my concern is that I now have to have a hard-coded directory for this. This code is going to be moved to a different server for production, so I either have to change the directory, or find a different solution.

Replies are listed 'Best First'.
Re: Re: Re: CGI file writing
by hmerrill (Friar) on Oct 29, 2003 at 14:23 UTC

    I don't quite understand - you are writing the output of a cgi script to a log file. Do you understand that the webserver usually already has a STDERR log file - the Apache webserver has a log file at /var/log/httpd/error_log and when your perl cgi script writes to STDERR, those log messages appear in that error_log file.

    Assuming you already understand that, and you still want to write to a separate log file from your cgi script, then you either have to hardcode the absolute log filename (including the path), or you could use the DOCUMENT_ROOT environment variable to base the log file location on. For example:

    my $logfile = qq!$ENV{"DOCUMENT_ROOT"}/my_logfile!; open(LOG, ">>$logfile") || die "Can't open $logfile: $!"; print LOG "Some message from my CGI script!\n";

    But the danger there is that your log file will be accessible under the document root - so if your webserver gets compromised, your log file could be read by the attacker.

    I would recommend either having your cgi script write to STDERR so that the message will appear in the standard webserver error log (outside the document root), or hardcode the location of the log file in your script, or put the location of the logfile in a site module that gets "use"d by your cgi script on both your dev server and your prod server.

    One other option if you want to keep your log file separate from the standard webserver error log, is to use the environment variable HTTP_HOST or SERVER_NAME to construct an if test around which log file name(and path) to use.

      That's not what I'm doing here.
      This is a custom search engine, and I'm saving some of the search paramaters each time a search is done. This data is to be stored in a specific location. Problem was, I couldn't figure out where to get it to start without having to hard-code the directory structure in. STDERR isn't the solution I'm looking for.
Re: Re: Re: CGI file writing
by barrd (Abbot) on Oct 29, 2003 at 14:27 UTC
    Hi mcogan1966,
    How I normally get around this is either create a module .pm which can be included in any dir in @INC and called with use Foo::Bar;. Or a library .pl called with require /path/to/;.

    You would then make filenames and paths variables which are read in from the file making it portable throughout many systems without having to touch the script itself.

      Actually, I found a different solution.
      Since I know that the server structures are going to be identical from my development system to the production system (as designed by the network admin who built this whole thing), I tried doing the relative path using ../../var/logs. Sure enough it works properly. Now, I don't have to worry about what system it's on.

      Yeah, I know, it's not exactly the best way to do it for overall portability. But that's not the situation here, and it's not like this code is ever going to go out into the wild. It's strictly for this client, for use in just this system (as is always the wonderful part about writing code for GovCo).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://303002]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-21 09:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.