Forwarding a user to a members area

This tutorial will show you how to put a php login script on your site that will forward members logging in to a central members area. I'll be using User Session Pro, but the principle can be applied to any login script.

Using the action attribute

The simplest way to send a visitor to any page is to make use of your you login forms action attribute. Just point it to your members areas. So the start of your login form will look something like this:

<form method="post" action="http://yousite.com/members-area.php">

If you do that when someone submits the login form they will be directed to whatever address you have put in the action attribute. The down side is that if the login is unsuccessful (with a wrong password or something) the user will still end up at the address of your members area. This might not be a bad thing as presumably you have a login script on your members area too.

If the above method is not for you read on and I'll tell you how to use the php "header" function to redirect a visitor.

Using the PHP header function

This way is slightly more complicated, but does have the advantage that if login is not successfull users will remain on the page they started on.

Basically, on each page you check to see if a user is logged in, and then if they are, check to see if they have submitted the login form. If they've submitted the login form you know they are loggin in now and you have to forward them to the relevant page.

You just have to make sure that the following code is included before any output is made.

//check with User Sessoin Pro to see if a user is logged in
if ($userSessionPro->checkLogin()){
//see if they have just submitted the login form..."doLogin" is the default name for the login forms submit button
if (isset($_POST['doLogin'])){
//the user has submitted the login form, forward them to the members area
header("Location: http://yousite.com/members-area.php");
//let the user know what is going on and stop execution of the script
die('Login successful, you are being forwarded to the members area.');
}
}

So there's two methods you can use to get logging in users to automatically arrive at your members area.


If you've got any questions and/or comments about the above tutorial, please do post them here using the form below...or if you prefer email me at info@prophpscripts.com

Name:
*Email:
Comment:
 
*We wont publish your email address, but we will use it to reply to you if you like.
ProPHPScripts