Login Form in PHP MySQL Database Source Code

Login Form in PHP MySQL Database Source Code

Simple login Page in PHP with MySQL Database; In this tutorial, you will learn how to create simple login form in PHP, MySQL, bootstrap, session and server-side validation.

And also, this tutorial will show how to create a login system in PHP and validate user credentials (email id or password) on the server side. And also, you can download and use free source code of this system.

Simple Login Form in PHP MySQL

Follow the following steps to create simple login form in PHP with MySQL database and as well as you can download the free source code of simple login form in PHP with MySQL database source code with server-side validation:

  1. Create a Database Connection File
  2. Create Simple Login Form In PHP
  3. User Profile Page PHP File

1. Create a Database Connection File

In this step, you need to create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When you need to insert form data into MySQL database, there you will include this file:

<?php
    $servername='localhost';
    $username='root';
    $password='';
    $dbname = "my_db";
    $conn=mysqli_connect($servername,$username,$password,"$dbname");
      if(!$conn){
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>

Create Simple Login Form In PHP

In this step, you need to create a form, where you accept user email id and password. So you can create a login.php file and update the below code into your file.

The below code is used to show login form and authenticate the user from MySQL database in PHP.

<?php
session_start();

require_once "db.php";

if (isset($_SESSION['user_id']) != "") {
    header("Location: dashboard.php");
}

if (isset($_POST['login'])) {
    $email    = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Please Enter Valid Email ID";
    }
    if (strlen($password) < 6) {
        $password_error = "Password must be minimum of 6 characters";
    }
    
    $result = mysqli_query($conn, "SELECT * FROM users WHERE email = '" . $email . "' and password = '" . md5($password) . "'");
    if ($row = mysqli_fetch_array($result)) {
        $_SESSION['user_id']     = $row['uid'];
        $_SESSION['user_name']   = $row['name'];
        $_SESSION['user_mobile'] = $row['mobile'];
        $_SESSION['user_email']  = $row['email'];
        header("Location: dashboard.php");
    } else {
        $error_message = "Incorrect Email or Password!!!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Simple Login Form in PHP with Validation | Tutsmake.com</title>
      <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <div class="row">
            <div class="col-lg-10">
               <div class="page-header">
                  <h2>Login Form in PHP with Validation</h2>
               </div>
               <p>Please fill all fields in the form</p>
               <span class="text-danger"><?php if (isset($error_message)) echo $error_message; ?></span>
               <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                  <div class="form-group ">
                     <label>Email</label>
                     <input type="email" name="email" class="form-control" value="" maxlength="30" required="">
                     <span class="text-danger"><?php if (isset($email_error)) echo $email_error; ?></span>
                  </div>
                  <div class="form-group">
                     <label>Password</label>
                     <input type="password" name="password" class="form-control" value="" maxlength="8" required="">
                     <span class="text-danger"><?php if (isset($password_error)) echo $password_error; ?></span>
                  </div>
                  <input type="submit" class="btn btn-primary" name="login" value="submit">
                  <br>
                  You don't have account?<a href="registration.php" class="mt-3">Click Here</a>
               </form>
            </div>
         </div>
      </div>
   </body>
</html>

3. User Profile Page PHP File

In this step, you need to create a new file name dashboard.php and update the below code into your file.

The below code used to show logged in user data.

<?php
   session_start();
   
   if(isset($_SESSION['user_id']) =="") {
       header("Location: login.php");
   }
   
   ?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>User Info Dashboard | Tutsmake.com</title>
      <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
      <div class="container">
         <div class="row">
            <div class="col-lg-8">
               <div class="card">
                  <div class="card-body">
                     <h5 class="card-title">Name :- <?php echo $_SESSION['user_name']?></h5>
                     <p class="card-text">Email :- <?php echo $_SESSION['user_email']?></p>
                     <p class="card-text">Mobile :- <?php echo $_SESSION['user_mobile']?></p>
                     <a href="logout.php" class="btn btn-primary">Logout</a>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </body>
</html>

Conclusion

In this tutorial, you have learned how to create a login system in PHP and MySQL database.

This is a very basic and easy example of creating a login form and authenticate the user to a MySQL database in PHP. In the next article, you will show you how you can create a login and registration application in PHP.

Recommended PHP Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Login Form in PHP MySQL Database Source Code

  1. Nice tutorial of Login page in PHP with database source code

Leave a Reply

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