PHP - Simple Login With Password Verfication

In this tutorial we will create a Simple Login With Password Verfication using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_verify, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_verify");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Login With Password Verfication</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <h3 class="text-success">User Login</h3>
  20. <br />
  21. <form method="POST">
  22. <div class="form-group">
  23. <label>Username</label>
  24. <input type="text" class="form-control" name="username" required="required"/>
  25. </div>
  26. <div class="form-group">
  27. <label>Password</label>
  28. <input type="password" class="form-control" name="password" required="required"/>
  29. </div>
  30. <?php include 'login.php'?>
  31. <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  32. </form>
  33. </div>
  34. </div>
  35. </body>
  36. </html>

Creating the Main Function

This code contains the main function of the application. This code will verify the user password to prove that is valid access the account. To do that just copy and write this block of codes inside the text editor, then save it as login.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7.  
  8. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username'") or die(mysqli_error());
  9. $row = mysqli_num_rows($query);
  10. $fetch = mysqli_fetch_array($query);
  11. if($row > 0){
  12. if(password_verify($password, $fetch['password'])){
  13. echo "<center><h4 class='text-success'>Password verified!</h4></center>";
  14. }else{
  15. echo "<center><h4 class='text-danger'>Password not verified!</h4></center>";
  16. }
  17. }else{
  18. echo "<center><h4 class='text-danger'>User not found!</h4></center>";
  19. }
  20. }
  21. ?>
There you have it we successfully created a Simple Login With Password Verfication using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment