All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Simple Login Form In PHP With MySQL Database

Last Updated : Mar 11, 2024

Simple Login Form In PHP With MySQL Database

In this article we will show you the solution of simple login form in PHP with MySQL database, here first we need to create login form with some input fields and database connection.

As we know valid user only successfully gets login otherwise it won’t allow user to interact with database connection then returns to login page.

If they are valid user then they return to home page with login user details.

Step By Step Guide On Simple Login Form In PHP With MySQL Database :-

Here we developed login form with two input fields ‘Name, Password’ there user have to enter login details then we can check whether user is valid or not by their login details.

If they are entered valid login details then that user take it to their home page otherwise we can’t move to any other page and valid user’s home we provided logout option which is help them to logout from their account whenever.

<!DOCTYPE html>
<html>
<head>
<title>LOGIN</title>
</head>
<body>
<form action="login.php" method="post">
<h2>LOGIN</h2>
<?php if (isset($_GET['error'])) { ?>
<p class="error"><?php echo $_GET['error']; ?></p>
<?php } ?>
<label>User Name</label>
<input type="text" name="uname" placeholder="User Name"><br>
<label>Password</label>
<input type="password" name="password" placeholder="Password"><br>
<button type="submit">Login</button>
</form>
</body>
</html>
Login.php
<?php
session_start();
$conn = mysqli_connect("localhost","root","","dbase");
if (!$conn) {
    echo "Connection failed!";
}
if (isset($_POST['uname']) && isset($_POST['password'])) {
    $uname = $_POST['uname'];
    $pass = $_POST['password'];
        $result = mysqli_query($conn, "SELECT * FROM info WHERE Name='$uname' AND Password='$pass'");
        if (mysqli_num_rows($result) === 1) {
            $row = mysqli_fetch_assoc($result);
            if ($row['Name'] === $uname && $row['Password'] === $pass) {
                echo "Logged in!";
                $_SESSION['user_name'] = $row['Name'];
                $_SESSION['id'] = $row['ID'];
                header("Location: homee.php");
                exit();
            }else{
                header("Location: sample.php?error=Incorect User name or password");
                exit();
            }
        }
        else{
            header("Location: sample.php?error=Incorect User name or password");
            exit();
        }
}else{
    header("Location: sample.php");
    exit();
}
Home.php
<?php
session_start();
if (isset($_SESSION['id']) && isset($_SESSION['user_name'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>HOME</title>
</head>
<body>
<h1>Hello, <?php echo $_SESSION['user_name']; ?></h1>
<a href="logout.php">Logout</a>
</body>
</html>
<?php
}else{
     header("Location: sample.php");
     exit();
}
?>
Logout.php
<?php
session_start();
session_unset();
session_destroy();
header("Location: sample.php");
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we developed index page with login form layout there we defined two input fields ‘Name, Password’ user needs to fill their login details.
  4. In login form we need to confirm two things, at input element definition we have to define ‘name’ attributes and at form element need to specify method attribute with post value, action attribute with login.php external php file which is used to redirect us to next page.
  5. In login.php we at first we started session which is used to maintain login user details for particular amount of time. Here we executed database connection by using ‘mysqli_connect()’ method which contains database, server connection details that is referred by variable ‘$conn’.
  6. If database existed then it return to if condition there we checking whether ‘name, password’ input fields is set ot not. If they sets then we collecting user inputs and stored on respective variables ‘$uname,$pass’.
  7. Using select query we checking which row have same ‘name, password’ like user provided login details then which result stored on variable ‘$result’ by mysqli_query() and there we storing that row user name, id on session variable then finally we navigate to login user’s home page.
  8. In home.php we displaying current user name with welcome message then we provided logout option if they decide to leave this page then user needs to click on logout option.
  9. In logout.php page we destroying session variables and session then redirect to index page.

Conclusion :-

In conclusion now we are able to know how to create simple login form in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser we can see login form there user needs to provide their name and password then it will redirect them to their home page if they provide valid details otherwise it throws error and we can’t move to any other page.

I hope this article on simple login form in PHP with MySQL database helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪