PHP + MySQL Login Form with Session

PHP MySQL login system with session; In this tutorial, i am going to show you how to create simple login system or form in PHP + MySQL with session and validation.

And through this tutorial; you can download free source code of PHP + MySQL login system with session.

Simple Login Form in PHP with MySQL Database

  • Step 1 – Create PHP Project
  • Step 2 – Create Database Table And Connect App to DB
  • Step 3 – Create Login Form In HTML
  • Step 4 – Create User Profile File

Step 1 – Create PHP Project

In step 1, Navigate to your local web server directory. And inside this directory, create one directory. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Create Database Table And Connect App to DB

In step 2, you need to create database and table. So run the following sql query to create database and table:

CREATE DATABASE my_db;

CREATE TABLE users(
id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
contact varchar(255) NOT NULL,
password varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Then create a php file that named mydbCon. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create mydbCon file and add the below given code into your file:

<?php

    $hName='localhost'; // host name

    $uName='root';   // database user name

    $password='';   // database password

    $dbName = "my_db"; // database name

    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");

      if(!$dbCon){
          die('Could not Connect MySql Server:' .mysql_error());
      }
?>

Step 3 – Create Simple Login Form In HTML

In step 3, create a php file that named login.php. This file will display PHP login form and authenticate user from MySQL database. Now, you need to add the following login form code into login.php file:

<?php
session_start();

require_once "mydbCon.php";

if (isset($_SESSION['user_id']) != "") {
    header("Location: user-profile.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>login form in PHP + MySQL with Session and Validation | Laratutorials.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>

Step 4 – Create User Profile File

In step 4, create new file that named user-profile.php file. Because this is used to show loggedIn User information,

So, add the below code into your user-profile.php file:

<?php
   session_start();
   
   if(isset($_SESSION['user_id']) =="") {
       header("Location: login.php");
   }
   
   ?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>login form in PHP + MySQL with Session and Validation | Laratutorials.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

Login form in php and mysql with validation and seesion; you have have learned how to create login system or login form in PHP + MySQL database with session and validation. And as well as; You can download free source code of PHP + MySQL login page with database.

Recommended PHP Tutorials

Be First to Comment

Leave a Reply

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