User Registration with PHP and MySQL

Hello,  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

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/bootstrap@5.0.0-beta2/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/bootstrap@5.0.0-beta2/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/bootstrap@5.0.0-beta2/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 : 

 

Share with your friends:

Ajith Jojo Joseph

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

HTML Form to Email Using JavaScript and SMTP server 🚀

Hello Everyone, 🙋‍♂️ In this tutorial, we are going to look at how to send emails from HTML Form to […]

22 September, 2021

How to make pretty URL in PHP using .htaccess

Hello Everyone, Here in this tutorial, you will learn How to make a pretty URL in PHP using .htaccess. and […]

18 September, 2021

Submit Form Without Refreshing Page PHP and Javascript

Hello Everyone, In this tutorial, we are going to learn, How to submit a form without reloading. yeah, that’s so […]

HTML from to Email using SMTP in PHP with Source code

Hello Everyone, In this tutorial, I am going to teach you how to send an email from an HTML form […]

16 September, 2021

Newsletter

Subscribe to our weekly Newsletter and stay tuned.


Free PHP Invoicing Software

Gmax CRM Lite is an Invoicing and project management tool for small businesses, features including client management , invoicing , quotation and project management

Download for free