Redirecting Members with ExpressionEngine

Jun 23, 2009

Sometimes simple is better. Actually, simple is almost always better. Especially in web development. This past week, I’ve been struggling to find a method in ExpressionEngine to redirect member groups to their appropriate templates after logging in.

Here is a little background on what I needed. My client supplies confidential information to subsequent clients. It would be easy to register all their clients as member, and then restrict access to the templates so only members could see them. However, in this case, the company has a handle full of data sets, and its customers subscribe to specific sets; they aren’t allowed to see the others.

First, it is useful to name associated member groups, weblogs, and templates the same. So if we have four member groups (north, south, east, west), they have each have corresponding weblogs and templates (north, south, east, west). Also, the groups are given access to only their respective templates, which is done through the simple Template Access Restriction.

When our members first arrive at the site, they are prompted to login, so we’ll need a template group specifically for logging them in, {login/index}. This one page will handle everything we need.

<pre>{exp:member:login_form return="login/index"}

	<p><span class="userpass">Username</span><br/>
	<input type="text" name="username"  maxlength="32" class="input" /></p>

	<p><span class="userpass">Password</span><br/>
	<input type="password" name="password"  maxlength="32" class="input"  /></p>

	{if auto_login}
	<p><input class='checkbox' type='checkbox' name='auto_login' value='1' /> Remember me</p>
	{/if}

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

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

{/exp:member:login_form}

Notice how the script returns itself to the same template, {login/index}. It doesn’t really matter where it returns because the bit we are about to add redirects the members after they log in to wherever they need to go … before the {exp:member:login_form} is queried again. ExpressionEngine gives us a convenient set of global variables that aid our redirections quite nicely. They are {group_id} and {redirect="template/path"}.

{if logged_in}
	{if group_id == "1"}
    	{redirect='north/index'}
    {if:elseif group_id == "2"}
    	{redirect='south/index'}
    {if:elseif group_id == "3"}
    	{redirect='east/index'}
    {if:elseif group_id == "4"}
    	{redirect='west/index'}
    {if:else}
    	{redirect='login/restricted'}
    {/if}
{/if}

The loop determines to which member group the user belongs, and pairs template redirection with that specific {group_id}. Simply identify the id number for each member group (easily found through the CP), and that’s it! We conclude the {if} loop with an {if:else} statement that takes care of any users who sneak through the log in process.

The {if:else} statement redirects these folks (perhaps they are members, but aren’t in a member group privy to the information shielded by EE’s template access) to some other template that tells them, “you don’t belong here.” This is a nice system, because you won’t need to do any extra coding on the templates the members are redirected towards. EE can handle access to those on its own.

There are limitations, however, to setting up you member redirect in this way. First, it assumes that your member groups are rather static (they are not being created or deleted frequently). If you have a ton of member groups with a ton of corresponding template groups, this would make from a monstrous {if} statement. Another option you have is setting some PHP variables using the EE {group_id} global. To do this you would have to set the PHP parsing to ‘output’ on the {login/index} template.

blog comments powered by Disqus
Keighl

© 2010 Keighl

1.508.265.3032
info@keighl.com