User Login in PHP With Session & MySQL PDF
User Login in PHP With Session & MySQL PDF
& MySQL
by Pawan
Table of Contents
● Introduction
● Forging a clean UI with Bootstrap for User Login
● Build and Hook up your MySQL database
● Fun Logic of PHP User Login System
● Conclusion
Introduction
User login functionality in a website is a pretty simple but compulsory
component. In all modern dynamic websites, the client asks for a login and
admin panel so that they can do basic tasks.
Today I will give you a simple and clean-looking login system with PHP and
MySQL. We will maintain the login tracking with PHP Sessions and let
Bootstrap make us a clean UI Design.
Around 81.7% of the websites on the web use PHP as their server-side
language.
index.php file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link
href="https://fonts.googleapis.com/css2?family=Poppins&display=swa
p" rel="stylesheet">
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootst
rap.min.css" rel="stylesheet">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<div class="card-body">
<div class="mb-3">
</div>
<div class="mb-3">
<label for="password"
class="form-label">Password</label>
</form>
</div>
</div>
</div>
<!-- JS -->
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstra
p.bundle.min.js"></script>
</body>
</html>
Next, we do simple CSS styling in our style.css file. To make our plain
background something fun to look at, I have added a small background image
in webp format. Also, coded in background color as a backup in the case of
legacy browsers that do not support webp format yet.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
body {
background: #f1f1f1;
background: url("../img/email-pattern.webp");
After the Database is created. Setup a table named users which has mainly
four columns we need: id, email, password, and active. We can also execute
the below SQL code in the PHPMyAdmin SQL tab and this will generate our
table “users”.
Now, we can insert user login details by running the following SQL code. We
are hard coding this now. But we can create a PHP code for the signup
process later. First, we must deploy our user login system.
connection.php
<?php
if ($conn->connect_error) {
Now our UI and database are set. Next, we tackle our User Login process
head-on! Yay! I am excited!
But breaking it down into small chunks and bits of pieces is more useful. Let’s
understand with current logic.
We need to determine the login process. But before you write any code. Let’s
write pseudo-code.
Good question! It’s not actual code. When we write small pieces of notes we
as coders for simplifying the complex logic processes — we call them
pseudo-code.
Sure, our login process is pretty straightforward. And we could code it without
effort.
But if you get in habit of writing pseudo-code before writing a single line of
actual code. You will fall in love with coding. And even page-long logic will
seem like child-play for you.
Avoid creating too many pieces when writing pseudo code and vice versa
don’t make too few of them.
<?php
require_once("connection.php");
session_start();
function santize($data)
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
if (isset($_POST['login'])) {
$email = santize($_POST['email']);
$password = santize($_POST['password']);
if (mysqli_num_rows($result) > 0) {
header("Location: admin.php");
exit();
} else {
header("Location: index.php");
exit();
?>
Before we go to the admin part. Add this snippet block for showing user login
errors. This is to implement a temporary PHP session. Paste this code in a
place where you want to show errors. Mind UI though.
<?php
$message = $_SESSION['errors'];
unset($_SESSION['errors']);
echo $message;
?>
</div>
admin.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootst
rap.min.css" rel="stylesheet">
<title>Admin</title>
</head>
<body>
<span class="navbar-toggler-icon"></span>
</button>
<div class="d-flex">
</div>
</div>
</div>
</nav>
<div class="container">
<h1>Welcome to Admin</h1>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstra
p.bundle.min.js"></script>
</body>
</html>
Now we can insert PHP code at top of admin.php which checks whether you
are login session is active. Without this everyone will be able to access your
admin file. So do put this code above your admin.php Html code.
<?php
require_once("connection.php");
session_start();
if (!isset($_SESSION['login_active'])) {
header("Location: index.php");
exit();
}
?>
Now we are pretty much ready to deploy our user login functionality at any
server or localhost.
Conclusion
Now that we have the seen whole process of building a user login system with
PHP, MySQL, and Bootstrap 5. You can just copy the code and deploy it as
needed. Below you download the whole code from my GitHub repository. It
also includes the database file of SQL– so don’t forget to import it if using that
code.
And if you are looking for an amazing free code editor. Check out our Why VS
code in the 2022 post.
Lastly if, you think our code helped you solve a need or problem then do
comment and follow us on GitHub and social media. Have fun guys!