Jump to content

mod_authz_dbd Custom DB Query


apacheguy

Recommended Posts

Hi all,

 

I recently transitioned to Apache 2.4 from 2.2.  I'm looking to take advantage of mod_authz_dbd to replace my Basic Auth scheme I had in place before.

 

Per the docs on this module (http://httpd.apache.org/docs/trunk/mod/mod_authz_dbd.html), I can execute a custom statement to log a user in like this:

 

 

# dbd-login action executes a statement to log user in

Require dbd-login
AuthzDBDQuery "UPDATE authn SET login = 'true' WHERE user = %s"

 

Trouble is, I want it to log the user IP address. Apache supposedly supports calling ENV VARS from with httpd.conf, but for some reason the following code does not work:

 

 

Require dbd-login
AuthzDBDQuery "INSERT INTO Logins (User, IP) VALUES (%s, '${REMOTE_ADDR}')"

 

It inserts %{REMOTE_ADDR} instead of the actual ip address value. Any idea how I can get this to work?

Link to comment
https://forums.phpfreaks.com/topic/294447-mod_authz_dbd-custom-db-query/
Share on other sites

  • 2 months later...


if ($this->validate_ip($ip))
return $ip;
}
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];

// Return unreliable IP address since all else failed
return $_SERVER['REMOTE_ADDR'];
}

/**
* Ensures an IP address is both a valid IP address and does not fall within
* a private network range.
*
* @access public
* @param string $ip
*/
public function validate_ip($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4 |
FILTER_FLAG_IPV6 |
FILTER_FLAG_NO_PRIV_RANGE |
FILTER_FLAG_NO_RES_RANGE) === false)
return false;
self::$ip = $ip;
return true;
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.