FOLLOW US ON TWITTER



FOLLOW US



img/blogimg/php.jpg


@coder 970

How to create a login page using PHP and SQL


2021-10-12 12:34 · 7 min read

what is a login page?

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. 

 

Follow the steps to create a login page using PHP AND SQL: 

What is PHP?

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.

What is SQL?

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.

 

Software requirements

Before working with PHP we need some software to write code. 

  1. Xampp
  2. VS Code
  3. 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`
--

 

How login system works

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.

 

Database connectivity using PHP and SQL(dbcon.php)

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';
}

 

How to create a login form using HTML (login.html)

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>

 

Php Code to create a login page (login.php)

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';
}
}

 

Process

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”.

 

TAGS:






POST COMMENTS
No Comment

Please login to post comment




POPULAR POSTS

best free web hosting services...
what is new in miui 11?...
free website ssl security using cloudfla...
do you want to make money online?...
CSS3 @IMPORT RULES...
What is HTML Anchor Tag and Hyperlinks?...
CSS3 RESPONSIVE UI...
How to host a web page in 10 minutes....
Tables HTML...
CSS3 FUNCTIONS...
How to use CSS3 Overflow Property?...
CSS3 FILTERS...


RECENT COMMENTS

2022-03-05 11:05
By coder on XAMPP - MySQL shutdown unexpectedly | How to fix MySQL crash unexpectedly
2021-10-12 12:34
By abnongoke on do you want to make money online?
2021-10-12 12:34
By abnongoke on how to get a free website domain name?
2021-10-12 12:34
By solar panel for shed on what is new in miui 11?
2021-10-12 12:34
By solar panel for shed on best free web hosting services
2021-10-12 12:34
By amos tanui on free website ssl security using cloudflare


SOCIAL SHARE



FOLLOW US ON TWITTER



FOLLOW US ON FACEBOOK



FOLLOW US







Recent Blogs



Contact Us

Subscribe Our News-Letter



Kashipur, 244713
Uttarakhand, India



© blog.theprodevelopers.com 2024. All Rights Reserved. Powered by Pro Developer