2011-12-24 186 views
5

我无法正确编码URL数据。使用下面的代码:Drupal网址编码

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

,但是,在我的浏览器测试出现的网址是:

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

我想到了用drupal_urlencode功能应该可以解决这个问题的编码。 任何人都可以建议一种方法来解决这个问题吗?

回答

3

你会更好使用内置的url()函数来创建你的网址,如果你传递一个数组作为query参数它处理URL编码您:

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

drupal_goto($redirect); 

drupal_encode()将整个字符串编码您传递给它,所以如果你想这样做,你原来的方式,它应该是这样的:

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

这并不完全正确的,因为['drupal_goto()'](http://api.drupal.org/api/drupal/包括 - common.inc/function/drupal_goto/6)在传入的参数中使用'url()'函数本身。正确的方法是将查询参数数组作为第二个参数传递给'drupal_goto()'。 – 2011-12-26 10:34:53

2

在Drupal 6这样做的最简单的方法是:

drupal_goto("user/register","destination=/node/1"); 
0

从克里夫下面的代码为我工作..

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

drupal_goto($redirect);