We have already seen how to redirect users to the Login page (Section 18.2.1), to the Homepage (Section
18.6.1) and to the Forum profile from the Your Info profile (Section 18.6.2). Finally, it is time that we consider the general
redirection problem:
You are using some module, say an eCommerce module like the Emporium Shopping Cart (Section 8.3.14), and at some point you need to ask the user to
login (if not already done) to finish off the order. You would like to redirect the user to the Login screen (Figure 3-28), then bring him back
where he was to continue.
This is already implemented in PHP-Nuke at some places, like the Private Messages part of the Your Account module: in the forums for example, if you aren't logged
in, you see the link 'Log in to check your private messages', which when clicked on it, takes you to Your Account (address line becomes :
http://www.yoursite.com/modules.php?name=Your_Account&redirect=privmsg&folder=inbox), and upon login in, you get taken back to PMs.
To accomplish this from an arbitrary module, that serves as the starting point for the redirection, open that module's code and find the place where the redirection has to happen. Initiate the
redirection with the following two lines:
$redirect = $module_name;
<a href=\"modules.php?name=Your_Account&redirect=$redirect\">CLICK HERE TO LOGIN</a>
|
The value of $redirect is what you will key off in the next step. In this example we use the module name as the key that will point the way back, but you can use anything, as long as you know how
to use it later, to trace your way back with the right link.
Now go to modules/Your_Account/index.php and look for the following code in the login() function:
if ($pm_login != "") {
Header("Location: modules.php?name=Private_Messages&file=index&folder=inbox");
exit;
}
|
Add the following after it:
if ($redirect == "XXXXXXXXX"){
Header("Location: modules.php?name=YYYYYYYYY");
exit;
}
|
where XXXXXXXXX is the value of $redirect from the first step and YYYYYYYY is the name of the module you want them dumped into after login.
You might think that we are done by now, but there is a caveat: the above will work as long as they log in correctly on the first try. If they make a mistake and have to try a second or more
times, they will get dumped in Your Account, after they log in successfully, not in the module they came from. This is also true for the Private Messages example above.
To fix this, find the following code in Your_Account\index.php at the bottom of the login function.
} else {
Header("Location: modules.php?name=$module_name&stop=1");
}
|
and change it to:
} else {
Header("Location: modules.php?name=$module_name&stop=1&redirect=$redirect");
}
|
Then it will redirect even if they log in incorrectly the first time. See How to redirect.