6

I am having trouble properly encoding URL data. Using the following code:

$redirect = drupal_urlencode("user/register?destination=/node/1");
drupal_goto( $redirect );

but, the URL that comes up in my browser test is the following:

http://testsite.com/user/register%253Fdestination%253D/node/1

I thought using the drupal_urlencode function should fix this encoding issue. How can I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
sisko
  • 9,604
  • 20
  • 67
  • 139

3 Answers3

3

You'd be better off using the built in url() function to create your URL, if you pass an array as the query parameter it handles URL encoding for you:

$options = array(
  'absolute' => TRUE,
  'query' => array('destination' => '/node/1')
);
$redirect = url('user/register', $options);

drupal_goto( $redirect );

drupal_encode() will encode the whole string that you pass to it, so if you want to do it your original way it would look like this:

$redirect = 'user/register?' . drupal_urlencode("destination=/node/1");
drupal_goto( $redirect );     
Clive
  • 36,918
  • 8
  • 87
  • 113
  • This is not entirely correct, as [`drupal_goto()`](http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_goto/6) uses the `url()` function itself on the passed in parameters. The right way would be to pass the query parameter array as the second parameter to `drupal_goto()`. – Henrik Opel Dec 26 '11 at 10:34
2

The simplest way of doing this in Drupal 6 is:

drupal_goto("user/register","destination=/node/1");
lizbit
  • 313
  • 1
  • 12
0

The below code from Clive worked for me..

    $options = array(
  'absolute' => TRUE,
  'query' => array('destination' => '/node/1')
);
$redirect = url('user/register', $options);

drupal_goto( $redirect );
mnik
  • 41
  • 2