testdevs

How To Create Login System Using PHP with MySQL Database FREE

Most of the coders especially to a beginner the main thing when developing a web application is to start planning how the system works, what is the purpose, create visual design, plan the database for, and many more, and the next thing to do is, of course, the important part of building a web application is the LOGIN SYSTEM and REGISTRATION so in this tutorial we will show you how to create a login code using PHP with MYSQL database using mysqli method.]

**NOTE: To copy the code, please highlight the code snippet provided on this tutorial right click and copy.

DB-CONFIG.PHP

Create a file and name it as db-config.php this will be useful because we are going to communicate on the database.


<?php
   define('DB_SERVER', 'localhost:3036');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'rootpassword');
   define('DB_DATABASE', 'database');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>


LOGIN-FORM.PHP

Now create a login-form.php and copy the code provided, this will be the place where user will input their username and password.


<?php
   include("db-config.php");
   session_start();
   
   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 
      
      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
      
      $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];
      
      $count = mysqli_num_rows($result);
      
      // If result matched $myusername and $mypassword, table row must be 1 row
  
      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;
         
         header("location: dashboard.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
<html>
   
   <head>
      <title>Login Page</title>
      
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         .box {
            border:#666666 solid 1px;
         }
      </style>
      
   </head>
   
   <body bgcolor = "#FFFFFF">
 
      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
    
            <div style = "margin:30px">
               
               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>
               
               <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
     
            </div>
    
         </div>
   
      </div>

   </body>
</html>


DASHBOARD.PHP

This is the place where a user will be redirected once successfully log in, so once they are here you already know that the login is working, take note that on this tutorial we use sessions.


<?php
   include('session.php');
?>
<html">
   
   <head>
      <title>Welcome </title>
   </head>
   
   <body>
      <h1>Welcome <?php echo $login_session; ?></h1> 
      <h2><a href = "logout.php">Sign Out</a></h2>
   </body>
   
</html>


LOGOUT.PHP

We will now create logout function to destroy session and exit user from dashboard if they want to log out.


<?php
   session_start();
   
   if(session_destroy()) {
      header("Location: login-form.php");
   }
?>


SESSION.PHP

If you are not familiar with a session, you can google it or watch videos on YouTube on how session important when it comes to login function.


<?php
   include('db-config.php');
   session_start();
   
   $user_check = $_SESSION['login_user'];
   
   $ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");
   
   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
   
   $login_session = $row['username'];
   
   if(!isset($_SESSION['login_user'])){
      header("location:login-form.php");
   }
?>


And that's it, you now successfully create a Login system with session and logout function, if you have any questions regarding this tutorial please leave a comment below.

Post a Comment

Previous Post Next Post

We permanently move to KAZUKIYAN.com Visit Us

X