Computer Science, html5, oop, php, web development

Simple PHP Object Oriented Login

This tutorial shows us how to create a login system complete with sessions from an object oriented perspective.
An Object Oriented Code must by definition fulfill three sturdy pillars of programming:

  • Encapsulation
  • Specialization
  • Polymorphism

The code in this tutorial upholds these requirements.

Overview

We need 5 files

  • connection.php
  • main.class.php
  • sessions.php
  • loginphp
  • login form

Let’s create a connection file connection.php.it will open a path to the database each time the website wants to query for data.

?php   
class GetConnection {

    private $server = 'localhost';
    private $user = 'admin';
    private $pass = '12gbt21!@df_w';
    private $dbname = 'users';

    public function getCon() {
        $conx = null;
        $this->conx = new mysqli($this->server, $this->user, 
                                 $this->pass, $this->dbname);
        if (mysqli_connect_errno()) {
            $output = 'Connection error' . mysqli_connect_error();
            exit();
        }
        
        return $this->conx;
    }

}

Next,we need the main.class.php file.This file is responsible for handling all the actual queries and query results and is the backbone of the login system.
We will also include a sessions.php file used to set user sessions. This file is created later.

?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of main
 *
 * @author nerd
 */
error_reporting(E_ALL);
include 'connection.php';
require_once 'sessions.php';

class main extends GetConnection {

    protected $username;
    protected $userid;

    public function __construct() {
    //connects to the database
        $this->getCon();
    }

    public function __destruct() {
     //closes the database connection after querying is complete
        $this->conx->close();
        
    }

    public function login($user, $pass) {
        $usr = $this->conx->real_escape_string($user);
        $pwd = $this->conx->real_escape_string($pass);
        $sql = $this->conx->query("SELECT * FROM users where lastname='" 
. $usr . "' AND password='" . $pwd . "'");

        $count = $sql->num_rows;
        if ($count == 1) {
            while ($data = $sql->fetch_array()) {


                $userid = $data['uid'];
                $username = '@' . $data['lastname'] . $data['firstname'];
                $t = new sessions();
                $t->getSess($userid, $username);//sets user session
            }
        } else {
            header("Location: index.php");
           
        }
    }

    public function logout() {
        $t = new sessions();
        $t->destroySess();
    }
}

Now we create the session.php class that allows the user to remain logged in even on page reload.

?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
ob_start();
session_start();
class sessions {
    
    public function getSess($userid, $username) {
        $_SESSION['uid'] = $userid;
        $_SESSION['username'] = $username;
        if (isset($_SESSION['uid']) && isset($_SESSION['username'])) {

            header("Location: home.php");
            
        }else {
            header("Location: index.php");
        }
    }
    
    public function destroySess() {
        if(isset($_SESSION['uid']) && isset($_SESSION['username'])){
            session_unset();
            
            session_destroy();
            header("Location: index.php");
        }
    }

}

We now need a login.php file to capture data entered from the webpage and pass it to main.class.php

?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'main.class.php';
require_once 'sessions.php';
$t = new sessions();
 
if(isset($_POST['user']) && isset($_POST['pass'])){
    $user = strip_tags($_POST['user']);
    $pass = strip_tags($_POST['pass']);
    
    $t = new chat();
    $t->login($user, $pass);
}

Last but not least,we need to create the login form

<form name="login" class="login" action="login.php" method="POST">
<input id="username" class="form-control " name="user" type="text" />
<input type="password" class="form-control" id="password" name="pass" />
<input type="submit" id="modal-submit"   name="login" />
<form />
Standard