drupal_override_server_variables

Versions
7
drupal_override_server_variables($variables = array())

Set appropriate server variables needed for command line scripts to work.

This function can be called by command line scripts before bootstrapping Drupal, to ensure that the page loads with the desired server parameters. This is because many parts of Drupal assume that they are running in a web browser and therefore use information from the global PHP $_SERVER variable that does not get set when Drupal is run from the command line.

In many cases, the default way in which this function populates the $_SERVER variable is sufficient, and it can therefore be called without passing in any input. However, command line scripts running on a multisite installation (or on any installation that has settings.php stored somewhere other than the sites/default folder) need to pass in the URL of the site to allow Drupal to detect the correct location of the settings.php file. Passing in the 'url' parameter is also required for functions like request_uri() to return the expected values.

Most other parameters do not need to be passed in, but may be necessary in some cases; for example, if Drupal's ip_address() function needs to return anything but the standard localhost value ('127.0.0.1'), the command line script should pass in the desired value via the 'REMOTE_ADDR' key.

See also

conf_path()

@see request_uri()

See also

ip_address()

Parameters

$variables (optional) An associative array of variables within $_SERVER that should be replaced. If the special element 'url' is provided in this array, it will be used to populate some of the server defaults; it should be set to the URL of the current page request, excluding any $_GET request but including the script name (e.g., http://www.example.com/mysite/index.php).

Code

includes/bootstrap.inc, line 439

<?php
function drupal_override_server_variables($variables = array()) {
  // Set defaults based on the provided URL.
  if (isset($variables['url'])) {
    $url = parse_url($variables['url']);
    unset($variables['url']);
  }
  else {
    $url = array();
  }
  $url += array(
    'path' => '',
    'host' => 'localhost',
  );
  $defaults = array(
    'HTTP_HOST' => $url['host'],
    'SCRIPT_NAME' => $url['path'],
    'REMOTE_ADDR' => '127.0.0.1',
    'REQUEST_METHOD' => 'GET',
    'SERVER_NAME' => NULL,
    'SERVER_SOFTWARE' => NULL,
    'HTTP_USER_AGENT' => NULL,
  );
  // Replace elements of the $_SERVER array, as appropriate.
  $_SERVER = $variables + $_SERVER + $defaults;
}
?>
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.