In this post, we’re going to learn how to create a log-in form with php for a webpage in order to restrict access to some certain information on the site.

Languages we’re going to use here are HTML, SQL and PHP in order to keep it simple.

Before we start, let me explain the whole process. Firstly we create our log-in form with HTML which will include an email field and a password field. Next we add a PHP script which searches the database for a corresponding email. If a match is found, it checks the password of that email and verifies it also.

Once the password has been verified, a session is going to be set in the script and the user is redirected to the restricted page.

Note: it’s advisable to always hash the password field with the “password_hash” function for better security.

Step 1.

Creating your database.

It’s always advisable to start your projects with the database. This can help you plan ahead of what you’re building. To create the database, make sure you have a database server installed either in your website or locally. Depending on where you choose to work on.

We’ve decided to name our database “Charming_desk” for simplicity. Now we create our first table and name it “user-login”. This table will contain four fields. id, email, fullname and password.

Our database details will be as follows. Email= johndoe@example.com. password =john419. Full name= John doe.

Then we insert our values into the database with the SQL code below.

INSERT INTO user-login(id, email, fullname, password)VALUES("1","johndoe@example.com","john419","John doe");

//If you have phpmyadmin, you can do this easily by filling up the fields in the insert tab.

Now that we’ve created our database inserted values in it, let’s create our index.php and login.php files.

Step 2.

Creating our restricted page (index.php).

In this page, we first of all start our session like this

Related: How to upload images on your website using a form.

<?php
session_start();

Now we check if a session named “user” is set. If it is set, we will remain in the index.php page. Else we’ll be redirected to the login.php page.

If(isset($_SESSION['user'])){

echo "$_SESSION['user']";

}
else{

header ("location:login.php");
}
?>

//Since our session named user is not set we'll have to be redirected to the login form.

Step 3.

Creating your login form.

Let us create our login form with html.

<?php session_start();?>

//to enable sessions in this page.


<form method="post">
<input type="email" name="email" value="" placeholder="write your email addreas"><br>

<input type="password"
name="password" placeholder="write your password here" value=""><br>

<button type="submit" name="submit">verify</button>
</form>

Step 4.

Make our login form work.

Now that we’ve created our form, let’s add some php code to ensure it works.

You might like How to publish your html codes online for free.

<?php
if (isset( $_POST['submit'] )){
$email= $_POST['email'];
$password= $_POST['password'];


$query=mysqli_query($conn, "SELECT * FROM user-login WHERE email='$email' AND password= '$password'");

//Check if combination exists.

if (mysqli_num_rows ($query)>0){
$row= mysqli_fetch_assoc($query);

//Fetch values in array.

$fullname= $row['fullname'];

//set our user session.

$_SESSION['user'] =$fullname;

//Redirect to restricted page.

header ("location:index.php");

}
else{

echo "invalid details";

}
}
?>

If the values are correct, you’ll be directed to the restricted page (index.php) and your full name will be printed on the screen.

Else you’ll remain in the login page with a write up saying “invalid details”.

Related: How to create a working sign-up form with php and SQL

Warning! This method is to be used for the purpose of testing only as the fields are not properly validated which can lead to attacks from hackers.

Verifying hashed passwords.

Sometimes the passwords stored in the database can be hashed using the password_hash() function making it difficult to compare. Well the good news is that php has a password verify function which enables you to verify hashed passwords in the database. Here’s a quick snippet on how to use it.

$password= //password from the input field.

$dbpassword= //password from the database.

if (password_verify ($password, $dbpassword)){

echo "verification sucessful";

header ("location: index.php");

}

That’s all you need to know about creating a log-in form. Feel free to follow our social media accounts for updates. Kudos!

Charming desk. Your favorite web developers!

Categories: php

vic okeke

I'm just a web developer hoping to help out junior developers in algorithms for their projects.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *