Hi, If you are looking for a PHP code for a login and registration page, then this is the right place.
In this tutorial, we are going to create a login and registration system in PHP.
Basically, this tutorial will provide basic knowledge about creating a simple website login and registration system.
In this system, a user can create an account by providing a username, email, and password, and the user can also login and logout.
Watch the demo –
To create this, you must have a basic knowledge of PHP like how PHP works.
As we all know that PHP is a server-side programming language, for that reason you must have a server to run it.
If you do not have any server then you create a local server using XAMPP software.
Follow the below steps to create this application
Step – 1
First, you go to your phpMyAdmin and create a new Database and name it login_registration
.


Step – 2
After creating the database select this login_registration
Database and go to SQL section then copy the below SQL code and paste into the SQL text-area and then click on Go button.
This SQL code creates `users` table into the login_registration
Database and it also creates `users` table structure.
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`username` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`user_email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`user_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT;

Step – 3
Go to your www directory or Xampp htdocs folder and create a new folder and name it as you want.
Structure of the newly created folder.
- db_connection.php
- home.php
- index.php
- insert_user.php
- login.php
- logout.php
- signup.php
- style.css
Writing PHP code for creating login and registration page
db_connection.php
Related – How to create database connection with MySQL Database in PHP
<?php
$db_connection = mysqli_connect("localhost","root","","login_registration");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
After making the database connection we’ll create signup.php and insert_user.php to insert the user into the database.
signup.php
<?php
session_start();
require 'db_connection.php';
require 'insert_user.php';
// IF USER LOGGED IN
if(isset($_SESSION['user_email'])){
header('Location: home.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign Up - Footmarkz.com</title>
<link rel="stylesheet" href="style.css" media="all" type="text/css">
</head>
<body>
<form action="" method="post">
<h2>Create an account</h2>
<div class="container">
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Enter username" id="username" name="username" required>
<label for="email"><b>Email</b></label>
<input type="email" placeholder="Enter email" id="email" name="user_email" required>
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter password" id="password" name="user_password" required>
<button type="submit">Sign Up</button>
</div>
<?php
if(isset($success_message)){
echo '<div class="success_message">'.$success_message.'</div>';
}
if(isset($error_message)){
echo '<div class="error_message">'.$error_message.'</div>';
}
?>
<div class="container" style="background-color:#f1f1f1">
<a href="index.php"><button type="button" class="Regbtn">Login</button></a>
</div>
</form>
</body></html>
insert_user.php
Related – Insert data into MySQL Database in PHP
<?php
if(isset($_POST['username']) && isset($_POST['user_email']) && isset($_POST['user_password'])){
// CHECK IF FIELDS ARE NOT EMPTY
if(!empty(trim($_POST['username'])) && !empty(trim($_POST['user_email'])) && !empty($_POST['user_password'])){
// Escape special characters.
$username = mysqli_real_escape_string($db_connection, htmlspecialchars($_POST['username']));
$user_email = mysqli_real_escape_string($db_connection, htmlspecialchars($_POST['user_email']));
//IF EMAIL IS VALID
if (filter_var($user_email, FILTER_VALIDATE_EMAIL)) {
// CHECK IF EMAIL IS ALREADY REGISTERED
$check_email = mysqli_query($db_connection, "SELECT `user_email` FROM `users` WHERE user_email = '$user_email'");
if(mysqli_num_rows($check_email) > 0){
$error_message = "This Email Address is already registered. Please Try another.";
}
else{
// IF EMAIL IS NOT REGISTERED
/* --
ENCRYPT USER PASSWORD USING PHP password_hash function
LEARN ABOUT PHP password_hash - http://php.net/manual/en/function.password-hash.php
-- */
$user_hash_password = password_hash($_POST['user_password'], PASSWORD_DEFAULT);
// INSER USER INTO THE DATABASE
$insert_user = mysqli_query($db_connection, "INSERT INTO `users` (username, user_email, user_password) VALUES ('$username', '$user_email', '$user_hash_password')");
if($insert_user === TRUE){
$success_message = "Thanks! You have successfully signed up.";
}
else{
$error_message = "Oops! something wrong.";
}
}
}
else {
// IF EMAIL IS INVALID
$error_message = "Invalid email address";
}
}
else{
// IF FIELDS ARE EMPTY
$error_message = "Please fill in all the required fields.";
}
}
?>
After that, we will create index.php and login.php for user login purpose.
index.php
<?php
session_start();
require 'db_connection.php';
require 'login.php';
// IF USER LOGGED IN
if(isset($_SESSION['user_email'])){
header('Location: home.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Footmarkz.com</title>
<link rel="stylesheet" href="style.css" media="all" type="text/css">
</head>
<body>
<form action="" method="post">
<h2>User Login</h2>
<div class="container">
<label for="email"><b>Email</b></label>
<input type="email" placeholder="Enter email" id="email" name="user_email" required>
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter password" id="password" name="user_password" required>
<button type="submit">Login</button>
</div>
<?php
if(isset($success_message)){
echo '<div class="success_message">'.$success_message.'</div>';
}
if(isset($error_message)){
echo '<div class="error_message">'.$error_message.'</div>';
}
?>
<div class="container" style="background-color:#f1f1f1">
<a href="signup.php"><button type="button" class="Regbtn">Create an account</button></a>
</div>
</form>
</body></html>
login.php
<?php
if(isset($_POST['user_email']) && isset($_POST['user_password'])){
// CHECK IF FIELDS ARE NOT EMPTY
if(!empty(trim($_POST['user_email'])) && !empty(trim($_POST['user_password']))){
// Escape special characters.
$user_email = mysqli_real_escape_string($db_connection, htmlspecialchars(trim($_POST['user_email'])));
$query = mysqli_query($db_connection, "SELECT * FROM `users` WHERE user_email = '$user_email'");
if(mysqli_num_rows($query) > 0){
$row = mysqli_fetch_assoc($query);
$user_db_pass = $row['user_password'];
// VERIFY PASSWORD
$check_password = password_verify($_POST['user_password'], $user_db_pass);
if($check_password === TRUE){
session_regenerate_id(true);
$_SESSION['user_email'] = $user_email;
header('Location: home.php');
exit;
}
else{
// INCORRECT PASSWORD
$error_message = "Incorrect Email Address or Password.";
}
}
else{
// EMAIL NOT REGISTERED
$error_message = "Incorrect Email Address or Password.";
}
}
else{
// IF FIELDS ARE EMPTY
$error_message = "Please fill in all the required fields.";
}
}
?>
After that, we’ll create home.php. Users can access this home.php page after logged in successfully.
home.php
<?php
session_start();
require 'db_connection.php';
// CHECK USER IF LOGGED IN
if(isset($_SESSION['user_email']) && !empty($_SESSION['user_email'])){
$user_email = $_SESSION['user_email'];
$get_user_data = mysqli_query($db_connection, "SELECT * FROM `users` WHERE user_email = '$user_email'");
$userData = mysqli_fetch_assoc($get_user_data);
}else{
header('Location: logout.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css" media="all" type="text/css">
<title>Home</title>
<style>
a, a:visited{
color: #0000EE;
}
a:hover{
color: #EE0000;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello, <?php echo $userData['username'];?></h1>
<a href="logout.php">Logout</a>
</div>
</body>
</html>
To log out the user, we will create the logout.php file.
logout.php
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
header("Location: index.php");
exit;
?>
And last we’ll create style.css to beautifying our front-end interface.
style.css
body {
padding: 5px;
margin: 0;
background-color: #FFF;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}
h2 {
text-align: center;
text-transform: uppercase;
letter-spacing: 6px;
}
form {
border: 3px solid #f1f1f1;
max-width: 600px;
margin: 0 auto;
}
input[type=text],
input[type=email],
input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
opacity: 0.8;
}
.Regbtn {
width: auto;
padding: 10px 18px;
background-color: #0077c8;
}
.error_message {
color: red;
padding-bottom: 10px;
text-align: center;
font-weight: bold;
}
.success_message {
color: green;
padding-bottom: 10px;
text-align: center;
font-weight: bold;
}
.container {
padding: 16px;
}
@media screen and (max-width: 300px) {
span.psw {
display: block;
float: none;
}
.Regbtn {
width: 100%;
}
}
Completed.
Download the PHP code of login and registration System
excellent this is good work!
thnks good work
How to add forgot password option in this page ??
I will add this tutorial soon.