How to Create User Registration and Login with PHP and MySQL

Do you know how to create Registration and Login with PHP and MySQL ?  if you are a PHP beginner you wish to learn how to create user registration with PHP and MySQL
so here I am writing a script to create a user login with PHP and MySQL

Here i will explain all the steps to create Registration and Login with PHP and if you are confused with the list of codes , dont worry i will add my explainer video and complete source code completely for free.

STEP: 1 CREATE DATABASE (run in phpmyadmin)

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

STEP 2: CREATING CONFIG FILE  (config.php)

<?php

error_reporting(0);
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'demo');
 
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
 
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

STEP 3 : CREATING REGISTRATION FORM (signup.php)

<html>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <div class="container">
    <div class="col-md-6">
    <h2> Signup Form </h2>
    <form method="post" action="createaccount.php">
        <input type="text" name="username" class="form-control" placeholder="Username">        
        <input type="password" name="password" class="form-control" placeholder="Password">
        <input type="submit" value="Register">
    </form>
    </div>

</html>

Create a file called createaccount.php and adding account creation PHP codes  (createaccount.php)

<?php
  session_start();
require_once "config.php";

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = $_POST["username"];
    $password = $_POST["password"];
    // hashing the password
    $password = md5($password );

    //checking username already exists

    $checking = mysqli_query($link,"SELECT * FROM users WHERE username='$username'");
    $checkcount = mysqli_num_rows($checking);

    if($checkcount!=0){
        echo "Username Already exists, Please try another one";
    }
    else
    {
      
        mysqli_query($link,"INSERT INTO `users` (`username`, `password`) VALUES ('$username', '$password')");
         // getting user id 
        $userid =  mysqli_insert_id($link);
      
         //creating session 
         $_SESSION["userid"] = $userid;

        header("location:dashboard.php");
    }
}

 

STEP 4: DASHBOARD (dashboard.php)

<?php
    session_start();
    require_once "config.php";

    $userid = $_SESSION["userid"];
    if($userid!=NULL){
        $getdata = mysqli_query($link,"SELECT * FROM users WHERE id='$userid'");
        $userdata = mysqli_fetch_array($getdata);
?>

<html>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <div class="container">
   
    <h2>Dashboard </h2>
    
    <h3> Welcome <?php echo $userdata['username']; ?> ! </h3>
    <p> Registred at <?php echo $userdata['created_at']; ?> ! </p>

    <h6> <a href="logout.php"> Logout </a> </h6>

    </div>

</html>

<?php } else {
    echo "You are not loged in";
} ?>

 

STEP 5: LOGIN PAGE (login.php)

<html>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <div class="container">
    <div class="col-md-6">
    <h2> Login Form </h2>
    <form method="post" action="logincheck.php">
        <input type="text" name="username" class="form-control" placeholder="Username">        
        <input type="password" name="password" class="form-control" placeholder="Password">
        <input type="submit" value="Login">
    </form>
    </div>

</html>

(logincheck.php) file

<?php
  session_start();
require_once "config.php";
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = $_POST["username"];
    $password = $_POST["password"];
    // hashing the password
    $password = md5($password );
    //checking username already exists
    if($username!=NULL){
       $checking =   mysqli_query($link,"SELECT * FROM users WHERE `username`='$username' AND `password`='$password'");
         // getting user id 
         $userdata = mysqli_fetch_array($checking);
         $userid =  $userdata['id'];
         if($userid!=NULL)
        {
             //creating session 
            $_SESSION["userid"] = $userid;
            
            header("location:dashboard.php");
        }
        else
        {
            header("location:login.php?error=1");
        }
        
    }
    else
    {     
      
        
    }
}

 

STEP 6 : LOGOUT (logout.php)

<?php

session_start(); 
$_SESSION = array(); 
session_destroy();
 
header("location: login.php");
exit;
?>

checkout the video for a detailed explanation



Download Source Code : 

 

Hope this helpful for you.

You can check out our awesome PHP tutorials, it will very helpful if you are a beginner 

What tools i have to use to create Registration and Login with PHP

If you are using a computer You must have a local server installed in your PC. I recommend you to use xampp.  

Where the datas will be saved

When you create a user the data will be saved in your database. in our case we are using MySQL.  

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Leave a Reply

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

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now