We all know that a login form simply needs an id and password to start a user session. We are going to develop a login form in PHP and MySQL. Using PHP we will process the form and verify in the MySQL database if user credentials are correct or not. If correct, we will let user login and if incorrect, we will give message as “Invalid userid/password”.
Along with the login form we will create a simple menu where Login link will be displayed. Also, we will display user name and Logout link after successful login. So, we will code for Logout also.
Folders and Files
Below is a screenshot of folder structure and files I am using:
I have created a folder called 'login' under 'htdocs' as I am using XAMPP. If you are using WAMP, create 'login' folder under 'www'.
Folder 'cfg' - In this folder I have kept
dbconnect.php which is used to connect to the MySQL database.
Folder 'css' - My custom stylesheet is in this folder
A menu bar(
top_menu.php) will be displayed on the top and there will be three links, Home (
index.php), Signup (
signup.php) and Login (
login.php). When user clicks on any of these links corresponding page will be displayed. In this topic I will show only login form. Signup page will be just for displaying purpose, it is not the actual signup page. Top menu (
top_menu.php) will be included in each of the pages.
Step 1 - Create a MySQL table to store user details
Let us create a table named 'users' in MySQL database. This table will have user details. I have a database called 'demo'. So 'users' Table will be created in this database. If you have an existing database other than demo you can also use it. Just make sure same table name does not exist already. Table structure is given below:
Table has 5 columns.
- id - it is the primary key and auto incremented
- email - email id of the user
- name - name of the user
- password - password for the user
- signup_date - date of signup, default is current date, this is not required for login, used as information purpose only
MySQL create table script for this table is given below, you can use this code to create the table. You can also download it from download section later in this topic.
After you create the table and inserted data in it, please verify if data is inserted correctly, it should have below data in it.
Step 2 - Connect to MySQL database (dbconnect.php)
Use below script to connect to the database. Note that we have this database connection php program in 'cfg' folder. This is written once and used in every program where database connection is needed. This will be easy for maintenance and also will enable reusability of the code.
I am using mysqli_connect() function which needs 4 parameters.
- server - in our case it is localhost
- userid - we are using root user
- password - no password for user root
- database name - demo in our case.
We will include this
dbconnect.php in other php programs so that we do not need to write it again in the program. For detail database connection understanding please read topic How to connect to MySQL database in PHP using mysqli_connect.
Step 3 - Create a top menu (top_menu.php)
This is the top menu which will display the Menu items Home, Signup and Login on a menu bar. We will include this menu in all the three other pages (Home, Signup and Login) so that this menu will be visible in all pages. Note that we are not developing signup page here, it is used just to show a sample page.
I am using PHP session as it is needed to display user name and check if user is logged in. You can see I have added links corresponding to Home, Signup and Login and Logout. Once user logs in we will set session variables. So, by checking session variables, I am displaying Logout if already logged in. Similarly, if user is not logged in I should display Login and Signup. Also, I am displaying user name if user is logged in.
Step 4 - Develop a PHP Login form (login.php)
Our Login form will have email id as userid and password for the user to login to the system. So, while login we have to validate email id and password entered by the user against the values stored in the database.
Let us look at the code for
togglePwd() is called. Note that password is stored as md5 hash, so I am converting it to md5 hash before checking with database.
If user enters correct credentials login will be successful and I am assigning user name and email id in two PHP session variables and redirecting user to the home page.
There is a piece of jQuery code used to highlight Login link in the menu, that means, it is used to show the active page to the user.
addClass() function adds a class "active" for the Login link in the menu. This active class is added in my custom CSS file
That's it for our main program
Step 5 - Add CSS (style.css)
Let us add below styles. We have already added
You can see simple styles are added here. Keep this
style.css file in your css folder. One point you must remember, since we added "active" class in our pages using jQuery, we must add properties for this class in our stylesheet. So, in
style.css, properties for "active" class are added.
You have already seen
togglePwd() function is called from login form when checkbox - "Show Password" is clicked. So, here is the code for it. Keep
validation.js file in js folder.
You can see
togglePwd() function is written here. It is used to show/hide password.
In this function, type of input object - password is changed from "text" to "password" and vice versa.
Now, I will just add two simple pages for Home and Signup. Remember that these are not actual home or signup pages, just for navigation purpose. See below codes for them
All I am doing here is displaying some messages so that you can confirm that you have landed in correct pages during navigation from the menu. Also note the same jQuery added for active pages.
Lastly, I want to show
logout.php as below:
Here, I am unsetting all session variables by using
session_unset() function. Then deleting session using
Our development is complete, let us test the application now.
Test the Application
Make sure your XAMPP control panel is running with services running. Open the browser and run localhost/login. You will see the home page displayed. See the below screen, you can see Home link in the menu is selected and highlighted:
Click on Login, Login Page will be opened and highlighted in the menubar. This is just to check our menu is working fine.
Test if Login form is working correctly.