Search code examples
htmlperlmason

IP address of form submitter (remote IP address) using Perl and HTML::Mason


I am using HTML::Mason to serve a web page containing a form. When the form is filled and submitted, I want to know the IP address of the remote client.

I tried printing $r->headers_in but I get

Apache2::Request=SCALAR(0x1961ba0)->headers_in

am I using the wrong argument?


Solution

  • For a CGI application, the remote IP address is in $ENV{REMOTE_ADDR}.

    If you are using mod_perl, then the equivalent value is at $r->connection->remote_ip where $r is your Apache2::Request object.

    The output you show will be produced if you have the method call in double quotes, like

    print "$r->headers_in\n"
    

    it will work properly if you remove the quotes

    print $r->headers_in, "\n"
    

    The $r->headers_in method returns an APR::Table object. You can dump the contents of this using the do method and a suitable subroutine (which must return 1 for the iteration through the table to continue) like this:

    my $table = $r->headers_in;
    $table->do(sub {
      printf "%s: %s\n", @_;
      1;
    });