Login form using PHP & MYSQL | Simple Signin System

Simple Login form in PHP

The login system plays an important role in every website. Whenever you have to secure the information, you use the login form. To create a simple login in PHP, you have to use HTML form and PHP script. To authenticate the user to the information, we create Sign In system.

If you have to sell a product on the website and you want the user to login first and purchase the product, you can create a login system. Through the login form, we can secure sensitive information.

Every single website has the same signin and SignUp system. SignUp system is used to take the data from the users and create the profile. We use login sytem in the same information as authentication.

We create a system to restrict every unknown user. First login and get authenticated information from you.

Login System using PHP & MYSQL

MySQL has a large impact role along with PHP to create a login system. When the user enters the username (email, mobile) and password in the HTML form, then PHP runs a query, which is known as MYSQL query.

MySQL query is also made to authenticate the user information. SQL query checks that there are matches in the database. If the information is matched then the user is logged in

.If unknown users try to log in, we make a restriction for them then the information is not matchable and unknown users never log in.

For the PHP login system, we use the MYSQL select query. With the help of MYSQL SELECT query, we select those database table columns that the user use to login.

The user fills the username or email and password to log in. The developer of PHP uses the SELECT query to select a username, email, and password from the database columns.

User Authentication process

User authentication process is a valuable Process for any login system. Authentication is the process of authenticating information that is given by the user at the registration time. The user cannot log in if a problem occurs in the user authenticated information.

Authentication process for Login form

1. First of all, the user fills the authenticated information in HTML form. The authenticated information may be username, email, mobile or password just like facebook.com.

2. After filing the information, if the information matches, then the user logs in.

3. If login informations(Authenticated credential ) do not match the user is not logged in. Here's the problem with the user's authentication.

4. The authentication process restricts the unknown user. They never login without authenticated information.

Hence! The complete Login system is known as the Authentication process

Create an MYSQL database table

Whenever you create a login form, you have to create an MYSQL database table. It is only on the base of the database table that we authenticate the user to the information .Every user is created with authenticated Information at SIGNUP time. Here, we will create a table and insert the data manually.


CREATE TABLE  `user` (

 `id` INT( 40 ) NOT NULL ,
 `uname` VARCHAR( 30 ) NOT NULL ,
 `upassword` VARCHAR( 40 ) NOT NULL
) ENGINE = INNODB DEFAULT CHARSET = latin1;

Insert data into MYSQL databse table using query

The user can not log in unless the data is in the table. Let's insert data into the table. We use MYSQL INSERT query for inserting the data into a database table.


INSERT INTO `user`(`id`, `uname`, `upassword`) VALUES (1,'admin','admin@123');

Create a config file

We create a config.php file to connect PHP and MYSQL. To create a config file, you should know about the following given information.

Database Host

- Database host means the address of the local server and the Global server. For local server, you can use host "localhost" only or 127.0.0.1.

Database Name

-

Define your database name.

Database Username

-

The username of localhost is "root" .You can change username for global server(Live) .

Database Password

-

Define your database password. Default password for localhost is nothing. Change password on Global server according to your need .

Let's create a file and save with name config.php.

<?php

$databaseHost = '127.0.0.1';//or localhost
$databaseName = 'test'; // your db_name
$databaseUsername = 'root'; // root by default for localhost 
$databasePassword = '';  // by defualt empty for localhost

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
 
?>

In above config file , we used mysqli_connect() function for a new connection. We passed these information as parameters and stored in a variable $mysqli .

Make an HTML form

We make an HTML form for filing data. The user gets the login form in the login system. User can login using the form only. In this HTML form. we create two input boxes and one button. This is our simple LOGIN form.

HTML Form code

<html>
<body><center>

<form action="" method="POST">

Username:-

Password:-

<form> </body></center> </html>

Create CSS for Login form

Along with PHP programming, the design should be good too. Here we create simple CSS for input boxes and button. Create a CSS to make a attractive login form.




It produces the output.

Login form in PHP. Create a Login sytem in PHP ,Design HTML form for SIGN IN

Create a PHP script for login

In this step, we create a PHP script. In PHP script, we connect the login form and MySQL server using connection (config.php) file. Whenever a user clicks the login button by entering his username and password, the PHP scripts work in the backend.

First we use the include() function to include the config.php file. Instead of the inclined() function, we can also use the require() function.

After this we perform the select query. By using the select query, we select two columns of the table. We use the mysqli_fetch_array () function to fetch the username and password. Now we create a condition by using the if condition. If the submitted information is matched then the user logs in.

If submitted information does not match, then a failed statement generated. We use the post() method to handle the login form.

PHP Script code

<?php 
include("config.php");

if(isset($_POST['sub']))
{
$uname = $_POST['uname'];
$upassword = $_POST['upassword'];

$res = mysqli_query($mysqli,"select* from user where uname='$uname'and upassword='$upassword'");
$result=mysqli_fetch_array($res);
if($result)
{
echo "You are login Successfully ";
//header("location:my-account.php");   // create my-account.php page for redirection 
	
}
else
{
	echo "failed ";
}
}
?>

POST method in PHP login form script

In previous tutorial , we discussed POST method in PHP . The POST method is used to handle HTML form data. The post method sends information to the server without showing values in page URL. To secure sensitive information to an unknown user, we use the POST method. Send unlimited data to the server using the POST method. Data can be images, characters, documents, etc.

Why should not use GET method in Login system?

GET method is also used to send information to the server by encoding. The form values are visible in page URL using the GET method . We can send only string data types. There is a limit to send data. We never use the GET method in the Login system. The encoded information separated by? (Question mark). You can see the encoded information in the URL separated by the? sign.

When the user fills the username and password and clicks on the submit button, the information visible in the page URL separated by? There is no security with the GET method. That's why we never use the GET method in the Login system.