function drupal_goto - common.inc

Public

Sends the user to a different 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: (optional) A Drupal path or a full URL, which will be passed to url() to compute the redirect for the URL.

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

$http_response_code: (optional) The HTTP status code to use for the redirection, defaults to 302. 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()

</> CopyGet raw version
php
  1. function drupal_goto($path = '', array $options = array(), $http_response_code = 302) {
  2. // A destination in $_GET always overrides the function arguments.
  3. // We do not allow absolute URLs to be passed via $_GET, as this can be an attack vector.
  4. if (isset($_GET ['destination']) && !url_is_external($_GET ['destination'])) {
  5. $destination = drupal_parse_url($_GET ['destination']);
  6. $path = $destination ['path'];
  7. $options ['query'] = $destination ['query'];
  8. $options ['fragment'] = $destination ['fragment'];
  9. }
  10.  
  11. drupal_alter('drupal_goto', $path, $options, $http_response_code);
  12.  
  13. // The 'Location' HTTP header must be absolute.
  14. $options ['absolute'] = TRUE;
  15.  
  16. $url = url($path, $options);
  17.  
  18. header('Location: ' . $url, TRUE, $http_response_code);
  19.  
  20. // The "Location" header sends a redirect status code to the HTTP daemon. In
  21. // some cases this can be wrong, so we make sure none of the code below the
  22. // drupal_goto() call gets executed upon redirection.
  23. drupal_exit($url);
  24. }

solve the query string problem

</> CopyGet raw version
php
  1. // drupal_goto() accepts an $options parameter to be passed on to the url() function. You can pass a query string like this:
  2.  
  3. $options = array('query' => array('foo' => 'bar'));
  4. drupal_goto('path', $options);
  5. // That example will forward on to /path?foo=bar