Dj Techblog

A blog on web development

Create a simple php login form

Last modified on on Oct 24, 2020
php login form using mysql database
A simple Login form in php is a 4 step process.

  1. Take input from user
  2. Post data to a php script for user verification
  3. Access the mysql database, and verify the user.
  4. Print message on the screen

Recoended read,

Get username and password for login

The user enters his username and password and hits on the Login button. The form action is redirected to login.php again where the login script is written in php.

Here, we have included an errors.php page which prints the error on the screen during the login process.

Login form

<form method="post" action="login.php">
  <p></p>
  <?php include('errors.php') ?>
  <p></p>
  <div class="logininput-group1">
    <label for="username">Username</label>
    <input type="text" name="username" placeholder="Enter username" value="<?php echo $username; ?>" required >
  </div>
  
  <div class="logininput-group1">
    <label for="password">Password </label>
    <input type="password"  placeholder="Enter Password" name="password" required>
  </div>
  </br>
  <div class="logininput-group1">
    <button type="submit" class="btn" name="login_user">Login</button>
  </div>
  <p></p>
  <p style="text-align: center; font-size: 15px;"><a href="password_recovery.php">Forgot password?</a> </p>
  <p></p>
  <p style="text-align: center; font-size: 15px;">
    Not yet a member? <a href="register.php">Sign up</a>
  </p>
  
  </form>

Php script access mysql database and verifies user

First, we start a php session and check whether the username session variable is set or not. If it is already set, the user is automatically redirected to the Welcome page, otherwise, the user verification process starts. We check for the username and password field is empty or not. If it is empty, errors are pushed into the array, and later displayed on the screen. If there is no error we build a database connection and make a MySQL select query to check for username match in the database. If we found the matching user is redirected to the welcome page otherwise errors are displayed on the screen.

Login script in php

<?php 
session_start();

    if(isset($_SESSION['username']))
    {
        header("location:welcome.php");
        die();
    }

$username = "";
$password = "";

$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost', 'db-user', '(db-password)', 'database-name');


// login user

if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);

if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($password)) { array_push($errors, "Password is required"); }

if (count($errors) == 0) {
  $password = md5($password);
  $query = "SELECT * FROM reg_user WHERE username='$username' AND password='$password'";
  $results = mysqli_query($db, $query);
  if (mysqli_num_rows($results) == 1) {
      
    $_SESSION['username'] = $username;
    header("location: welcome.php");
  }else {
    array_push($errors, "Wrong username./password combination");
  }
}
}


?>
Put the code snippet in a single page and name it login.php. And it should work as explained.
If you face any issue while implementing the code, you can contact me. Thanks for patient reading.

If you like this article, share with your friends. It will inspire me to write more.
Published on Oct 24, 2020

Intro

Debabratta Jena

I mainly write about HTML5, CSS3, Javascript, Angular JS, Ajax, PHP, Mysql, On page SEO, Google Ads, Tag manager, Universal Analytics, Google My Business, SERP, Apache server configuration etc, Yoga is my passion.

Reach to me in the contact us form below

Follow me on

Contact us

Subscribe

Tags

Php HTML5 Javascript CSS Ajax Angular JS Google My bisiness Listing Google tag Manager Google Universal Analytics Search Engine Optimization On page SEO Off page SEO Google Marketing platform Digital Marketing Google Ads Mysql Apache Server