xaraya
(34,325 views)

A general return redirect mechanism

On a site, I wanted users to register and then continue a little workflow through several extra screens (sort of a registration wizard). But the registration process doesnt provide a return_url at the end. Here's a workaround, which is generalized for any functions that don't already support return_url's

My registration wizard lets users register and continue along a registration process. I don't require they validate via email, so once registered they're logged in. That way the next steps they can create items (such as articles) as required by my application.

However, the registration module's xaruser/register.php does not offer a return_url for the end of the registation process. Rather it redirects to the home page of the site.

Perhaps I could have added return_url support to the code but since there are multiple phases and multiple templates it started to look complicated. Instead I let register return me to the home page, and from there redirect to where i really want to end up.

My solution is generalized. It uses a session variable to hold the redirection information, set in an initiating template. Then any potential 'landing' page (where the you were sent by default) includes code the check for redirection and redirect from there.

session variable:
$session_redirect = array(
'referer' => $referer_url,
'landing' => $landing_url,
'redirect' => $redirect_url );

For example:

Array
(
[referer] => http://localhost/alumbiz/registration/register
[landing] => http://localhost/alumbiz/
[redirect] => http://localhost/alumbiz/signup/schools
)

 

1. Initialize the session_redirects in a template

For example, in the user-registerform.xd, saved as mytheme/modules/registrations/user-registerform.xt, contains the following near the top:

<xar:comment> workaround for no return_url in registration </xar:comment>
<xar:set name="foo">xarSessionSetVar( 'session_redirect',
array(
'referer' => xarServerGetCurrentURL(),
'landing'=>xarServerGetBaseURL(),
'redirect'=>xarModUrl('xarpages', 'user', 'display', array('pid'=>'34'))
))</xar:set>

When registration is done, we know it sends us to the home page (xarServerGetBaseURL()), coming from the register page. But we want to go to the xarpages pid 34 instead.

2. Redirect in the landing page

You could do this on a per page basis, or in the pages/default.xt template. I chose to do it in an xarpages custom function, and just ensure this function is in the config of the page. (In my case the home page is an xarpage so I know the function will get called with you go to web root).

The php code looks like

// workaround for missing return_urls (used in user-registerform.xt)
$session_redirect = xarSessionGetVar( 'session_redirect' );
if (!empty($session_redirect) &&
($session_redirect['landing'] == xarServerGetCurrentURL()) && //consider stripping get vars (here and where initialized)
($session_redirect['referer'] == $_SERVER['HTTP_REFERER']) ) //consider a php solution, HTTP_REFERER var "isnt reliable"
{
$redir = $session_redirect['redirect'];
xarSessionDelVar( 'session_redirect' );
xarResponseRedirect( $redir );
}

Basically I get the session var, check if we're on the specified landing page, and check that we came from the specified referer page. If so, we delete the session var (I assume it's only used once), and then do the redirect.

If you are putting this in a template, I'll leave the translation to BL tags an exercise for the reader...

3. Making it HTTP_REFERER Safe

Note, I use $_SERVER['HTTP_REFERER'] to see where we came from. This is said to not always be reliable (it is set by the browser, not the server). So I implemented a php substitute for this variable, using session variables.

We save the current and previous urls in a session var; and do this for every page on the site e.g. from pages/default.xt , as follows:

<xar:comment> save referer url (make this a php function...)</xar:comment>
<xar:set name="foo">1;
$session_referer = xarSessionGetVar('session_referer');
if (!empty($session_referer['current']))
$session_referer['referer'] = $session_referer['current'];
$session_referer['current'] = xarServerGetCurrentURL();
xarSessionSetVar('session_referer', $session_referer)
</xar:set>

 

Then the redirection code above changes to:

// workaround for missing return_urls (used in user-registerform.xt)
$session_redirect = xarSessionGetVar( 'session_redirect' );
$session_referer = xarSessionGetVar( 'session_referer' );
if (!empty($session_redirect) && !empty($session_referer) &&
($session_redirect['landing'] == xarServerGetCurrentURL()) &&
($session_redirect['referer'] == $session_referer['referer']) )
{
$redir = $session_redirect['redirect'];
xarSessionDelVar( 'session_redirect' );
xarResponseRedirect( $redir );
}

 

Note: I just noticed this considers a browser refesh a new page and thus the current url also becomes the referer.... whatever.

 

Comments

by Autocrat on May 18, 2007

>>

regarding this.... firstly, I've considered something similar (in <xar> tags only), so that I can add an inline "back" link to pages.

secondly, to solve the problem of the refresh, would adding an extra holding var and an additional url check not solve the issue?

I mean, if you are taking the previous url... passing it to "second previous URL". As this is done, check to see if CurrentURL = PreviousURL etc... this means that you can tell it to ignore the change, and keep the prior url ???

by sezer on Jun 24, 2008

>>

About Redirect to other web address // html code --

http://html-lesson.blogspot.com/2008/06/redirect-to-web-addres.html

New Comment

markdown formatting permitted