ip_address

Versions
6 – 7
ip_address()

If Drupal is behind a reverse proxy, we use the X-Forwarded-For header instead of $_SERVER['REMOTE_ADDR'], which would be the IP address of the proxy server, and not the client's. If Drupal is run in a cluster we use the X-Cluster-Client-Ip header instead.

Return value

IP address of client machine, adjusted for reverse proxy and/or cluster environments.

▾ 17 functions call ip_address()

comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
drupal_anonymous_user in includes/bootstrap.inc
Generates a default anonymous $user object.
drupal_block_denied in includes/bootstrap.inc
Handle denied users.
flood_clear_event in includes/common.inc
Make the flood control mechanism forget about an event for the current visitor.
flood_is_allowed in includes/common.inc
Check if the current visitor is allowed to proceed with the specified event.
flood_register_event in includes/common.inc
Register an event for the current visitor to the flood control mechanism.
poll_cancel in modules/poll/poll.module
Submit callback for poll_cancel_form().
poll_load in modules/poll/poll.module
Implement hook_load().
poll_vote in modules/poll/poll.module
Submit handler for processing a vote.
statistics_exit in modules/statistics/statistics.module
Implement hook_exit().
system_block_ip_action in modules/system/system.module
Implement a Drupal action. Blocks the user's IP address.
system_ip_blocking_form_validate in modules/system/system.admin.inc
user_login_authenticate_validate in modules/user/user.module
A validate handler on the login form. Check supplied username/password against local users table. If successful, $form_state['uid'] is set to the matching user ID.
watchdog in includes/bootstrap.inc
Log a system message.
watchdog_skip_semaphore in modules/simpletest/tests/actions_loop_test.module
Replacement of the watchdog() function that eliminates the use of semaphores so that we can test the abortion of an action loop.
_drupal_bootstrap_page_cache in includes/bootstrap.inc
Bootstrap page cache: Try to serve a page from cache.
_drupal_session_write in includes/session.inc
Session handler assigned by session_set_save_handler().

Code

includes/bootstrap.inc, line 1850

<?php
function ip_address() {
  $ip_address = &drupal_static(__FUNCTION__);

  if (!isset($ip_address)) {
    $ip_address = $_SERVER['REMOTE_ADDR'];

    if (variable_get('reverse_proxy', 0)) {
      if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
        // If an array of known reverse proxy IPs is provided, then trust
        // the XFF header if request really comes from one of them.
        $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
        if (!empty($reverse_proxy_addresses) && in_array($ip_address, $reverse_proxy_addresses, TRUE)) {
          // The "X-Forwarded-For" header is a comma+space separated list of IP addresses,
          // the left-most being the farthest downstream client. If there is more than
          // one proxy, we are interested in the most recent one (i.e. last one in the list).
          $ip_address_parts = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
          $ip_address = trim(array_pop($ip_address_parts));
        }
      }

      // When Drupal is run in a cluster environment, REMOTE_ADDR contains the IP
      // address of a server in the cluster, while the IP address of the client is
      // stored in HTTP_X_CLUSTER_CLIENT_IP.
      if (array_key_exists('HTTP_X_CLUSTER_CLIENT_IP', $_SERVER)) {
        $ip_address = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
      }
    }
  }

  return $ip_address;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.