PHP & MYSQL : Login form with Session

HTML & CSS

PHP & MYSQL :Learn how to create Login form with Session

To create database and table execute the following code to Mysql.

SQL FILE: account_db

CREATE DATABASE account_db;
CREATE TABLE user_tbl(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)

Given code below can create connection of PHP to Database.

PHP FILE: config.php

<?php
$server="localhost";
$username="root";
$password="";
$db= "account_db";
$conn = mysqli_connect($server, $username, $password, $db);
if(!$conn){
die("connection failed!". mysqli_connect_error);
}
?>

The following code includes ‘login.php ‘ and HTML Login form.

PHP FILE : index.php

<?php
include('login.php');
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>login</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="bootstrap/css/login.css">
</head>
<body>
    <div class="login-card"><img src="assets/img/avatar_2x.png" class="profile-img-card">
        <p class="profile-name-card"> </p>
        <form class="form-signin" method="POST"><span class="reauth-email"> </span>
            <input class="form-control" type="text" name="username" required="" placeholder="Username" >
            <input class="form-control" type="password" name="password" required="" placeholder="Password">
            <div class="checkbox"></div>
            <button class="btn btn-primary btn-block btn-lg btn-signin" name="submit" type="submit">Sign in</button>
        </form>
    </div>
</body>
</html>

The following code includes login script in which PHP session starts.

It will redirect to ‘welcome.php’ if the username and password matched from the database and html form.

PHP FILE : login.php

<?php
include('config.php');
if(isset($_POST['submit'])){
$username= $_POST['username'];
$password= $_POST['password'];
$sql = mysqli_query($conn,"SELECT * FROM user_tbl WHERE username='$username' AND password='$password'");
$row = mysqli_fetch_assoc($sql);
$id = $row['id'];
$user = $row['username'];
$pass = $row['password'];
if($username==$user && $password==$pass){
session_start();
$_SESSION['id']= $id;
$_SESSION['username']= $user;
$_SESSION['password']= $pass;
?>
<script>window.location.href='welcome.php'</script>
<?php
}else{
echo "Invalid Username and Password";
}
}
?>

The following code includes connection of database and session, it will redirect to ‘index.php’ if the username and password is not set. 

PHP FILE : welcome.php 



Execute the following code to destroy all sessions. After the session destroyed it will redirect to ‘index.php’

PHP FILE : logout.php 

<?php
session_start();
if(session_destroy()) {
header("Location: login.php");
}
?>

Comment down below if you have some questions 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *