The Login_form tag allows you to specify the URL you wish to redirect the user after successful logon using the return= attribute.
Standard method to redirect the user to the previous page is to omit the return= parameter, however, there are possible situations where the HTTP_REFERER variable is empty, invalid, or otherwise you would like to control messages and redirection based on different referers. Either through PHP conditional checking or assigning the value to a EE variable then using EE conditional checking, you can check for a valid URL with regex or straight string comparisons, or do complex redirections based on the result.
There are a few other methods outlined at the wiki articles Redirect after login and Redirect User Upon Logging In which I recommend you read as well to determine which method is best for you.
You must enable PHP on the login form template and set it to parse on INPUT (see Control Panel | Templates | Site | Preferences).
Down and dirty method is to set the retrun parameter to the following:
{exp:member:login_form return="<?php echo $_SERVER['HTTP_REFERER']; ?>"}
You can get more complex however, doing comparisons against that value.
For example if the referer url is blank or “http://www.mydomain.com” is not the beginning of the referring URL, you can set a default redirection, otherwise redirect to the refering URL.
<?php
$my_url = $_SERVER['HTTP_REFERER'];
$my_domain = 'www.mydomain.com';
$my_pattern = '@^http[s]?://www\.mydomain\.com.*?$@';
if ( $my_url == '' || !preg_match($my_pattern, $my_url) ) {
$my_url = 'http://' . $my_domain;
}
?>
{exp:member:login_form return="<?php echo $my_url; ?>"}
It is possible to modify the cp.login.php core template in the Redirect the user to the CP home page section so it does the same thing, however, hacking a core templates always complicates things, causes issues during upgrades and creating your own login template is highly recommended.
Additionally you can verify that the referring URL responds (using the curl library) based on the CURLINFO_HTTP_CODE or verify the host and IP with the gethostbyname() and gethostbyaddr() functions.
Category:Tricks
Category:Users
Category:Redirection
Category:URLs
Category:PHP
Category:Members
