Saturday, 21 July 2018

Object Oriented PHP Login Script with mysql database

Good Day Everyone, Sorry if i made a lot of mistakes in my code but i am new to object oriented programming in PHP as i heard it is easily readable and organizes code.
I am trying a first project by working on a Login Script with mysql database.
The issue is i have written my functions but can seem to get it to work and i am not getting any errors to use to debug. Below are my codes.
I have a database.php file that contains most functions
class Database 
{
    //Database conn properties

    private $host   = 'localhost';
    private $user   = 'root';
    private $pass   = 'password';
    private $dbname = 'rtmdb';

    private $dbh;
    private $error;
    private $stmt;

    public function __construct() 
    {
        //Function for database connection
        //Set DSN

        $dsn = 'mysql:host='. $this->host . ';dbname'. $this->dbname;

        //Set Options include persistent connection

        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );

        //Create new PDO Instance

        try
        {
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        catch(PDOException $e) 
        {
            $this->error = $e->getMessage();
        }
    }

    public function query($query)
    {
        //@param function for executing insert, select, update

        $this->stmt = $this->dbh->prepare($query);

        if(!$this->stmt)
        {
            echo $this->dbh->lastErrorMsg();
        }
        else
        {
            return $this->stmt = $this->dbh->prepare($query);
        }
    }

    public function bind($param, $value, $type = null) 
    {
        if(is_null($type))
        {
            switch(true)
            {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                    default;
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function clean_str($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        $data = str_replace("'", "’", $data);
        return $data;
    }

    public function execute()
    {
        return $this->stmt->execute();
    }

    public function lastInsertId() 
    {
        $this->dbh->lastInsertId();
    }

    public function resultset()
    {
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSCO);
    }

    public function registerAdmin($fname, $lname, $oname, $uname, $email, $idnumber,
 $pass, $profimg, $status)
    {
        $email = $this->clean_str($email);
        $lname = $this->clean_str($lname);
        $email = $this->clean_str($oname);
        $lname = $this->clean_str($uname);
        $email = $this->clean_str($email);
        $lname = $this->clean_str($idnumber);
        $email = $this->clean_str($pass);
        $lname = $this->clean_str($profimg);
        $email = $this->clean_str($status);

        $database->query('INSERT INTO admin (fname, lname, oname, uname, 
email, idnumber, pass, profimg, status) VALUES(:fname, :lname, :oname, :uname,
 :email, :idnumber, :pass, :proofimg, :status)');

        $database->bind(':fname', $fname);
        $database->bind(':lname', $lname);
        $database->bind(':oname', $oname);
        $database->bind(':uname', $uname);
        $database->bind(':email', $email);
        $database->bind(':idnumber', $idnumber);
        $database->bind(':pass', $pass);
        $database->bind(':profimg', $profimg);
        $database->bind(':status', $status);

        $database->execute();

        if(!$database->lastInsertId())
        {
            die('Yawa Don Gas: ' . $this->dbh->lastErrorMsg());
        }
        $this->dbh->close();
    }
    public function loginAdmin($uname, $pass)
    {
        $uname = $this->clean_str($uame);
        $pass = $this->clean_str($pass);

        $database->query('SELECT * FROM admin WHERE uname = :uname AND
 pass = :pass');

        $database->bind(':uname', $uname);
        $database->bind(':pass', $pass);

        $results = $database->execute();
        $count = mysql_num_rows($results);

        if ($count == 1)
        {
            $rows = $database->resultset();

            foreach($rows as $row)
            {
                $id = $row['id'];
                $uname = $row['uname'];
                $pass = $row['pass'];
            }
            if(!isset($uname) or empty($uname))
            {
                echo 'Invalid Usernmae';
            }
            elseif(!isset($pass) or empty($pass))
            {
                echo 'Invalid Password Details';
            }
            else
            {
                echo 'Good';
                $set = $this->crossEncryption(ENCRYPT_KEY, 10).$id;
                setcookie('itravel', $set, time()+COOKIE_EXPIRE, COOKIE_PATH);
            }
            return;
             $this->dbh->close();
        }
    }

    public function crossEncryption($key,$length)
    {
        $characters = $key;
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    public function logout()
    {
        if(isset($_SESSION['uname']))
        {
            unset($_SESSION['uname']['id']);
            session_destroy();
            header('Location: index.php');
        }
    }
}

I have another travelapis.php file
<?php
require_once 'database.php';

class travelapis
{
    public function __construct() 
    {
        $this->dbh = new Database;
    }

    public function login()
    {
        if(isset($_POST['uname']))
        {
            $uname = $_POST['username'];
            $pass = $_POST['password'];

            if(empty($uname) || empty($pass))
            {
                echo 'Please Fill in all Fields';
            }
            else
            {
                $this->dbh->login($uname, $pass);
            }
        }
    }
}

Then also a login.php file to carry out the function
<?php

require_once "travelapis.php";

$api = new travelapis;

$api->login();

This is used in the form action. For some reason i really don't understand, this doesn't seem to work. Any help would to as i need to wrap my head around OOP with PHP


from Object Oriented PHP Login Script with mysql database

No comments:

Post a Comment