PHP MySQL Login Logout Form

In this tutorial I explain how to develop PHP MySQL Login Logout form with validation using Session concept. Some developer was uploaded the code without session, that’s why here I make this article for how important to add missing methods. Actually it’s very important for take out project into next level of implementations. So session is must for pass our information from database to user side.

Previously we are published Simple Registration form using PHP & MySQL database. This is the continues for create login system for those who are registered their details. In the signup form we are collected User Name, Email Address, Mobile Number, Password and more details. Here create login page for using your existing name and password. Hereafter you can manage your personal dashboard with logout options. Without session is not possible for collect and transfer particular information from one page to another page.

php mysql login logout form

This is very simple Login and Logout form with proper validation. If we miss to type user name or password then it alert like please give your username or password to move on homepage. Every project validation is very important but it’s nice when it should be very simple and clean. Because then only end users are not struggle to understand the error messages.

Technology Used

In below I shared which programming languages and software are using to complete this project. Already we are shared in previous form however happy to share again who are miss to check past articles.

Front-EndHTML & CSS
Back-EndPHP
DatabaseMySQL
EditorSublime Text
Web ServerXAMPP

Create Project – PHP MySQL Login Logout Form

In the above screenshot I share my Login form and the code will uploaded on end of articles. Now let’s start to create simple project of Login and Logout for existing users who are create account via signup form. Suppose if you are not creating account then you can contact admin to manually add your details on database side.

Hereafter you can fetch your registered information and also able to get website details such as id, status and more. For example if you are plan to create simple e-commerce sites then you can continue with existing code of services.

Features

  1. Session
  2. Simple Navigation
  3. Easy to Use
  4. Logout
  5. Auto Timed out
  6. Validation

Moreover you can customize the code as per your own requirements. We are give importance only for development so not using so much code of CSS to design each pages.

Screenshots – PHP MySQL Login Logout

Live demo is not added here so alternatively we share output image for how this applications are executed on browser. Totally you need to create four files are after login the account into move on new home page where user stored their personal information.

php mysql login validation
php mysql logout form

Source Code

I hope above documentation and output files are helps to understand the project. And now here share every files source code which is helps to retrieve data from database. Later that apply CSS and apply decoration on your website..

File Names

  1. Login.php
  2. login_process.php
  3. homepage.php
  4. logout.php

Note : This is continue series of registration form using PHP which is published on previous article. Moreover the particular link shared from top of the second section.

Login.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Login Form</title>
</head>
<body>
	<center><br><br>
<form action="login_process.php" method="post">
	<?php if(isset($_GET['error'])) {
		echo $_GET['error'];
	}
?>
	User Name <input type="text" name="name"><br><br>
	Password <input type="password" name="password"><br><br>
	<button type="submit">Login</button>
</form>
</center>
</body>
</html>
login_process.php
<?php
include "db/config.php";
session_start(); 
if (isset($_POST['name']) && isset($_POST['password'])) {
    $name = ($_POST['name']);
    $password = ($_POST['password']);
    if (empty($name)) {
        header("Location: login.php?error=User Name is required");
        exit();
    }else if(empty($password)){
        header("Location: login.php?error=Password is required");
        exit();
    }else{
        $sql = "SELECT * FROM users WHERE name='$name' AND password='$password'";
        $result = mysqli_query($mysqli, $sql);
        if (mysqli_num_rows($result)) {
            $row = mysqli_fetch_assoc($result);
            if ($row['name'] === $name && $row['password'] === $password) {
                $_SESSION['name'] = $row['name'];
                $_SESSION['id'] = $row['id'];
                header("Location: homepage.php");
                exit();
            }
        }else{
            header("Location: login.php?error=Fill both of User Name & Password");
            exit();
        }
    }
}
homepage.php
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['name'])) {
	?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Homepage</title>
</head>
<body>
	<center><h1>Welcome <?php echo $_SESSION['name'];?> </center>
		<div style="text-align: center;"><a href="logout.php">Logout</div>
</body>
</html>

<?php
	}else {
		header("Location:login.php");
		exit();
	}
?>
logout.php
<?php
session_start();
session_unset();
header("Location:login.php");

If you are facing any issues on above section just contact us or watch our tutorial video for how successfully login the account. We are clearly step by step explain in the video section.

Leave a Reply