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 ?

0

4 Answers 4

33

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

5
  • I guess it's cleaner than header(location ? Jan 29, 2013 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, 2013 at 13:46
  • Ok thanks. I'll use header for this one but still, I guess drupal_goto is cleaner. Jan 29, 2013 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, 2015 at 11:11
  • Amazing got to learn something new
    – Rishab
    Sep 19, 2019 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.

1
  • Great, I upped your answer as I keep Clive's chosen. Thx you all. Jan 29, 2013 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());

1
  • 3
    this is wrong. drupal_goto is made for some reason you know.
    – AKS
    Jan 29, 2013 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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