PHPWeb Development Projects

Login System using PHP with Mysql 2022 | Authentication in PHP.

Dear friends in this article we will tell you How to create Login System using PHP with Mysql 2022. So i hope you like our article because here you can get the source code free and this code you can use in your projects. Guys if you learn more interesting articles then go to the search bar and type article name then you can find easily more interesting articles.

Project Overview

1. Create Registration Page

2. Create Login Page

3. Create Welcome Page

Folder Structure

1. Create Database with 4 Table Fields

2. Create config.php file for make Databse Connection

3. Create registration.php file

4. Create login.php file

5. Create welcome.php file

6. Create logout.php

7. Create style.css file for Design the Pages

Queries

1. How to create portfolio website with dark mode enable or disable using Javascript?

2. How to make crud operation using php?

3. How to design tea cup with vapour effects using css?

4. How to create Neumorphism Clock using Javascript?

5. How to make traffic lights using html css and javascript?

Create Database

Friends, we will know how to create a database, first of all we have to open localhost / phpmyadmin, then in the left side you will get the option to create a new database, click on it, after that you will get the option to create a table by giving the name of the table. And keep its fields 4 and click on go. After that you have to name the first field as id and the name of the second field as name. Then the name of the third field is to be named email and the name of the fourth field is to be named password. Then click on save. This is for create the login system using php.

Create Config File for Database Connectivity

First of all, we have to take any one code editor, then we have to go to the local host, after that we have to go to the htdocs folder, by going there we have to create a new folder, which is to be named login system. After that we have to open this folder in our code editor, then inside this folder we have to create config.php file. After that we have to copy the code given below and paste it in the config.php file then you database connection is connect with login system using php project.

Config File Code.

<?php
$conn = mysqli_connect("localhost","root","","authdatabase");

if(!$conn){
    echo "database not connected";
}

?>

Create Registration Page

Now we will create the register.php file, then we will create a form in it, after that we will design it something like this.

register.php

//For PHP Code paste on the top
<?php
include('config.php');
$alert1 = false;

if (isset($_POST['register'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    //This code is for email validation
    $getEmail = "SELECT * FROM `authtable` WHERE email='$email'";
    $runQ = mysqli_query($conn, $getEmail);
    $findEmail = mysqli_fetch_array($runQ);
    $uEmail = $findEmail['email'];
    

    if (!$uEmail == $email) {
        $insertData = "INSERT INTO `authtable` (`name`,`email`,`password`)VALUES('$name','$email','$password')";

        $runQuery = mysqli_query($conn, $insertData);
        if ($runQuery) {
            echo '<script>alert("User has been Registered Successfully")</script>';
            echo '<script>window.location.href="login.php"</script>';
        } else {
            echo '<script>alert("User not Registered")</script>';
        }
    } else {
        $alert1 = true;
    }
}
?>

//For HTML Code..
<div class="register">
        <form method="POST">
            <h3>Register Now</h3>
            <?php
            if($alert1 == true){
                echo '<span>This email already exist <a href="login.php">Login</a></span><br><br>';
            }
            ?>
            <input type="text" name="name" placeholder="Name" autocomplete="off"><br><br>
            <input type="email" name="email" placeholder="Email" autocomplete="off"><br><br>
            <input type="password" name="password" placeholder="Password" autocomplete="off"><br><br>
            <button type="submit" name="register">Submit</button>
        </form>
    </div>

Create Login Page

Now we will create the login.php file, then we will create a form in it, after that we will design it something like this.

login.php

<?php
include('config.php');
session_start();

$alert1 = false;
if (isset($_POST['login'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

   //find email and password for login page
   $allData = "SELECT * FROM `authtable` WHERE email='$email' && password='$password'";
   $runQ = mysqli_query($conn,$allData);

   $row = mysqli_fetch_array($runQ);
   $myName = $row['name'];
   if($row){
    echo '<script>alert("Welcome my Dashboard")</script>';
    echo '<script>window.location.href="welcome.php"</script>';
    $_SESSION['username'] = $myName;
   }else{
    $alert1 = true;
   }
    
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Login Account</title>
</head>

<body>
    <div class="register">
        <form method="POST">
            <h3>Login Account</h3>
            <?php
            if($alert1 == true){
                echo '<span>Not registered <a href="register.php">Register Now</a></span><br><br>';
            }
            ?>
            <input type="email" name="email" placeholder="Email" autocomplete="off"><br><br>
            <input type="password" name="password" placeholder="Password" autocomplete="off"><br><br>
            <button type="submit" name="login">Login</button>
        </form>
    </div>
</body>

</html>

Create Welcome Page

Here we will give you welcome page source code and logout source code.

//For Welcome Page --- welcome.php
<div class="welcome">
     <h2>Welcome to <?php echo $_SESSION['username']; ?></h2>
     <a class="btn" href="logout.php">Logout</a>
    </div>

//For Logout Page --logout.php
<?php
session_start();
if(session_destroy()){
    echo '<script>alert("You are logout")</script>';
    echo '<script>window.location.href="login.php"</script>';
}
?>

Desgin Project

Friends, the code given below will make your project more attractive, if you want, you can copy this code and paste it in your style file and if you want, you can design by yourself, if you want to use our code then You have to create a file which we have defined in the folder structure. By creating that file, you add a link to it in all your files so that this style will be applied to all the files.

style.css

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: rgb(236, 236, 236);
}
.register{
    width: 300px;
    height: auto;
    padding: 30px 20px;
    background-color: #fff;
    box-shadow: 2px 3px 30px #00000030;
    border-radius: 10px;
}
.register form{
    width: 100%;
    text-align: center;
}
.register form h3{
    text-align: center;
    font-family: sans-serif;
    margin-bottom: 20px;
}
span{
    color: red;
    font-family: sans-serif;
}
.register form input{
    outline: none;
    border: none;
    border: 1px solid rgb(189, 189, 189);
    padding-left: 5px;
    width: 100%;
    height: 35px;
    border-radius: 5px;
    font-size: 15px;
    letter-spacing: 1px;
}
.register form button{
    width: 50%;
    height: 35px;
    outline: none;
    background-color: rgb(9, 0, 131);
    color: #ffff;
    border: none;
    border-radius: 30px;
    margin-top: 20px;
    letter-spacing: 1px;
}

Watch Video Related this Article

Related Articles

Leave a Reply

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

Back to top button