Creating a membership based site seems like a daunting task at first. If you ever wanted to do this by yourself, then just gave up when you started to think how you are going to put it together using your PHP skills, then this article is for you. We are going to walk you through every aspect of creating a membership based site, with a secure members area protected by password.
The whole process consists of two big parts: user registration and user authentication. In the first part, we are going to cover creation of the registration form and storing the data in a MySQL database. In the second part, we will create the login form and use it to allow users access in the secure area.
Download the code
You can download the whole source code for the registration/login system from the link below:
Configuration & Upload
The ReadMe file contains detailed instructions.
Open the source\include\membersite_config.php file in a text editor and update the configuration. (Database login, your website’s name, your email address etc).
Upload the whole directory contents. Test the register.php by submitting the form.
The registration form
In order to create a user account, we need to gather a minimal amount of information from the user. We need his name, his email address and his desired username and password. Of course, we can ask for more information at this point, but a long form is always a turn-off. So let’s limit ourselves to just those fields.Here is the registration form:
So, we have text fields for name, email and the password. Note that we are using the password widget for better usability.
At this point it is a good idea to put some form validation code in place, so we make sure that we have all the data required to create the user account. We need to check if name and email, and password are filled in and that the email is in the proper format.
To be on the safe side, we will also have the same validations on the server side too. For server side validations, we will use the PHP form validation script
Handling the form submission
Now we have to handle the form data that is submitted.
Here is the sequence (see the file fg_membersite.php in the downloaded source):
First, we validate the form submission. Then we collect and ‘sanitize’ the form submission data (always do this before sending email, saving to database etc). The form submission is then saved to the database table. We send an email to the user requesting confirmation. Then we intimate the admin that a user has registered.
Saving the data in the database
Now that we gathered all the data, we need to store it into the database.
Here is how we save the form submission to the database.
Note that you have configured the Database login details in the membersite_config.php file. Most of the cases, you can use “localhost” for database host.
After logging in, we make sure that the table is existing.(If not, the script will create the required table).
Then we make sure that the username and email are unique. If it is not unique, we return error back to the user.
The database table structure
This is the table structure. The CreateTable() function in the fg_membersite.php file creates the table. Here is the code:
The id_user field will contain the unique id of the user, and is also the primary key of the table. Notice that we allow 32 characters for the password field. We do this because, as an added security measure, we will store the password in the database encrypted using MD5. Please note that because MD5 is an one-way encryption method, we won’t be able to recover the password in case the user forgets it.
Inserting the registration to the table
Here is the code that we use to insert data into the database. We will have all our data available in the
Notice that we use PHP function md5() to encrypt the password before inserting it into the database.
Also, we make the unique confirmation code from the user’s email address.
Now that we have the registration in our database, we will send a confirmation email to the user. The user has to click a link in the confirmation email to complete the registration process.
We use the free PHPMailer script to send the email.
Note that we make the confirmation URL point to confirmreg.php?code=XXXX (where XXXX is the confirmation code).
In the confirmreg.php script, we search for this confirmation code and update the ‘confirmed’ field in the table.
After completing all these operations successfully, we send an email to the admin (configured in the membersite_config.php file)
See also:Making a login form using PHP
9th Jan 2012
Reset Password/Change Password features are added
The code is now shared at GitHub.
25th May 2011
Now you can display the logged-in user’s name with this code:
Source: Click Here