What You Will Learn?
As stated previously, in this article, you are going to learn the following important concepts while creating the complete user registration form in PHP and using the MySQL database.
How to create a user registration form in HTML and CSS?
- How to create a new MySQL database in PHPMyAdmin?
- How to connect with MySQL database with PHP?
- How to do registration form validation in PHP?
- Password encryption or password hashing in PHP?
- How to insert or update user data into the database table?
- How to use $_session variable in the PHP user registration and login form?
- How to set the cookies variable in PHP for user registration?
- How to create a User logout PHP code?
- How to create a user login system in PHP?
- How to write PHP code for the user login form?
Video Tutorial – Login Register PHP
Meanwhile, You can watch the following video tutorial which includes the step-by-step process to create a complete user registration form using PHP and MySQL databases.
Steps – Login and Register for PHP Form
More importantly, to create a complete user registration system in PHP and MySQL, we are going to adopt the following approach.
- First of all, we are going to create the following PHP files:
- Create an index.php file. This file contains the HTML and CSS code for the user Sign up form.
- Create a new MySql database in PHPMyAdmin and a new user table where you want to store the user login details.
- Create a linkDB.php file. This file will contain MYSQL database connection PHP code.
- Create a server.php file. This file will contain all server-side PHP and MYSQL database codes for user registration and user login forms. This file is linked with both, index.php and login.php files using the PHP include function.
- Create a LoggedInPage.php file. This will be your home page, and after the successfully logged user in, will redirect to this home page.
- Create a login.php file. This file contains the login form HTML and CSS code.
Source Code – (index.php File)
The following HTML CSS source code belongs to the index.php file. This code is for the user registration form in PHP.
<!-- PHP command to link server.php file with registration form -->
<?php include('server.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration</title>
<!-- CSS Code -->
<style>
.container{
justify-content: center;
text-align: center;
align-items: center;
}
input{
padding: 5px;
}
.error{
background-color: pink;
color: red;
width: 300px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>User Registration System</h1>
<h4><a href="loggedInPage.php">Home Page</a></h4>
<div class="form" id="signUp">
<form method="POST">
<div class="error"> <?php echo $error ?> </div>
<!--------- To check user regidtration status ------->
<p>
<?php
if (!isset($_COOKIE["id"]) OR !isset($_SESSION["id"]) ) {
echo "Please first register to proceed.";
}
?>
</p>
<input type="text" name="name" placeholder="User Name"> <br> <br>
<input type="email" name="email" placeholder="Email"> <br><br>
<input type="password" name="password" placeholder="password"><br><br>
<input type="password" name="repeatPassword" placeholder="Repeat Password"><br><br>
<label for="checkbox">Stay logged in</label>
<input type="checkbox" name="stayLoggedIn" id="chechbox" value="1"> <br><br>
<input type="submit" name="signUp" value="Sign Up">
<p >Have an account already? <a href="logIn.php">Log In</a></p>
</form>
</div>
</body>
</html>
Source Code – (login.php File)
<!-- PHP command to link server.php file with registration form -->
<?php include('server.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User logIn</title>
<style>
.container{
justify-content: center;
text-align: center;
align-items: center;
}
input{
padding: 5px;
}
.error{
background-color: pink;
color: red;
width: 300px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1> User Registration System</h1>
<h4><a href="loggedInPage.php">Home Page</a></h4>
<!--------log in form------>
<div class="logInForm" id="logIn">
<form method="POST">
<!-- To show errors is user put wrong data -->
<div class="error"> <?php echo $error2 ?> </div>
<!-- To check the user loged In status -->
<p>
<?php
if (!isset($_COOKIE["id"]) OR !isset($_SESSION["id"]) ) {
echo "<p>Please first log in to proceed.</p>";
}
?>
</p>
<input type="email" name="email" placeholder="Email"> <br><br>
<input type="password" name="password" placeholder="password"><br><br>
<label for="checkbox">Stay logged in</label>
<input type="checkbox" name="stayLoggedIn" id="chechbox" value="1"> <br><br>
<input type="submit" name="logIn" value="Log In">
<!-- User registration form link -->
<p>Not a register user <a href="index.php"> Create Account</a></p>
</form>
</div>
</div>
</script>
</body>
</html>
Source Code – (linkDB.php File)
The linkDB.php includes the following PHP code.
<?php
// Open a new connection to the MySQL server
$linkDB = mysqli_connect("localhost","my_user_name","my_password","my_db_name");
if (mysqli_connect_error()){ //for connection error finding
die ('There was an error while connecting to database');
}
?>
Source Code – (server.php File)
The following back-end code for the user registration form in PHP will be included in the server.php file:
<?php
session_start();
//------ PHP code for User registration form---
$error = "";
if (array_key_exists("signUp", $_POST)) {
// Database Link
include('linkDB.php');
//Taking HTML Form Data from User
$name = mysqli_real_escape_string($linkDB, $_POST['name']);
$email = mysqli_real_escape_string($linkDB, $_POST['email']);
$password = mysqli_real_escape_string($linkDB, $_POST['password']);
$repeatPassword = mysqli_real_escape_string($linkDB, $_POST['repeatPassword']);
// PHP form validation PHP code
if (!$name) {
$error .= "Name is required <br>";
}
if (!$email) {
$error .= "Email is required <br>";
}
if (!$password) {
$error .= "Password is required <br>";
}
if ($password !== $repeatPassword) {
$error .= "Password does not match <br>";
}
if ($error) {
$error = "<b>There were error(s) in your form!</b> <br>".$error;
} else {
//Check if email is already exist in the Database
$query = "SELECT id FROM users WHERE email = '$email'";
$result = mysqli_query($linkDB, $query);
if (mysqli_num_rows($result) > 0) {
$error .="<p>Your email has taken already!</p>";
} else {
//Password encryption or Password Hashing
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$query = "INSERT INTO users (name, email, password) VALUES ('$name', '$email', '$hashedPassword')";
if (!mysqli_query($linkDB, $query)){
$error ="<p>Could not sign you up - please try again.</p>";
} else {
//session variables to keep user logged in
$_SESSION['id'] = mysqli_insert_id($linkDB);
$_SESSION['name'] = $name;
//Setcookie function to keep user logged in for long time
if ($_POST['stayLoggedIn'] == '1') {
setcookie('id', mysqli_insert_id($linkDB), time() + 60*60*365);
//echo "<p>The cookie id is :". $_COOKIE['id']."</P>";
}
//Redirecting user to home page after successfully logged in
header("Location: loggedInPage.php");
}
}
}
}
//-------User Login PHP Code ------------
if (array_key_exists("logIn", $_POST)) {
// Database Link
include('linkDB.php');
//Taking form Data From User
$email = mysqli_real_escape_string($linkDB, $_POST['email']);
$password = mysqli_real_escape_string($linkDB, $_POST['password']);
//Check if input Field are empty
if (!$email) {
$error2 .= "Email is required <br>";
}
if (!$password) {
$error2 .= "Password is required <br>";
}
if ($error2) {
$error2 = "<b>There were error(s) in your form!</b><br>".$error2;
}
else {
//matching email and password
$query = "SELECT * FROM users WHERE email='$email'";
$result = mysqli_query($linkDB, $query);
$row = mysqli_fetch_array($result);
if (isset($row)) {
if (password_verify($password, $row['password'])) {
//session variables to keep user logged in
$_SESSION['id'] = $row['id'];
//Logged in for long time untill user didn't log out
if ($_POST['stayLoggedIn'] == '1') {
setcookie('id', $row['id'], time() + 60*60*24); //Logged in permanently
}
header("Location: loggedInPage.php");
} else {
$error2 = "Combination of email/password does not match!";
}
} else {
$error2 = "Combination of email/password does not match!";
}
}
}
?>
User Logout PHP Code
The below code is used to log out with a session in PHP and log out PHP session.
//PHP code to logout user from website
if (isset($_GET["logout"])) {
unset($_SESSION['id']);
setcookie("id", "", time() - 3600);
$_COOKIE['id'] = "";
}
Final Words
In the above discussion, we have created the registration and login form in PHP and MySQL databases. If you want to implement the above user registration form in PHP on your website then you need to follow the procedures explained in this tutorial. If you still find any difficulty in understanding, feel free to contact this website www.hmawebdesign.com. If you found this tutorial helpful please don’t forget to SUBSCRIBE YOUTUBE CHANNEL.
Share this Article!
Top comments (0)