x
 
Create New Page
 View Previous Changes    ( Last updated by Mark Bowen )

Redirect User Upon Logging In

What is this for?
Ever wanted to re-direct a user upon logging into the system then this is for you.

Isn’t there already the return=“site/index” parameter in the login form?
Yes there is but if you use this method you can have multiple re-direct URLs set dynamically.

N.B. This method requires the user to allow cookies on their machine.
(If cookies aren’t allowed on their machine then they should still get logged in and re-directed in the usual manner)

Method
On a page where you would like someone to log in from place the following code at the top of the template.

<?php
setcookie
("myCookie", "http://www.mysite.com/site/about", "0", "/");
?>

You will need to have PHP turned on for the template

Change the “http://www.mysite.com/site/about” value to be the full URL of the page or site that you would like the user to re-directed to upon login. The code above will save a cookie on the users machine with the URL that you would like them to be re-directed to upon log in.

Next you will need a template that uses the standard ExpressionEngine login form tag. Place a link to this login form on the previous template that had the cookie setting code and make sure that you do not have any of the standard member/login tags on that page or this method will not work if a user logs in from those.

{exp:member:login_form return="site/index"}

<p><label>Username</label><br />
<
input type="text" name="username" value="" maxlength="32" class="input" size="25" /></p>

<
p><label>Password</label><br />
<
input type="password" name="password" value="" maxlength="32" class="input" size="25" /></p>

{if auto_login}
<p><input class='checkbox' type='checkbox' name='auto_login' value='1' /> Auto-login on future visits</p>
{/if}

<p><input class='checkbox' type='checkbox' name='anon' value='1' checked='checked' /> Show my name in the online users list</p>

<
p><input type="submit" name="submit" value="Submit" /></p>

<
p><a href="{path=member/forgot_password}">Forgot your password?</a></p>

{/exp:member:login_form}

PHP will need to be turned on for this template also and will need to be set to ‘PHP Parsing Stage - Input’.

Change the top line of the code above to the following :

{exp:member:login_form return="<?php echo $_COOKIE['myCookie']; ?>"}

This will then set the return=”“ parameter of the login form tag to the value of the cookie that you set in the previous template.

To make this work you will have to use the login form and not the standard ExpressionEngine login tags which take users to member/login as there is no control over these.

Hope this helps people out.

Best wishes,

Mark

Category:Tricks
Category:Users
Category:Redirection

Categories: