Hello again, I am sure 90% of all the websites now a days offers Membership and in simple words the login area. The Membership PHP program totally depends upon your requirements. It can be highly complex to a very simple to use.
So, I am going to help you build a simple to use membership area. Your visitors can register and after your approval, they can visit your Membership area. To accomplish this we require:
1. MySQL database
2. Registration Page
3. Login Page
4. Your Membership folder/ page
5. Logout page
MySQL database
Lets start with step one. You need to have a database to store user login details. So lets see how the simple database would be
| Database "user" |
| id |
email |
name |
password |
status |
| 1 |
demo_amie@x-developer.com |
Amie |
amie004 |
1 |
| 2 |
demo_kathy@x-developer.com |
Kathy |
kat123 |
1 |
| 3 |
demo_kasper@x-developer.com |
Kasper |
kasi009 |
0 |
I assume you have a basic knowledge of SQL on how to create tables. Again, this is a demonstration of a simple to build Membership area. You can capture tons of information from the visitors while signing up like IP, date, phone no. etc. That is totally up to you.
Registration Page
Let go to step no. 2 creating a registration page. Let’s name the page as register.php. Find the code below:
|
|
<form method="POST" action="">
Name: <input type="text" name="name" size="20"> <br>
Email: <input type="text" name="email" size="20"> <br>
Password: <input type="password" name="password" size="20"> <br>
<input type="submit" value="Register" name="submit">
</form> |
So, the registration form is ready. Now, let’s write the php code for the registration form. I will explain the code in comments below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
/*
You need to paste this code on the top in register.php */
$auto_approve = "0"; //0 = auto approve off and 1 = ON. If you chose 1, you need to approve each user after registration.
/* First we need to write a small code which will check if the form is submitted and has all the values we require for the registration */
if(isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['password']))
{
/* Now we check if the variables are not empty */
if($_REQUEST['name'] != "" && $_REQUEST['email'] != "" && $_REQUEST['password'])
{
/*If you are creating membership area, you want to keep a unique variable among all the members. Which differentiate them while logging in. Here, we will keep the email id unique.
So we are going to check if the email already exists. */
$sql_register = "select email from user where email = '".$_REQUEST['email']."'";
$result_register = mysql_query($sql_register);
$rows_register = mysql_num_rows($result_register); //will return 0 if the email id is unique
if ($rows_register > 0 )
{
/* Now we are good to go */
$sql_insert = "INSERT INTO user(id, email, name, password, status) VALUES('','".$_REQUEST['email']."','".$_REQUEST['name']."','".$_REQUEST['password']."','".$auto_approve = "0";."')";
mysql_query($sql_insert);
$error = "Thank you for registration";
}
else
{
$error = "Email id already exists. Please use a different email id to register";
}
}
else
{
$error = "Please complete the form before submitting"; //comes when the form is submitted incomplete
}
}
echo $error; // displays the message |
And your registration page is done!
Login Page
Okay, now let us move forward to login page and membership area. Lets create a login form. And lets keep the file name as login.php
|
|
<form method="POST" action="">
<input type="text" name="email" size="20"> <br>
<input type="password" name="password" size="20"> <br>
<input type="submit" value="Submit" name="submit"> <br>
</form> |
Now let’s write the code for this page. Remember, this is login page where you need to verify the login credentials. I have added a small feature for you. You can also tell the user if his account is not approved or disabled for violation of terms and conditions etc. Let’s see how we do it.
let us say your membership area is file member.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
/* Add this code to the top of login.php */
session_start(); //read more about php sessions
if($_SESSION['id'] != "") // checking if the user is already logged in
{
header("location:member.php"); //redirecting to the member page.
}
else
{
/*Condition comes here when user is not logged in*/
/* Now we are checking if the login form is submitted */
if (isset($_REQUEST['email']) && isset($_REQUEST['password']))
{
/*Okay the form is submitted*/
/* Now let's check if the form was filled properly before submitting */
if($_REQUEST['email'] != "" && $_REQUEST['password'] != "")
{
/* We are good to go. Let us find out if the user credential are verified*/
$sql_check = "select * from user where email = '".$_REQUEST['email']."' and password = '".$_REQUEST['password']."'";
$result_check = mysql_query($sql_check);
$rows_check = mysql_num_rows($result_check); // will return 0 is not matched and 1 if matched
if ($rows_check > 0 )
{
/* Okay! So the user is valid. Now we check if the user status is unapproved, active or disabled
active = 1
unapproved = 0
disabled = 2
*/
$row_user = mysql_fetch_array($rows_check);
$user_status = $row_user['status'];
$user_id = $row_user['id'];
if ($user_status == "0")
{
$error = "Sorry! Your account is not approved. Please check back later";
}
elseif($user_status == "2")
{
$error = "Your account has been disabled. Please contact the Administrator";
}
elseif($user_status == "1")
{
/* Now our user is ready to go to your membership page.
But first we will register a session so that he doesn't gets logged out. */
$_SESSION['id'] = $user_id;
header(location:member.php); //User redirected to your member page
}
}
else
{
$error = "Sorry! The email and password do not match";
}
}
else
{
$error = "Please enter username and password both before submitting";
}
}
}
echo $error; //Displays the message |
Membership folder/ page
So, we are done with the 95% of coding so far. Now the user gets successfully logged in and enters your member’s area. Let’s do the coding for your member’s area now
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
/* Add this code to the top of all your membership pages */
session_start(); //read more about php sessions
if($_SESSION['id'] == "") // checking if the user is already logged in
{
header("location:login.php"); //redirecting to the login page because the user is not logged in
}
else
{
/***
Here comes the content for your membership page(s).
***/
} |
Alright, now your membership pages are 100% secured. Only you can decide who can visit them. Not let us move on to our last step log out.
Logout Page
If the member is inactive on your website, he automatically gets logged out in an interval of mins. But that again depends upon your hosting company. Some hosting companies allows longer session which allows the inactive logged in user to stay on your website for longer interval. And many hosting companies do not allow this for security reasons.
But we can enhance the login period irrespective of the hosting company session killing time. We can do with with the help of cookies. I will explain this in another tutorial. For now, let’s understand the basic login.
So what left now is our logout page. Let’s name it as logout.php. The code for this page is simple and sweet.
|
|
session_start();
session_destroy(); //Destroys all active sessions of a website.
header("location:login.php"); //Redirects to login page of logging out. Its standard. |
I have used php validations to make sure that the form is submitted correctly. In general we also use java script validation which is nice and in real time. I will explain that in another tutorial.
I am sure by now, you have understood this tutorial. Please note, I made this tutorial for education purpose and it is very basic and simple. If you still want to use it on your website, be my guest!!
Again, thank you for reading and supporting me. Hope to get back soon with more lessons.