drupal_goto an external destination
I need to send the user to an external URL after a form submit. My approach is to set $form['destination'] via hook_form_alter. When the form is submitted, drupal_goto is called. In drupal_goto, my destination gets badly munged and the user is sent to the wrong URL. In my case, I'm trying to send the user to http://apps.facebook.com/my_application/my_path. But the user gets sent instead to http://my_domain.com/drupal/%252Fmy_application/my_path. The reason for this is at the start of drupal_goto. It looks like this: if (isset($_REQUEST['destination'])) { extract(parse_url(urldecode($_REQUEST['destination']))); } else if (isset($_REQUEST['edit']['destination'])) { extract(parse_url(urldecode($_REQUEST['edit']['destination']))); } $url = url($path, $query, $fragment, TRUE); The use of extract() makes the code nearly impossible to understand, unless you find perl intuitive, but I digress... During extract() $path, $query, and $fragment all get set. So does $scheme, $host, $user and $pass. But drupal_goto does nothing with the latter variables, and in my case important parts of the URL are dropped. Is there even a reason why parse_url is used here? Why not something closer to: if (isset($_REQUEST['destination'])) $url = url($_REQUEST['destination']) else ... And is there a workaround for me without patching core? Thanks, -Dave
On Tue, 6 May 2008 00:49:27 -0700 Dave Cohen <drupal@dave-cohen.com> wrote:
I need to send the user to an external URL after a form submit. My
return the target at the end of the _submit hook D5. fill $form_state['redirect'] in D6. You can return an array that will be fed to drupal_goto If there is just one place where you're going to redirect users unconditionally you can set it in the form definition with #redirect. -- Ivan Sergio Borgonovo http://www.webthatworks.it
That code makes it clear that external urls are simply not supported, and probably were never considered. I agree it woud be useful. Patches welcome :) On Tue, May 6, 2008 at 3:49 AM, Dave Cohen <drupal@dave-cohen.com> wrote:
I need to send the user to an external URL after a form submit. My approach is to set $form['destination'] via hook_form_alter. When the form is submitted, drupal_goto is called. In drupal_goto, my destination gets badly munged and the user is sent to the wrong URL.
In my case, I'm trying to send the user to http://apps.facebook.com/my_application/my_path. But the user gets sent instead to http://my_domain.com/drupal/%252Fmy_application/my_path.
The reason for this is at the start of drupal_goto. It looks like this:
if (isset($_REQUEST['destination'])) { extract(parse_url(urldecode($_REQUEST['destination']))); } else if (isset($_REQUEST['edit']['destination'])) { extract(parse_url(urldecode($_REQUEST['edit']['destination']))); }
$url = url($path, $query, $fragment, TRUE);
The use of extract() makes the code nearly impossible to understand, unless you find perl intuitive, but I digress...
During extract() $path, $query, and $fragment all get set. So does $scheme, $host, $user and $pass. But drupal_goto does nothing with the latter variables, and in my case important parts of the URL are dropped.
Is there even a reason why parse_url is used here? Why not something closer to:
if (isset($_REQUEST['destination'])) $url = url($_REQUEST['destination']) else ...
And is there a workaround for me without patching core?
Thanks,
-Dave
On Tue, 6 May 2008 09:56:59 -0400 "Moshe Weitzman" <weitzman@tejasa.com> wrote:
That code makes it clear that external urls are simply not supported, and probably were never considered. I agree it woud be useful. Patches welcome :)
If he can avoid to use #destination, he can still redirect to external sites even with the a $query as I explained in the previous post. If he need to POST the result to an external site wouldn't specifying an #action work? -- Ivan Sergio Borgonovo http://www.webthatworks.it
Thanks for your suggestion of using $form['#redirect']. That's working for me. I was having trouble choosing from all the options for sending the user elsewhere after submitting a form. As I understand it, the options are... 1) set $form['#action']. Useful if you are posting the data elsewhere. In my case this would have prevented validate and submit callbacks from being called. 2) set $form['#redirect']. Useful if you know in advance where to send the user after submit callbacks. 3) have your submit callback return the URL to redirect to. Useful to dynamically determine where to go next. If multiple submit callbacks, only one will be honored. Loses to $form['#redirect'] if both are specified. 4) set ?destination= in the URL, or $form['destination'] as a form element. Unlike the other methods this works only with local paths. Takes priority over options 2 and 3. This is not part of the form api, it's part of drupal_goto(). On Tuesday 06 May 2008, Ivan Sergio Borgonovo wrote:
On Tue, 6 May 2008 09:56:59 -0400
"Moshe Weitzman" <weitzman@tejasa.com> wrote:
That code makes it clear that external urls are simply not supported, ...
It does? I wanted to ask here in case this was done on purpose. I.e. to prevent a cross site scripting attack or some such.
If he can avoid to use #destination, he can still redirect to external sites even with the a $query as I explained in the previous post. If he need to POST the result to an external site wouldn't specifying an #action work?
I don't want to post to an external site. I want to redirect there after submit handlers have been called.
participants (3)
-
Dave Cohen -
Ivan Sergio Borgonovo -
Moshe Weitzman