http://www.perlmonks.org?node_id=63937

tame1 has asked for the wisdom of the Perl Monks concerning the following question:

I followed a small tutorial and was able to modify my Apache to write accesses to one certain directory into a database, by makeing a Apache::MyAccess.pm.

Everything is fine, accept one small thing: I cannot seem to grab the environment variable REMOTE_USER no matter what method I use.

I know for a fact that it is set, as I had to enter a username/pass combo just to access the directory I am logging.

Here is the code I am using:

package Apache::TaartsLog; use CGI qw(:cgi); use Apache (); use Apache::Constants qw(:common &OPT_EXECCGI &REDIRECT); use DBI; my $query = new CGI(); $dbh = DBI->connect("dbi:mysql:taarts","jrobiso2","") || die "$DBI::er +rstr: $!\n"; sub handler { my $r = shift; # @_[0] contains a reference to # the current request handler my ($request); my ($hostname); # Returns a blessed reference if ($r->main) { # unless this is the main request $orig = $r->main; } else { $orig = $r; } $hostname = $orig->get_remote_host; $request = $orig->filename; $user = $query->remote_user(); # this apparently fails. unless (($request =~ /jpg/) || ($request =~ /gif/)) { $dbh->do("insert into logs (host,request,time_stamp,cdsid) +VALUES ('$hostname','$request',NOW(),'$user')"); } } END { undef($dbh); } 1;

Can anyone tell me why the $query->remote_user() is failing? The other three fields get written to the database just fine. Only the remote_user is always blank.

What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

Replies are listed 'Best First'.
Re: Apache Modules
by larryl (Monk) on Mar 13, 2001 at 00:32 UTC

    And now that I think about it a bit more... If all you're using the CGI object for is to get the remote user name, why not just use:
    $r->connection->user()
    instead? That will return the user name typed in for Basic authentication. You can also get the password from:
    ($ret,$pw) = $r->get_basic_auth_pw().

      BINGO!!!!!

      Thanks for the $r->connection->user(); It worked!

      I had searched through Apache.pm, etc. looking for such a thing. Is there a better write-up somewhere? (Before you answer, I own "Programming Apache Modules with Perl" but just haven't read it yet!)

      What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"
Re: Apache Modules
by larryl (Monk) on Mar 13, 2001 at 00:21 UTC

    The code outside your handler ( my $query = new CGI()) will get executed at server startup. Only code inside your handler gets executed on each request. Try moving that line inside your handler and see what happens.

Re: Apache Modules
by LunaticLeo (Scribe) on Mar 14, 2001 at 01:40 UTC
    The basic problem is that you are using CGI.pm in a non-CGI environment. When invoking a CGI the apache server handler for CGIs sets up the environment variables and STDIN/STDOUT filehandles just before it executes the CGI program. The CGI apache handler sets up the CGI runtime environment *from* the info contained/accessable in the $r request object.

    The big "Ahha!" experience for me was to realize that the $r request object is the rough equivalent of the $q CGI.pm query object. Actually the $r request object has alot more info in it, which is great.