How to Login System using PHP with MYSQL database

This is a simple login script using php and mysql. In this script, a form will be displayed with two fields, email and password. When user is submitting with valid email and password, then he can access authenticated page.

First we will connect to the database.

Since we already have our table created “iosr_user_detail”.

HTML Code

<form method="post" action="">
  <div class="form-group">
	<label class="sr-only" for="inputEmail">Email</label>
	<input type="email"  required="" class="form-control" id="inputEmail" name="email" placeholder="Email">
  </div>
  <div class="form-group">
	<label class="sr-only" for="inputPassword">Password</label>
	<input type="password" required="" class="form-control" id="inputPassword" name="password"
	placeholder="Password">
  </div>

	<button type="submit" name="submit" class="btn btn-primary btn-block">Sign in</button>
</form>

PHP and Mysql Code

<?php
require_once './config.php';
$error = '';
if(isset($_POST['submit']))
{
    $email = $_POST['email'];
    $password = $_POST['password'];
    $sql = mysqli_query($con,"SELECT * FROM iosr_user_detail WHERE 
                             email = '".$email."' AND password = '".$password."' ");
  
    if(mysqli_num_rows($sql ) > 0)
    {
        while ($user_result = mysqli_fetch_assoc($sql))
        {
            $_SESSION['userid']		= $user_result['user_id'];
            $_SESSION["email"]		= $user_result['email'];
            $_SESSION['is_admin']	= $user_result['user_type'];
			$_SESSION['user_pic']	= $user_result['image'];
            header('Location:dashboard.php');
        }
    }
    else
    {
        $error = 'Email and Password Wrong!!';
    }
}
?>

Leave a Reply

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