Download Login Form in PHP and Mysql

Abhigyan Singh 09th Sep 2020

In this tutorial, I will explain how to create a login form in PHP MySQL.php MySQL Creating a login form in any language has the same concept that We will retrieve the previously stored User credential like User name and password and while Login when the user provides inputs like Username and password, we will compare this Input with the stored fields for that User.

When both are the same user will redirect to the next page otherwise some error will print with login fail message. Now let us understand the steps behind this:

Steps for creating a Login Form are given below:
There are two PHP file

  1. dbConfig.php
  2. login.php

The dbConfig.php file looks like this:

<?php
define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", ""); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME",""); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
?>

Here just change the above value like Hostname, database user name, database password, and database name.

The login.php file looks like this:

<?php

include "dbConfig.php";
$msg = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$password = md5($_POST["password"]);
if ($name == '' || $password == '') {
$msg = "You must enter all fields";
} else {
$sql = "SELECT * FROM members WHERE name = '$name' AND password = '$password'";
$query = mysql_query($sql);

if ($query === false) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($query) > 0) {

header("Location: YOUR_LOCATION");
exit;
}

$msg = "Username and password do not match";
}
}
?>

In this file just change YOUR_LOCATION variable into your exact success path.

That's all... Enjoy with login form

For any feedback and comment, please write us below a comment. Your comment will be highly appreciable.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk