7 common.inc | drupal_goto($path = '', array |
4.6 common.inc | drupal_goto($path = '', $query = NULL, $fragment = NULL) |
4.7 common.inc | drupal_goto($path = '', $query = NULL, $fragment = NULL) |
5 common.inc | drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302) |
6 common.inc | drupal_goto($path = '', |
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: (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
url()
Related topics
- aggregator_admin_refresh_feed in modules/
aggregator/ aggregator.admin.inc - Page callback: Refreshes a feed, then redirects to the overview page.
- comment_approve in modules/
comment/ comment.pages.inc - Menu callback; publish specified comment.
- comment_multiple_delete_confirm in modules/
comment/ comment.admin.inc - List the selected comments and verify that the admin wants to delete them.
- comment_reply in modules/
comment/ comment.pages.inc - This function is responsible for generating a comment reply form. There are several cases that have to be handled, including:
- common_test_drupal_goto_redirect in modules/
simpletest/ tests/ common_test.module - Redirect using drupal_goto().
- batch_process in includes/
form.inc - Processes the batch.
- common_test_menu in modules/
simpletest/ tests/ common_test.module - Implements hook_menu().
- DrupalGotoTest::testDrupalGoto in modules/
simpletest/ tests/ common.test - Test drupal_goto().
- drupal_redirect_form in includes/
form.inc - Redirects the user to a URL after a form has been processed.
- update_batch in includes/
update.inc - Starts the database update batch process.
File
- includes/
common.inc, line 686 - 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()) );
drupal_goto in settings.php file fails
Note that drupal_goto() seems to fail when called from within settings.php. The work around in that case, is to use plain old php code like:
<?php
header("Location: $my_url");
exit();
?>
Intentional, however.
This is intentional. settings.php is before the bootstrap of Drupal. Drupal API functions are not typically available in settings.php. Why would one want to redirect a user from settings.php? Curious about the use case.
One use case: redirect from domain.com to www.domain.com
I recently did exactly this to do a redirect to add www in a shared hosting environment. I did not have access to the webserver configuration file, and did not want to customize Drupal's .htaccess file. I googled a couple of things to consider options, and ended up doing it in settings.php as described above.
include hash (#) in url
for example
drupal_goto("products", array('fragment' => $node->nid));
this will result prducts#nid
Setting the path from a calculated variable
I've banged my head against this for hours - I'm trying to use drupal_goto within a script like this:
drupal_goto($my_calculated_path, array $options = array(), $http_response_code = 302);
but always getting the error "Parse error: syntax error, unexpected T_VARIABLE, expecting '(' "
It's surely possible to define the path programatically? Any ideas what I'm doing wrong?
What @texas-bronius said. In
What @texas-bronius said.
In addition... in PHP a function definition can specify default values, so that if the variables are not given, they still have a value.
So using drupal_goto as an example:
<?php
function drupal_goto($my_calculated_path, array $options = array(), $http_response_code = 302) {
//... etc etc
}
?>
If a programmer like you simply writes the following:
<?php
drupal_goto($my_calculated_path);
?>
The function definition says that inside the function drupal_goto, $options will be set to array() and $http_response_code will be set to 302, even though you haven't given them values. This saves time because we don't have to specify them when we don't need them.
Good luck
probably not the place for support issues, but
@gittosj: If what you copy/pasted is literally what you're doing:
<?php
drupal_goto($my_calculated_path, array $options = array(), $http_response_code = 302);
?>
should be just:
<?php
drupal_goto($my_calculated_path, array(), 302);
?>
Seems this is more a php syntax familiarity issue, and you'll get better traction in a generic Drupal IRC channel.