Making a login form using PHP
in PHP Form
This is in continuation of the tutorial on making a membership based web site. Please see the previous page PHP registration form for more details.
Download the code
You can download the whole source code for the registration/login system from the link below:
RegistrationForm.zip The ReadMe.txt file in the download contains detailed instructions.
The login form

Here is the HTML code for the login form.
Logging in
We verify the username and the password we received and then look up those in the database. Here is the code:
In
order to identify a user as authorized, we are going to check the
database for his combination of username/password, and if a correct
combination was entered, we set a session variable.
Here is the code to look up the username and password.
Please
notice that we must compare the value for the password from the
database with the MD5 encrypted value of the password entered by the
user. If the query returns a result, we set an "authorized" session
variable, and then redirect to the protected content. If there are no
rows with the entered data, we just redirect the user to the login form
again.
Access controlled pages
For those pages that can only be accessed by registered members, we need to put a check on the top of the page.
Notice that we are setting an "authorized" session variable in the
login code above. On top of pages we want to protect, we check for that
session variable. If user is authorized, we show him the protected
content, otherwise we direct him to the login form.
Include this sample piece of code on top of your protected pages:
See the file:
access-controlled.php in the downloaded code for an example.
Here is the CheckLogin() function code.
These
are the basics of creating a membership site. Now that you have the
basic knowledge, you can experiment with it and add new features, such
as a "Forgot password" page to allow the user to retrieve or change his
password if he forgets it.
No comments:
Post a Comment