How To Build A LOGIN/REGISTRATION System in PHP Using A Flat-File Database
What Will I Learn?
- You will learn how to build a LOGIN/REGISTRATION System in PHP using a flat-file database
Requirements
- PHP INSTALLATION
- Compatible Server Software Installation (Apache , Nginx ,e.t.c. )
Difficulty
- Intermediate
Tutorial Contents
The essence of this tutorial is to cater for situations when there might be no database or when using a flat file database for the sake of security and speed or easy remote database access.
STEP 1: Creation of the Flat file database as an empty file
Just create a file and name it "file.db"
STEP 2: Creation of the Registration Frontend
This registration is going to be simple to make the tutorial easily comprehendable.
- Create an empty file and name it
registration.php - then, copy the html codes below to create the registration form.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" style="border:1px solid #ccc">
<div class="container">
<h1>Sign Up</h1>
<p>Please fill inon this form to create an account.</p>
<hr>
<label for="email"><b>Email</b></label>
<input type="email" placeholder="Enter Email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<div class="clearfix">
<input type="submit" name="submit" value="Submit">
</div>
</div>
</form>
The above html codes is pretty easy to understand. It contains three fields (Email, Password & Repeat Password) and it has an action page that leads to the same page.
STEP 3: Creation of the Registration Backend
The Codes below represent the backend processing of the html codes and they should be copied inside registration.php on top of the registration html codes.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["email"]) && !empty($_POST["psw"]) && !empty($_POST["psw-repeat"])) {
$email = trim($_POST["email"]);
if ($_POST["psw"] == $_POST["psw-repeat"]){
$pw = md5($_POST["psw"]);
$id = md5($_POST["email"].$_POST["psw"]);
$file = file_get_contents('http://localhost/tut/file.db');
$file = json_decode($file, true);
if (empty($file)){
$new = array(
$id => array(
'email' => "$email",
'password' => "$pw"
)
);
$db =json_encode($new, JSON_FORCE_OBJECT);
}
else if (is_array($file)){
$new = array('email' => "$email",'password' => "$pw");
$file["$id"] = $new;
$db =json_encode($file,JSON_FORCE_OBJECT);
}
$filename = 'file.db';
$fp = fopen($filename, 'w');
fwrite($fp, $db);
fclose($fp);
echo 'Registration Done';
}
else {die('Passwords Not the same');}
}
else {die('Fields are empty');}
}
?>
if ($_SERVER["REQUEST_METHOD"] == "POST"){} : This checks if the form was actually submitted and prevents the code from running by default when page is loaded.
if (!empty($_POST["email"]) && !empty($_POST["psw"]) && !empty($_POST["psw-repeat"])){}: This checks if all fields were filled $email = trim($_POST["email"]); : This removes unnecessary white spaces at the begining and end of the string if ($_POST["psw"] == $_POST["psw-repeat"]){}: This makes sure the user's password is the same as the repeat password $pw = md5($_POST["psw"]): This hashes the password $id = md5($_POST["email"].$_POST["psw"]);: This creates the id from hashing the concatenation of the password and the email $file = file_get_contents('http://localhost/tut/file.db'); $file = json_decode($file, true);: This retrieves the initial database content and decodes it into an array. if (empty($file)){}: This checks if the db empty, this might only happen once when you are making an entry for the first time $new = array($id => array('email' => "$email",'password' => "$pw")); $db =json_encode($new, JSON_FORCE_OBJECT);: This generates an array for the new user details and then encode it as a json file. $filename = 'file.db'; $fp = fopen($filename, 'w'); fwrite($fp, $db); fclose($fp);echo 'Registration Done';: This stores the json into the file.db and declares the registration as done STEP 4: Creation of the login Front End
- Create an empty file and name it
login.php - Copy the following codes into the file
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" style="border:1px solid #ccc">
<div class="container">
<h1>LOGIN</h1>
<hr>
<label for="email"><b>Email</b></label>
<input type="email" placeholder="Enter Email" name="email" required>
<hr>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<div class="clearfix">
<input type="submit" name="submit" value="Submit">
</div>
</div>
</form>
The above code is just a simple login snippet that contains a compulsory email and password field.
STEP 5: Creation of the login backend.
- Just copy the following php codes above the login html codes.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["email"]) && !empty($_POST["psw"]) ) {
$email = trim($_POST["email"]);
$psw = trim($_POST["psw"]);
$id = md5($_POST["email"].$_POST["psw"]);
$file = file_get_contents('http://localhost/file.db');
$file = json_decode($file, true);
if (array_key_exists($id,$file)){
die('login Successfully');
}
else { die('login does not exist');}
}
}
?>
if ($_SERVER["REQUEST_METHOD"] == "POST"){}: this checks if the form has been submitted and protects the codes from by running by default when the page is loaded.
if (!empty($_POST["email"]) && !empty($_POST["psw"]) ) {}: This checks if the email and password field are not empty on submission $email = trim($_POST["email"]); $psw = trim($_POST["psw"]);: This removes unnecessary white spaces at the endand beginning of the email and password string. $id = md5($_POST["email"].$_POST["psw"]); $file = file_get_contents('http://localhost/file.db'); $file = json_decode($file, true); if (array_key_exists($id,$file)){ die('login Successful'); } else { die('login does not exist');}The above codes creates an md5 hash of the concantenation of the email and password and checks if such s key exists , because , if you remember , that was how we created the key. Now if yes, the codes 'dies` and echoes 'login successful', if not it echoes 'login does not exist'.
STEP 6: Download the whole file and test the codes
You can now download this file from here and register as many accounts as possiblt, then play with the login.
Curriculum
If you are interested in other tutorials about PHP. Check Below
Curriculum
PHP MVC SERIES
- PHP MVC SERIES: How to use MVC Structured Codebase in PHP (PART 1)
- PHP MVC SERIES: How to use MVC Structured Codebase in PHP (PART 2)
PHP FORM SERIES
- PHP FORM SERIES: How to receive form data in PHP (POST AND GET METHOD)
- PHP FORM SERIES: How to format form data
- PHP FORM SERIES: How to insert data into database
PHP REGEX
Posted on Utopian.io - Rewarding Open Source Contributors