Php mysqli Login Registration Source code

Php MySQLi Login Registration Source code, Php MySQLi Login Registration System Source code, Php MySQLi Login Registration latest tutorial 2020



Lets Create Complete Login , Registration system using Php MySQLi  use following steps 

  1.  Create Database :  UserRegistration 
  2. Create  Table : User As shown in image below , use SQL Script  


My Sql User Registration Table 

   CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `trn_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
 


Lets Starts Code 

1. Create Php MySQLi Connection String page as db.php page 

 write connection string as follows 

<?php
$con = mysqli_connect("localhost","root","","UserRegistration")or die("insert-view-edit-and-delete-record");
?>

2.  Create Page with handle Session After user login  session_check.php

where  i am cheking if user is not valid it will redirect to login.php page 

<?php
session_start();
if(!isset($_SESSION["username"]))
{
header("Location: login.php");
exit(); }
?>


Create CSS Folder and Create style.css file in it  use following css code 

body {background-color:gray;margin-top:200px;}
.clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after { clear: both; }
a {color:#0067ab; text-decoration:none;}
a:hover {text-decoration:underline;}
.form{background-color:silver; padding:15px; margin-top:30px; width: 300px; margin: 0 auto;border:2px solid red; border-radius:60px;}
input[type='text'], input[type='email'], input[type='password'] {width: 200px; border-radius: 2px;border: 1px solid #CCC; padding: 10px; color: #333; font-size: 14px; margin-top: 10px;}
input[type='submit']{padding: 10px 25px 8px; color: #fff; background-color: #0067ab; text-shadow: rgba(0,0,0,0.24) 0 1px 0; font-size: 16px; box-shadow: rgba(255,255,255,0.24) 0 2px 0 0 inset,#fff 0 1px 0 0; border: 1px solid #0164a5; border-radius: 2px; margin-top: 10px; cursor:pointer;}
input[type='submit']:hover {background-color: #024978;}









3. Create Login Page as login.php   


where we check if user is valid it will logged in and redirect to index.php otherwise it will give invalid user message by clicking user registration user can create new account 



Save following code as  login.php   

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>User Login</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
session_start();
    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){
$username = stripslashes($_REQUEST['username']); // removes backslashes
$username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
        $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'"; // MD5 Encryption 
$result = mysqli_query($con,$query) or die(mysqli_error());
$rows = mysqli_num_rows($result);
        if($rows==1){
$_SESSION['username'] = $username;
header("Location: index.php"); // Redirect user to index.php
            }else{
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
    }else{
?>
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>

</div>
<? php } 
?>


</body>
</html>


4. Create User Registration page  Page as registration.php 

here we are using 3 fields as username  , email id and password 
we are going to use md5 encryption for user registration 



Use Following code save as registration.php 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>User Registration</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<?php

require('db.php');

    // If form submitted, insert values into the database.

    if (isset($_REQUEST['username'])){

$username = stripslashes($_REQUEST['username']); // removes backslashes

$username = mysqli_real_escape_string($con,$username); //escapes special characters in a string

$email = stripslashes($_REQUEST['email']);

$email = mysqli_real_escape_string($con,$email);

$password = stripslashes($_REQUEST['password']);

$password = mysqli_real_escape_string($con,$password);


$trn_date = date("Y-m-d H:i:s");

        $query = "INSERT into `users` (username, password, email, trn_date) VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";

        $result = mysqli_query($con,$query);

        if($result){

            echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";

        }

    }else{

?>

<div class="form">

<h1>Registration</h1>

<form name="registration" action="" method="post">

<input type="text" name="username" placeholder="Username" required />

<input type="email" name="email" placeholder="Email" required />

<input type="password" name="password" placeholder="Password" required />

<input type="submit" name="submit" value="Register" />

</form>

</div>

<?php } ?>

</body>

</html>



5. Lets Write Code For Logout as logout.php 

<?php

session_start();

if(session_destroy()) // Destroying All Sessions

{

header("Location: login.php"); // Redirecting To Home Page

}

?>


Click here to download Complete

Php MySQLi Login Registration Source code



Php MySQLi Login Registration Source code, Php MySQLi Login Registration System Source code, Php MySQLi Login Registration latest tutorial 2020






Oldest

Display Data Dynamically in Dropdown using Php MySQLi

 Display Data Dynamically in Dropdown using Php MySQLi Display Data Dynamically in Dropdown using Php MySQLi,  php Mysql Dropdown tutorial ,...

Article