Ad Code

Creating a User Login And Register System with PHP and MySQL



In this tutorial we'll create a simple registration and login system using the PHP and MySQL.

Requirement:

  • If you haven't got a local web server setup you should download and install XAMPP.

File Structure And Setup:

We now need to start our web server and create the files and folders we're going to use for our login and register system.
  • Open XAMPP Control Panel
  • Next to the Apache module click Start
  • Next to the MySQL module click Start
  • Navigate to XAMPPs installation folder (C:\xampp)
  • Open the htdocs folder
  • Create the following folders and files:

    \-- loginStstem
        |-- connection.php
        |-- register.php
        |-- login.php
        |-- session.php
        |-- welcome.php
        |-- logout.php



Step 1 : Creating the Database Table

Execute the following SQL query to create the datebase and users table inside your MySQL database (XAMPP).


CREATE DATABASE  mydatabase
CREATE TABLE users (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(50) NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

Step 2 : Creating the connection.php File

Connection.php file is having information about MySQL Data base configuration

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

Step 3 : Creating the register.php File

Register PHP is having information about php script and HTML script to do register.


<?php
include("connection.php");
 if($_SERVER["REQUEST_METHOD"] == "POST") {
 
  $myusername = mysqli_real_escape_string($db,$_POST['username']);
    $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
    $myConfirmpassword = mysqli_real_escape_string($db,$_POST['confirm_password']); 
  
    $sql="SELECT * FROM users where username='$myusername'";
 $result = mysqli_query($db,$sql);
    $count = mysqli_num_rows($result);
    if($count == 0){
     if($mypassword==$myConfirmpassword){
         $sql1="INSERT INTO users (username,password) values('$myusername','$mypassword')";
            mysqli_query($db,$sql1);
            header("location: login.php");
        }
        else{
         $error = "Password not match";
        }
    }
    else{
      $error = "User Already exists Choose Another";
    }
 }
 
 ?>
 
<html lang="en">
<head>   
    <title>SignUp</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"></link>
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>

<body>
    <div class="wrapper">
        <h2>
Sign Up</h2>
<form action="" method="post">
            <div class="form-group">
                <label>Username</label>
                <input class="form-control" name="username" required type="text" />
                
            </div>
<div class="form-group">
                <label>Password</label>
                <input class="form-control" name="password" required type="password" />
                
            </div>
            <div class="form-group">
                <label>Confirm Password</label>
                <input class="form-control" name="confirm_password" required type="password" />
                
            </div>
<div class="form-group">
                <input class="btn btn-primary" type="submit" value="Submit" />
            </div>
             <p>Already have an account? <a href="login.php">Login here</a>.</p>
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</body>
</html>

Step 4: Creating the login.php File

Login PHP is having information about php script and HTML script to do login.


<?php

include("connection.php");
   session_start();
   
   if($_SERVER["REQUEST_METHOD"] == "POST") {

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
      
      $sql = "SELECT id FROM users WHERE username = '$myusername' and passwrod = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];
      
      $count = mysqli_num_rows($result);
 
      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;
         
         header("location: welcome.php");
      }else {
         $error = "Your Username or Password is Incurrent";
      }
   }
?>

<html lang="en">
<head>   
    <title>Login</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"></link>
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>

<body>
    <div class="wrapper">
        <h2>
Login</h2>
<form action="" method="post">
            <div class="form-group">
                <label>Username</label>
                <input class="form-control" name="username" type="text" />
                
            </div>
<div class="form-group">
                <label>Password</label>
                <input class="form-control" name="password" type="password" />
                
            </div>
<div class="form-group">
                <input class="btn btn-primary" type="submit" value="Submit" />
            </div>
<p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</body>
</html>

Step 5 : Creating the session.php File

Session.php will verify the session, if there is no session it will redirect to login page.


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

Step 6 : Creating the welcome.php File

After successful login, it will display welcome page.


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

Step 7 : Creating the logout.php File

Logout page is having information about how to logout from login session.


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


enjoy ;)

====================================================================================
Reactions

Post a Comment

1 Comments

  1. can you send me source code for this email address. "jeck.mic9@gmail.com"

    ReplyDelete

Close Menu