PHP - Login And Registration Source Code

In this tutorial we will create a Login And Registration using PHP. This code will register and login the user account when the form is submitted. The code use PHP POST method to call a function that will register a new user account using MySQLi INSERT query and login a user account using SELECT query by providing a condition in the WHERE clause. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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_register_login, 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_register_login");
  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 your text editor, then save it as shown below. 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 - Login And Registration Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-5">
  18. <div class="alert alert-info">No account yet? Register here...</div>
  19. <form action="" method="POST">
  20. <div class="form-group">
  21. <label>Name</label>
  22. <input type="text" name="name" class="form-control" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" maxlength="12" name="password" class="form-control" required="required"/>
  31. </div>
  32.  
  33. <center><button name="register" class="btn btn-primary">Register</button></center>
  34.  
  35. </form>
  36. <br />
  37. <?php include'register.php'?>
  38. </div>
  39. <div class="col-md-2"></div>
  40. <div class="col-md-5">
  41. <form action="" method="POST">
  42. <div class="form-group">
  43. <label>Username</label>
  44. <input type="text" name="username" class="form-control" required="required"/>
  45. </div>
  46. <div class="form-group">
  47. <label>Password</label>
  48. <input type="password" name="password" class="form-control" required="required"/>
  49. </div>
  50.  
  51. <center><button name="login" class="btn btn-primary">Login</button></center>
  52.  
  53. </form>
  54. <br />
  55. <?php include 'login.php'?>
  56. </div>
  57. </div>
  58.  
  59. </body>
  60. </html>
home.php
  1. <?php
  2. if(!ISSET($_SESSION['user_id'])){
  3. header('location: index.php');
  4. }
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  10. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  11. </head>
  12. <body>
  13. <nav class="navbar navbar-default">
  14. <div class="container-fluid">
  15. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  16. </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class="text-primary">PHP - Login And Registration Source Code</h3>
  21. <hr style="border-top:1px dotted #ccc;"/>
  22. <center><h1>WELCOME USER</h1></center>
  23. <a class="btn btn-danger" href="logout.php">logout</a>
  24. </div>
  25.  
  26. </body>
  27. </html>

Creating The Main Function

This code contains the main function of the application. This code will create a new user account and login the newly created account. To do that just copy and write this block of codes inside the text editor, then save it as shown below. register.php
  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['register'])){
  4. $name=$_POST['name'];
  5. $username=$_POST['username'];
  6. $password=$_POST['password'];
  7.  
  8.  
  9. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$name', '$username', '$password')") or die(mysqli_error());
  10.  
  11.  
  12. echo "<center><h4 class='text-success'>Successfully registered!</h4></center>";
  13. }
  14. ?>
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' && `password`='$password'") or die(mysqli_error());
  9. $fetch=mysqli_fetch_array($query);
  10. $count=mysqli_num_rows($query);
  11.  
  12. if($count > 0){
  13. $_SESSION['user_id']=$fetch['user_id'];
  14. header('location: home.php');
  15. }else{
  16. echo "<div class='alert alert-danger'>Invalid username or password</div>";
  17. }
  18. }
  19. ?>
logout.php
  1. <?php
  2. header('location: index.php');
  3. ?>
There you have it we successfully created Login And Registration 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!

Comments

Submitted byAapy (not verified)on Mon, 06/27/2022 - 03:48

Hi, register.php error line 9 Warning: mysqli_error() expects exactly 1 parameter, 0 given in for reason is error for that error

Add new comment