Login and Registration system in php

Login and Registration system in php

Login and registration system is very important part of any website which protect website from hacker and many other attacks. In this tutorial we will see how to make a login and registration system using php, mysqli and boostrap. Here we use bootstrap for designing. We will use seasion to store login detail.

In this tutorial we will also see how to change session data after every time page refresh by using session_regenrate_id(true). It is very important because it will protect our website from attackers.

Login and Registration system in php

This tutorial follow some step which are given bellow

  • Step 1: Downloading Boostrap

  • Step 2: Creating database

  • Step 3: Creating table

  • Step 4: Database Connection

  • Step 5: Creating Views files

      • config.php

      • Index.php

      • login.php

      • wellcome.php

      • logout.php

  • Step 6: Testing project

 

Step 1 – Downloading Boostrap

In this step we need boostrap css and bootstrap js Click to Downlad and extract in project folder.

 

Step 2 – Creating Database

First of all start Xampp/Wampp and open http://localhost/phpmyadmin/ and create a database with the name of test.

Step 3 – Creating Table

After creating a database create a table by using given code or manually with the name of userpass:

CREATE TABLE `userpass` (
  `id` int(10) UNSIGNED NOT NULL,
  `user` varchar(200) NOT NULL,
  `pass` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `userpass`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `userpass`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
COMMIT;

Step 4: Database Connection

After creating table create folder in Xampp/htdocs/LoginSystem with the name of LoginSystem and create a file with name of config for database connection copy the following code and paste in it:

Config.php

<?php
//DB Connection
 $conn = new mysqli('localhost', 'root', '') or die(mysqli_error());
 //Select DB From database
 $db = mysqli_select_db($conn, 'test') or die("databse error");
?>

This config file we will use in all our other files for database connection will include this file.

Step 5 - Creating Views files

After that create a file with the name of index.php and paste the following code in it:

Index.php

 
<!doctype html>
<html>
<head>
<title>User Registration || The Skill Stock</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<h1 class="text-light bg-dark bt-3 pb-3 text-center">User Registration</h1>

    <div class="container">
      <div class="row">
        <div class="col-md-3"></div>
         <div class="bg-success rounded pt-3 text-light col-md-6 pb-4">
             <!-- Login Link -->
               <center><a href="login.php" class="btn btn-primary justify_content_center">Login</a></center>

           <form action="" method="post" class="pt-4">
            <div class="form-group">
             <label for="username">Enter Your Username</label>
             <input type="text" name="user" required class="form-control rounded">
           </div>
             <div class="form-group">
              <label for="password">Enter Your Password</label>
              <input type="password" name="pass" required class="form-control rounded ">
             </div>
            <center><input type="submit" value="Register" name="submit" class="btn btn-primary"></center>
       </form>
     </div>
    <div class="col-md-3"></div>
 </div>
</div>

<?php
if(isset($_POST["submit"])){
 if(!empty($_POST['user']) && !empty($_POST['pass'])){
 $user = $_POST['user'];
 $pass = $_POST['pass'];
include "config.php";
 //Selecting Database
 $query = mysqli_query($conn, "SELECT * FROM userpass WHERE user='".$user."'");
 $numrows = mysqli_num_rows($query);
 if($numrows == 0)
 {
 //Insert to Mysqli Query
 $sql = "INSERT INTO userpass(user,pass) VALUES('$user','$pass')";
 $result = mysqli_query($conn, $sql);
 //Result Message
 if($result){
 echo "Your Account Created Successfully";
 }
 else
 {
 echo "Failure!";
 }
 }
 else
 {
 echo "That Username already exists! Please try again.";
 }
 }
 else
 {
 ?>
 <!--Javascript Alert -->
 <script>alert('Required all felds');</script>
 <?php
 }
}
?>
<script type="text/javascript" src="js/bootstrap.js"></script>
</body>
</html>
 

Then create

login.php

<!doctype html>
<html>
<head>
<title>User Login || Muhammad Zubair</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<h1 class="text-light bg-dark bt-3 pb-3 text-center">User Login</h1>

    <div class="container">
      <div class="row">
        <div class="col-md-3"></div>
         <div class="bg-success rounded pt-3 text-light col-md-6 pb-4">
             <!-- Login Link -->
               <center>if you are new <a href="index.php" class="btn btn-primary justify_content_center">Register</a></center>

           <form action="" method="post" class="pt-4">
            <div class="form-group">
             <label for="username">Enter Your Username</label>
             <input type="text" name="user" required class="form-control rounded">
           </div>
             <div class="form-group">
              <label for="password">Enter Your Password</label>
              <input type="password" name="pass" required class="form-control rounded ">
             </div>
            <center><input type="submit" value="Login" name="submit" class="btn btn-primary"></center>
       </form>
     </div>
    <div class="col-md-3"></div>
 </div>
</div>


<?php
if(isset($_POST["submit"])){
 if(!empty($_POST['user']) && !empty($_POST['pass'])){
 $user = $_POST['user'];
 $pass = $_POST['pass'];
 include"config.php";
 //Selecting database
 $query = mysqli_query($conn, "SELECT * FROM userpass WHERE user='".$user."' AND pass='".$pass."'");
 $numrows = mysqli_num_rows($query);
 if($numrows !=0)
 {
 while($row = mysqli_fetch_assoc($query))
 {
 $dbusername=$row['user'];
 $dbpassword=$row['pass'];
 }
 if($user == $dbusername && $pass == $dbpassword)
 {
 session_start();
 $_SESSION['sess_user']=$user;
 //Redirect Browser
 header("Location:welcome.php");
 }
 }
 else
 {
 echo "Invalid Username or Password!";
 }
 }
 else
 {
 echo "Required All fields!";
 }
}
?>
<script type="text/javascript" src="js/bootstrap.js"></script>
</body>
</html>

then create

wellcome.php

<?php
include"config.php";
session_start();
if(!isset($_SESSION["sess_user"])){
 header("Location: login.php");
}
else
{
?>

<?php
//Fetching user login data
$username = $_SESSION['sess_user'];
$select = "SELECT * FROM userpass WHERE user='$username'";
$querry = mysqli_query($conn,$select);
while ($show = mysqli_fetch_assoc($querry)) {
    $id = $show['id'];
    $name = $show['user'];
    $password = $show['pass'];
}

?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to The Skill Stock</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<h1 class="pt-4 pb-4 bg-dark text-white text-center">Welcome back <?=$_SESSION['sess_user'];?>! to The Skill Stock </h1>
<center><a href="logout.php" class="btn btn-danger">Logout</a></center>

 <div class="container pt-4">
     <div class="row bg-success p-5 text-white rounded">
       <div class="col-md-12 ">
         <table class="justify-content-center"  width="100%">
             <thead class="text-center">
                 <th>Id</th>
                 <th>Name</th>
                 <th>Password</th>
             </thead>
             <tbody class="text-center">
                 <tr>
                     <td><?php echo $id;  ?></td>
                     <td><?php echo $name;  ?></td>
                     <td><?php echo $password;  ?></td>
                 </tr>
             </tbody>
         </table>
       </div>
     </div>
 </div>
<script type="text/javascript" src="js/bootstrap.js"></script>
</body>
</html>
<?php
}
?>

Then create
logout.php
<?php
session_start();
unset($_SESSION['sess_user']);
session_destroy();
header("Location: login.php");
?>

Step 6: Testing project

 

For project testing click Here to run project.

 

Thank You for Reading tutorial!

You may also CRUD System app in Laravel 9

 

 

Comments

No Comment posted Yet!

Leave a Reply

OK! You can skip this field.