Create user registration and login with PHP and MySQLi beginner Guide

In this tutorial I will explain you how to create user registration and login with PHP and MySQLi beginner Guide. User registration and login system is most important part for any kind of web applications and session plays important role for the security of your website.In this tutorial, we are going to use PHP sessions to keep user login status. First create a table name users in database.

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(25) NOT NULL,
  `email` varchar(35) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

Next create a folder name PHPlogin in your localhost. Now create database connection with the database. Create a file name dbconnect.php and paste the below code.

<?php
$servername = "localhost";
$username = "root";//your database username
$password = "";//your database password
$dbname = "test";//your database name
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Next create a file name index.php inside the folder and paste below code. In the below code

<?php
session_start();
include_once 'dbconnect.php';
if(isset($_SESSION['user'])!="")
{
	header("Location: home.php");
}
if(isset($_POST['btn-login']))
{
	$email = mysqli_real_escape_string($conn,$_POST['email']);
	$upass = mysqli_real_escape_string($conn,$_POST['pass']);
	$res=mysqli_query($conn,"SELECT * FROM users WHERE email='$email'");
	$row=mysqli_fetch_array($res);
 
	if($row['password']==md5($upass))
	{
		$_SESSION['user'] = $row['user_id'];
		header("Location: home.php");
	}
	else
	{
		?>
        <script>alert('wrong details');</script>
        <?php
	}
 
}
?>
 
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-login">Sign In</button></td>
</tr>
<tr>
<td><a href="register.php">Sign Up Here</a></td>
</tr>
</table>
</form>

Next create another file name register.php which we will use to register the user.

<?php
session_start();
if(isset($_SESSION['user'])!="")
{
	header("Location: home.php");
}
include_once 'dbconnect.php';
 
if(isset($_POST['btn-signup']))
{
	$uname = mysqli_real_escape_string($conn,$_POST['uname']);
	$email = mysqli_real_escape_string($conn,$_POST['email']);
	$upass = md5(mysqli_real_escape_string($conn,$_POST['pass']));
 
	if(mysqli_query($conn,"INSERT INTO users(username,email,password) VALUES('$uname','$email','$upass')"))
	{
		?>
        <script>alert('successfully registered ');</script>
        <?php
	}
	else
	{
		?>
        <script>alert('error while registering you...');</script>
        <?php
	}
}
?>
 
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="uname" placeholder="User Name" required /></td>
</tr>
<tr>
<td><input type="email" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
<tr>
<td><a href="index.php">Sign In Here</a></td>
</tr>
</table>
</form>

Make sure you will use the session_start before the html. If the user is not logged in user will be redirected to login page.

Now create a file name home.php and paste the below code. we will redirect the user to home page once the login is successful.

<?php
session_start();
include_once 'dbconnect.php';
 
if(!isset($_SESSION['user']))
{
	header("Location: index.php");
}
$res=mysqli_query($conn,"SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);
?>
<div id="header">
    <div id="right">
    	<div id="content">
        	hi' <?php echo $userRow['username']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
</div>

Once the user get logged in we use logout button to destroy the user session and logout from the website. Create a file name logout.php and paste the below code.

<?php
session_start();
 
if(!isset($_SESSION['user']))
{
	header("Location: index.php");
}
else if(isset($_SESSION['user'])!="")
{
	header("Location: home.php");
}
 
if(isset($_GET['logout']))
{
	session_destroy();
	unset($_SESSION['user']);
	header("Location: index.php");
}
?>

This is how your file structure will look like inside the PHPlogin folder

dbconnect.php
register.php
index.php
home.php
logout.php

Now run the url http://localhost/PHPlogin and you can register and do login.

Hope this article Create user registration and login with PHP and MySQLi beginner Guide will help you.