[drupal-devel] [feature] Replace $_SERVER["REMOTE_ADDR"] with a function which understands X-Forwarded-For

degerrit drupal-devel at drupal.org
Sat May 21 21:44:52 UTC 2005

Issue status update for http://drupal.org/node/20471

 Project:      Drupal
-Version:      4.5.2
+Version:      4.6.0
 Component:    base system
 Category:     feature requests
 Priority:     normal
-Assigned to:  Anonymous
+Assigned to:  degerrit
 Reported by:  degerrit
 Updated by:   degerrit
-Status:       active
+Status:       patch
 Attachment:   http://drupal.org/files/issues/remote_addr.diff (6.07 KB)

Here's my proposal for showing user IP addresses behind proxy servers,
especially in log files. It adds a function remote_addr() and replaces
all the uses of $_SERVER["REMOTE_ADDR"] with that function.

This is my first patch for Drupal, don't be too harsh one me :-)

A few notes:
- I patched session.inc because $_SERVER["REMOTE_ADDR"] is also called
in session.inc and common.inc is not available at that time it seems
- maybe I replaced too vigorously in common.inc and sessions.inc, it
works for me though
- maybe it should be renamed http_remote_addr or apache_remote_addr


Previous comments:

April 14, 2005 - 00:42 : degerrit

In several modules, replace $_SERVER["REMOTE_ADDR"] with a function
which understands X-Forwarded-For.

Proxy servers often (sometimes, always?) insert "X-Forwarded-For"
headers which contains the "real" client IP address. I think it is
useful to log the client IP address in that situation.

This little snippet works for me, and I'd suggest something similar be
made into a function?

$headers = apache_request_headers();
if (array_key_exists('X-Forwarded-For', $headers)){
  $hostname=$headers['X-Forwarded-For'] . ' via ' .
} else {

I can make a patch proposal if people like the idea.

More information about the drupal-devel mailing list