drupal_goto

5 common.inc drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302)
6 common.inc drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302)
7 common.inc drupal_goto($path = '', array $options = array(), $http_response_code = 302)
8 common.inc drupal_goto($path = '', array $options = array(), $http_response_code = 302)

Sends the user to a different Drupal page.

This issues an on-site HTTP redirect. The function makes sure the redirected URL is formatted correctly.

Usually the redirected URL is constructed from this function's input parameters. However you may override that behavior by setting a destination in either the $_REQUEST-array (i.e. by using the query string of an URI) This is used to direct the user back to the proper page after completing a form. For example, after editing a post on the 'admin/content'-page or after having logged on using the 'user login'-block in a sidebar. The function drupal_get_destination() can be used to help set the destination URL.

Drupal will ensure that messages set by drupal_set_message() and other session data are written to the database before the user is redirected.

This function ends the request; use it instead of a return in your menu callback.

Parameters

$path: A Drupal path or a full URL.

$options: An associative array of additional URL options to pass to url().

$http_response_code: The valid values for 3xx redirection status codes are defined in RFC 2616 and the draft for the new HTTP status codes:

  • 301: Moved Permanently (the recommended value for most redirects).
  • 302: Found (default in Drupal and PHP, sometimes used for spamming search engines).
  • 303: See Other.
  • 304: Not Modified.
  • 305: Use Proxy.
  • 307: Temporary Redirect.

See also

drupal_get_destination()

url()

Related topics

44 calls to drupal_goto()

5 string references to 'drupal_goto'

File

includes/common.inc, line 669
Common functions that many Drupal modules will need to reference.

Code

function drupal_goto($path = '', array $options = array(), $http_response_code = 302) {
  // A destination in $_GET always overrides the function arguments.
  // We do not allow absolute URLs to be passed via $_GET, as this can be an attack vector.
  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $destination = drupal_parse_url($_GET['destination']);
    $path = $destination['path'];
    $options['query'] = $destination['query'];
    $options['fragment'] = $destination['fragment'];
  }

  drupal_alter('drupal_goto', $path, $options, $http_response_code);

  // The 'Location' HTTP header must be absolute.
  $options['absolute'] = TRUE;

  $url = url($path, $options);

  header('Location: ' . $url, TRUE, $http_response_code);

  // The "Location" header sends a redirect status code to the HTTP daemon. In
  // some cases this can be wrong, so we make sure none of the code below the
  // drupal_goto() call gets executed upon redirection.
  drupal_exit($url);
}

Comments

example to send query string using drupal_goto

Following is the eexample to send query string using drupal_goto

drupal_goto('your_url', array('query'=>array(
'variable_name1'=>'variable_value1',
'variable_name2'=>'variable_value2',
'variable_name3'=>'variable_value3',
...... ...
)));

Using drupal_get_destination

For some reason the syntax for the common case of using this with drupal_get_destination on D7 was tripping me up. The common case of redirecting a user to the the login form before redirecting back to the content originally attempted to be accessed is illustrated as follows.

drupal_goto('user', array('query'=>drupal_get_destination()) );

Comment about 307 is incorrect.

According to the IETF draft on the 308 status code the 307 code differs from the 302 in the sense that the HTTP method must not be changed. I fail to see the relationship with a 503. Can someone elaborate if that's not the case please? Thanks.

Login or register to post comments