Simple Login Form in PHP and MySQL
In this tutorial, you will get free source code to create a simple login form in PHP and MySQL. It is developed with complete validation. It will be very useful to develop the best login system. So, you can easily integrate it into your projects.
The login form is an integral part of a web application. It is used to access individual information from the websites. Many popular websites Such as Facebook, Gmail, LinkedIn, Twitter & more have login features. Even you can use it to create an admin login panel for managing the front-end content dynamically.
How to create a Simple Login Form Using PHP
The login form is created with two required fields like email & password. These fields have a backend validation to accept only valid& registered email address & password
Features –
- Using PHP, It can send login data through the POST method.
- Using MySQL, It can check registered users.
- Login form does not allow invalid data and protects from the hacking
- Login can convert illegal user input into legal input data.
- You can’t get into the dashboard until you will not log in with valid credentials.
- You can log out after login.
Before creating a login form in your project, you must have to create a signup/registration form. Because users can’t log in without signup/ register. If you don’t know to create a registration form, then there is no need to worry, you can also learn it on this website by clicking the following link.
Folder Structure
You should create the following folder structure. Otherwise, you can use the given login script directly in your project.
login-system/ |__database.php |__login-form.php |__login-script.php |__dashboard.php |__logout.php |__style.css
1. Connect PHP login Script to MySQL Database
First of all, write the following MySQL database connection script to connect PHP login System with Database.
File Name – database.php
2. Create Login Form Using HTML
Now, configure the following steps to create a login form using HTML
- Include
login-script.php
using the following script. Don’t worry it will explain it in the next step. - Include external
style.css
- Also, include the following bootstrap4 libraries to create a responsive login form.
- Write the HTML code to create a login form
File Name – login-form.php
3. Design Login Form Using CSS
Use the following custom CSS code to design the login form
File Name – style.css
4. Create a Login Script Using PHP and MySQL
Before writing a login script, you should understand the following points.
- Include database connection file using
require_once('database.php')
. - Assign a connection variable
$conn
to a new variable$db
. - Check registration data are set using
isset($_POST['submit'])
, if these are set then validate email & password usinglegal_input()
function. - If both email & password are validated successfully, call a custom function
login()
to process to login by checking valid credentials - If given credentials are already registered, it will redirect to the dashboard page.
File Name – login-script.php
5. Create Dashboard Using HTML and PHP
- To create a dashboard, you have to do the following things –
- Start session using
session_start()
- Assign session login email
$_SESSION['email']
to a new variable$email_address
- If the session login email is empty, it will redirect to the login form page. means that you can’t directly open the dashboard page without login.
File Name – dashboard.php
6. Create Logout Script Using PHP
You can quickly log out by writing the following few lines of PHP code.
- First of all, start the session using
session_start()
- Destroy session using
session_destroy()
- Redirect to the login page after logout.
File Name – logout.php
My Suggestion
Dear Developers, I hope you have learned to create a simple login form using PHP. If you have any doubts or questions related to this tutorial, you can ask me through the below comment box. I will reply as soon as possible.
Thanks For giving time to this tutorial…