OOP PHP Registration and Login - Part 2
Submitted by razormist on Monday, January 16, 2017 - 20:41.
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
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
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
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
home.php
This is where the user will be directed, after login in correctly
logout.php
This script we will release the stored session, and directed the user to the login page
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!!
- <!DOCTYPE html>
- <html lang = "en">
- <head>
- <meta charset = "UTF-8" name "viewport" content = "width-device=width, initial-scale=1"/>
- <title>OOP PHP Registrarion and Login Form Using MySQLi</title>
- </head>
- <body>
- <nav class = "navbar navbar-default">
- <div class = "container-fluid">
- <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <br />
- <br />
- <br />
- <div class = "row">
- <div class = "col-md-4">
- </div>
- <div class = "col-md-4 well">
- <h4 class = "text-danger">OOP PHP Registration and Login Form Using MySQLi</h4>
- <hr style = "border-top:1px dotted #000;"/>
- <form method = "POST" action = "login_query.php">
- <div class="form-group">
- <input type = "text" placeholder = "Username" name = "username" class = "form-control" required = "required"/>
- </div>
- <div class="form-group">
- <input type = "password" placeholder = "Password" name = "password" class = "form-control" required = "required">
- </div>
- <button class = "btn btn-primary pull-left" name = "login"><span class = "glyphicon glyphicon-log-in"></span> Login</button>
- <label class = "pull-right">Don't have an account yet? <a href = "index.php"> Click here</a></label>
- </form>
- </div>
- </div>
- </body>
- </html>
- <?php
- require 'config.php';
- class db_class{
- public $host = db_host;
- public $user = db_user;
- public $pass = db_pass;
- public $dbname = db_name;
- public $conn;
- public $error;
- public function __construct(){
- $this->connect();
- }
- private function connect(){
- $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
- if(!$this->conn){
- $this->error = "Fatal Error: Can't connect to database".$this->conn->connect_error;
- return false;
- }
- }
- public function save($username, $password, $firstname, $lastname){
- $stmt = $this->conn->prepare("INSERT INTO `user` (username, password, firstname, lastname) VALUES(?, ?, ?, ?)") or die($this->conn->error);
- $stmt->bind_param("ssss", $username, $password, $firstname, $lastname);
- if($stmt->execute()){
- $stmt->close();
- $this->conn->close();
- return true;
- }
- }
- public function login($username, $password){
- $stmt = $this->conn->prepare("SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die($this->conn->error);
- if($stmt->execute()){
- $result = $stmt->get_result();
- $valid = $result->num_rows;
- $fetch = $result->fetch_array();
- 'user_id'=> $fetch['user_id'],
- 'count'=>$valid
- );
- }
- }
- public function user_account($user_id){
- $stmt = $this->conn->prepare("SELECT * FROM `user` WHERE `user_id` = '$user_id'") or die($this->conn->error);
- if($stmt->execute()){
- $result = $stmt->get_result();
- $fetch = $result->fetch_array();
- 'firstname'=> $fetch['firstname'],
- 'lastname'=>$fetch['lastname']
- );
- }
- }
- }
- ?>
- <?php
- require_once 'class.php';
- $conn = new db_class();
- $username = $_POST['username'];
- $password = $_POST['password'];
- $get_user = $conn->login($username, $password);
- if($get_user['count'] > 0){
- $_SESSION['user_id'] = $get_user['user_id'];
- echo '<script>alert("Successfully login!")</script>';
- echo '<script>window.location = "home.php"</script>';
- }else{
- echo '<script>alert("Invalid username or password")</script>';
- echo '<script>window.location = "login.php"</script>';
- }
- }
- ?>
- <?php
- if(!($_SESSION['user_id'])){
- }
- ?>
- <!DOCTYPE html>
- <?php
- require_once 'session.php';
- require 'class.php';
- ?>
- <html lang = "en">
- <head>
- <meta charset = "UTF-8" name "viewport" content = "width-device=width, initial-scale=1"/>
- <title>OOP PHP Registrarion and Login Form Using MySQLi</title>
- <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
- </head>
- <body>
- <nav class = "navbar navbar-default">
- <div class = "container-fluid">
- <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <br />
- <br />
- <br />
- <div class = "row">
- <div class = "col-md-4">
- </div>
- <div class = "col-md-4 well">
- <h4 class = "text-danger">OOP PHP Registration and Login Form Using MySQLi</h4>
- <hr style = "border-top:1px dotted #000;"/>
- <h3>Welcome:</h3>
- <?php
- $user_id = $_SESSION['user_id'];
- $conn = new db_class();
- $user = $conn->user_account($user_id);
- echo '<center><h4 class = "text-success">'.$user['firstname'].' '.$user['lastname'].'</h4></center>';
- ?>
- <a href = "logout.php" >Logout</a>
- </div>
- </div>
- </body>
- </html>
- <?php
- ?>
Add new comment
- 547 views