12

I need to redirect user from a page with example.com/node/add/dispo?arg1=1&arg2=2 to the same type of URL. But when I simply do a drupal_goto(request_uri());, it redirects my user to example.com/node/add/dispo%25F%3C etc....

I tried a str_replace but it didnt work.

Would you have any workaround ?

32

drupal_goto() accepts an $options parameter to be passed on to the url() function. You can pass a query string like this:

$options = array('query' => array('foo' => 'bar'));
drupal_goto('path', $options);

That example will forward on to /path?foo=bar

  • I guess it's cleaner than header(location ? – Gregory Kapustin Jan 29 '13 at 13:43
  • 2
    It depends really, if you look at the code all drupal_goto() does is build up a URL based on provided parameters which it then calls header() on itself. The main differences are that drupal_exit() is called, giving a clean exit to the script, and if $_GET['destination'] is set, that will override the path that you pass to the function. Generally drupal_goto() should be used, but there are occasions when good old header() is more appropriate. Just depends what you need to do – Clive Jan 29 '13 at 13:46
  • Ok thanks. I'll use header for this one but still, I guess drupal_goto is cleaner. – Gregory Kapustin Jan 29 '13 at 13:47
  • notice that the options array has an array query with name and value pair. So my code to log in with a destination variable looks like this: $options = array('query'=>array('destination'=>'internal/dashboard') drupal_goto('user', $options)); – reptilex Nov 13 '15 at 11:11
  • Amazing got to learn something new – Rishab Sep 19 '19 at 6:33
13

This is same as Clive's answer. It's correct IMO but I just wanted to add the answer that suits your use case 1:1.

$options = array('query' => drupal_get_query_parameters());
drupal_goto(current_path(), $options);

You wanted to call drupal_goto() to the same location as the current one.

mysite.com/node/add/dispo?arg1=1&arg2=2

current_path() returns node/add/dispo; drupal_get_query_parameters() returns all $_GET parameters except $_GET['q'] which is the internal path node/add/dispo.

When you call drupal_goto(), Drupal can rebuild the same URI to send the user to.

  • Great, I upped your answer as I keep Clive's chosen. Thx you all. – Gregory Kapustin Jan 29 '13 at 14:01
0

You can also use drupal_goto($GLOBALS['base_root'] . request_uri());

Note that it is not same as drupal_goto(request_uri()); where the passed parameter is urlencoded as if it is an internal drupal path, which is actually the source of your issue.

-1

Actually I made it work by using header("location: " . $base_path . request_uri());

  • 3
    this is wrong. drupal_goto is made for some reason you know. – AyeshK Jan 29 '13 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.