Community Documentation

ip_address

6 bootstrap.inc ip_address()
7 bootstrap.inc ip_address()
8 bootstrap.inc ip_address()

Returns the IP address of the client machine.

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. The actual header name can be configured by the reverse_proxy_header variable.

Return value

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

▾ 23 functions call ip_address()

BootstrapIPAddressTestCase::testIPAddressHost in modules/simpletest/tests/bootstrap.test
test IP Address and hostname
CommentInterfaceTest::setEnvironment in modules/comment/comment.test
Re-configures the environment, module settings, and user permissions.
CommentInterfaceTest::testCommentNewCommentsIndicator in modules/comment/comment.test
Tests new comment marker.
comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
DBLogTestCase::generateLogEntries in modules/dblog/dblog.test
Generate dblog entries.
DBLogTestCase::testDBLogAddAndClear in modules/dblog/dblog.test
Login an admin user, create dblog event, and test clearing dblog functionality through the admin interface.
drupal_anonymous_user in includes/bootstrap.inc
Generates a default anonymous $user object.
drupal_block_denied in includes/bootstrap.inc
Handles denied users.
flood_clear_event in includes/common.inc
Makes the flood control mechanism forget an event for the current visitor.
flood_is_allowed in includes/common.inc
Checks whether a user is allowed to proceed with the specified event.
flood_register_event in includes/common.inc
Registers an event for the current visitor to the flood control mechanism.
openid_verify_assertion_nonce in modules/openid/openid.module
Verify that the nonce has not been used in earlier assertions from the same OpenID provider.
poll_cancel in modules/poll/poll.module
Submit callback for poll_cancel_form().
poll_load in modules/poll/poll.module
Implements hook_load().
poll_vote in modules/poll/poll.module
Submit handler for processing a vote.
statistics_exit in modules/statistics/statistics.module
Implements hook_exit().
system_block_ip_action in modules/system/system.module
Blocks the current 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
Logs 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
Attempts to serve a page from the cache.
_drupal_session_write in includes/session.inc
Writes an entire session to the database (internal use only).

File

includes/bootstrap.inc, line 2775
Functions that need to be loaded on every Drupal request.

Code

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

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

    if (variable_get('reverse_proxy', 0)) {
      $reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
      if (!empty($_SERVER[$reverse_proxy_header])) {
        // 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());

        // Turn XFF header into an array.
        $forwarded = explode(',', $_SERVER[$reverse_proxy_header]);

        // Trim the forwarded IPs; they may have been delimited by commas and spaces.
        $forwarded = array_map('trim', $forwarded);

        // Tack direct client IP onto end of forwarded array.
        $forwarded[] = $ip_address;

        // Eliminate all trusted IPs.
        $untrusted = array_diff($forwarded, $reverse_proxy_addresses);

        // The right-most IP is the most specific we can trust.
        $ip_address = array_pop($untrusted);
      }
    }
  }

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