OOP PHP Registration and Login - Part 2

In this tutorial we will create a simple login form in an Object Oriented Programming(OOP). In my last tutorial I created a registration form operated by using PHP functions, but this time we will continue on for what we left behind. Before we proceed make sure you have read my previous tutorial OOP PHP Registration and Login - Part 1, because I will still using that, and I will add some new stuff to make this simple program complete. So lets take a look and start coding From my previous tutorial I'd already created the form for registration of user. login.php In this form, it contains all the form for login that will be use for retrieving the stored data from database
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <meta charset = "UTF-8" name "viewport" content = "width-device=width, initial-scale=1"/>
  5. <title>OOP PHP Registrarion and Login Form Using MySQLi</title>
  6. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  7. </head>
  8. <body>
  9. <nav class = "navbar navbar-default">
  10. <div class = "container-fluid">
  11. <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <br />
  15. <br />
  16. <br />
  17. <div class = "row">
  18. <div class = "col-md-4">
  19. </div>
  20. <div class = "col-md-4 well">
  21. <h4 class = "text-danger">OOP PHP Registration and Login Form Using MySQLi</h4>
  22. <hr style = "border-top:1px dotted #000;"/>
  23. <form method = "POST" action = "login_query.php">
  24. <div class="form-group">
  25. <input type = "text" placeholder = "Username" name = "username" class = "form-control" required = "required"/>
  26. </div>
  27. <div class="form-group">
  28. <input type = "password" placeholder = "Password" name = "password" class = "form-control" required = "required">
  29. </div>
  30. <button class = "btn btn-primary pull-left" name = "login"><span class = "glyphicon glyphicon-log-in"></span> Login</button>
  31. <label class = "pull-right">Don't have an account yet? <a href = "index.php"> Click here</a></label>
  32. </form>
  33. </div>
  34. </div>
  35. </body>
  36. </html>
If you have downloaded the script of in the previous tutorial, just open "class.php" and add the new function, copy/paste 'login()' and 'user_account()' function that I have given below, if not just copy/paste the whole code below then name it 'class.php' class.php
  1. <?php
  2. require 'config.php';
  3.  
  4. class db_class{
  5. public $host = db_host;
  6. public $user = db_user;
  7. public $pass = db_pass;
  8. public $dbname = db_name;
  9. public $conn;
  10. public $error;
  11.  
  12. public function __construct(){
  13. $this->connect();
  14. }
  15.  
  16. private function connect(){
  17. $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
  18. if(!$this->conn){
  19. $this->error = "Fatal Error: Can't connect to database".$this->conn->connect_error;
  20. return false;
  21. }
  22. }
  23.  
  24. public function save($username, $password, $firstname, $lastname){
  25. $stmt = $this->conn->prepare("INSERT INTO `user` (username, password, firstname, lastname) VALUES(?, ?, ?, ?)") or die($this->conn->error);
  26. $stmt->bind_param("ssss", $username, $password, $firstname, $lastname);
  27. if($stmt->execute()){
  28. $stmt->close();
  29. $this->conn->close();
  30. return true;
  31. }
  32. }
  33.  
  34. public function login($username, $password){
  35. $stmt = $this->conn->prepare("SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die($this->conn->error);
  36. if($stmt->execute()){
  37. $result = $stmt->get_result();
  38. $valid = $result->num_rows;
  39. $fetch = $result->fetch_array();
  40. return array(
  41. 'user_id'=> $fetch['user_id'],
  42. 'count'=>$valid
  43. );
  44. }
  45. }
  46.  
  47. public function user_account($user_id){
  48. $stmt = $this->conn->prepare("SELECT * FROM `user` WHERE `user_id` = '$user_id'") or die($this->conn->error);
  49. if($stmt->execute()){
  50. $result = $stmt->get_result();
  51. $fetch = $result->fetch_array();
  52. return array(
  53. 'firstname'=> $fetch['firstname'],
  54. 'lastname'=>$fetch['lastname']
  55. );
  56. }
  57. }
  58. }
  59. ?>
Creating login query login_query.php This script will hold the value of form when triggered by the button, then will call the function 'login()' to retrieve the requested value from the database
  1. <?php
  2. require_once 'class.php';
  3. if(ISSET($_POST['login'])){
  4. $conn = new db_class();
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7. $get_user = $conn->login($username, $password);
  8. if($get_user['count'] > 0){
  9. $_SESSION['user_id'] = $get_user['user_id'];
  10. echo '<script>alert("Successfully login!")</script>';
  11. echo '<script>window.location = "home.php"</script>';
  12. }else{
  13. echo '<script>alert("Invalid username or password")</script>';
  14. echo '<script>window.location = "login.php"</script>';
  15. }
  16. }
  17. ?>
session.php This code will check if the session have already stored an existing variable, if not then it will directly forced user back to login page
  1. <?php
  2. if(!($_SESSION['user_id'])){
  3. header('location:login.php');
  4. }
  5. ?>
home.php This is where the user will be directed, after login in correctly
  1. <!DOCTYPE html>
  2. <?php
  3. require_once 'session.php';
  4. require 'class.php';
  5. ?>
  6. <html lang = "en">
  7. <head>
  8. <meta charset = "UTF-8" name "viewport" content = "width-device=width, initial-scale=1"/>
  9. <title>OOP PHP Registrarion and Login Form Using MySQLi</title>
  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://www.sourcecodester.com">Sourcecodester</a>
  16. </div>
  17. </nav>
  18. <br />
  19. <br />
  20. <br />
  21. <div class = "row">
  22. <div class = "col-md-4">
  23. </div>
  24. <div class = "col-md-4 well">
  25. <h4 class = "text-danger">OOP PHP Registration and Login Form Using MySQLi</h4>
  26. <hr style = "border-top:1px dotted #000;"/>
  27. <h3>Welcome:</h3>
  28. <?php
  29. $user_id = $_SESSION['user_id'];
  30. $conn = new db_class();
  31. $user = $conn->user_account($user_id);
  32. echo '<center><h4 class = "text-success">'.$user['firstname'].' '.$user['lastname'].'</h4></center>';
  33. ?>
  34. <a href = "logout.php" >Logout</a>
  35. </div>
  36. </div>
  37. </body>
  38. </html>
logout.php This script we will release the stored session, and directed the user to the login page
  1. <?php
  2. session_unset($_SESSION['user_id']);
  3. header('location:login.php');
  4. ?>
There you have it we created the simple login form using OOP PHP. I hope you learned from this tutorial, and applied this to your project. For more updates and tutorial, just kindly visit this site. Enjoy Coding!!

Tags

Add new comment