A login page is used in a website to secure users' data. It is a web page that requires user identification and authentication. The login page uses the user details for checking the credentials with the data that already exists in the database. The login page works with the session system. The login page allows a user to gain access to an application by entering their username and password or by authenticating using a social media login. When a user enters their details(username and password combination) in the login form the login system creates a session using the user's email id. Some websites use cookies to track users during their logged-in sessions.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP scripts are executed on the server.
SQL stands for Structured Query Language · SQL is used to access and manipulate databases. SQL is a computer language for storing, manipulating, and retrieving data stored in a relational database.
Before working with PHP we need some software to write code.
- Xampp
- VS Code
- Web Browser(Google Chrome, Firefox, Microsoft Edge, etc.)
Firstly, we need to create a database. To create a database enter URL: http://localhost/dashboard/ in your web browser and go to PhpMyAdmin. Now create a database and create a table inside the database.
Create Database
creating a database “sampledatabase”.
CREATE DATABASE sampledatabase;
Create Table (registration_table)
--
-- Table structure for table `registration_table`
--
CREATE TABLE `registration_table` (
`id` int(4) NOT NULL,
`username` varchar(30) NOT NULL,
`useremail` varchar(60) NOT NULL,
`password` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `registration_table`
--
First, we need to create a database for saving the user's details. Before the login process, we need to upload users' data on the server using a user registration form. But here we will talk only about a login form and how a user can gain access to their account available on the website.
Create a PHP file (dbcon.php) and copy the code given below.
$con = new mysqli("localhost", "root", "", "sampledatabase");
if($con){
echo'Connected';
}
if(mysqli_connect_errno()){
echo 'failed to connect';
}
Now create an HTML page (login.html) and copy the code. And use login.php file name in action attribute.
<form action="login.php" method="post">
<div class="container">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" id="uname" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" id="psw" name="password" required>
<button type="submit" name="login">Login</button>
</div>
</form>
create a PHP file (login.php) and copy the login code.
<?php
session_start();
// press login button
if (isset($_POST['login'])) {
//email id
$email = strtolower(trim(filter_input(INPUT_POST, 'email')));
//password
$pass = strtolower(trim(filter_input(INPUT_POST, 'password')));
$email = mysqli_real_escape_string($con, $email);
$pass = mysqli_real_escape_string($con, $pass);
// query
$getquery = mysqli_query($con, "SELECT * FROM registration_table where email = '$email'");
// if data exists
if (mysqli_num_rows($getquery) > 0) {
$rows = mysqli_fetch_assoc($getquery);
// matching email
if ($rows['email'] == $email) {
// encrypted password verification
if (password_verify($pass, $rows['password'])) {
// creating session
$_SESSION['login'] = $email;
if (isset($_SESSION['login'])) {
echo 'Logged in';
}
}
}
} else {
echo 'You are not a registered user';
}
}
Now start your xampp server and run Apache and MySQL.
Save your files in a folder called htdocs inside your xampp folder in C Drive.
Open any Web Browser.
Now enter the URL: http://localhost/folder_name/login.html
Fill the login form with correct details and press the submit button.
If the details entered by the user are correct then the login.php file shows you “logged in message”
Else you will get a message “You are not a registered user”.