I have just started to try and learn 'OOP' but it appears I'm doing this wrong, according to the people on StackOverflow the code below is far from Object Orientated, but I'm finding it hard as I'm self teaching my self and everyone does everything different ways.
I'm building a shopping cart and I need to be able to have both the admin and user login so obviously both user types can use the same login and logout functions, so I'm guessing I could use inheritance there, but I just need some one to tell me what I have done wrong or if anything is done right to confirm this. Below is my create user page and my database and user class.
//Sanitize User Input
$username = $database->sanitize_admin_input($_POST['username']);
$password = $database->sanitize_admin_input($_POST['password']);
//Check if user already exists.
$exists = $database->check_user_exists($username);
//Creates a salt, then passes the salt in to create a secure password.
$salt = $database->createSalt();
//Create the password hash using the salt.
$password_hash = $database->make_password_hash($password,$salt);
//Add Admin user.
$admin->add_admin($username, $password_hash, $salt);
Admin Class
<?php
require_once('../includes/class.database.php');
class Admin
{
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public $number_of_users; //Holds the number of users in the DB.
function __construct()
{
$this->number_of_users = $this->number_of_users();
}
private function number_of_users()
{
global $database;
$results = $database->query("SELECT COUNT(*) FROM users");
$rows = mysqli_fetch_row($results);
$number = $rows[0];
return $number;
}
public function add_admin($username,$password_hash,$salt)
{
global $database;
$result = $database->query("INSERT INTO users (username, password, salt ) VALUES ('{$username}','{$password_hash}','{$salt}')");
return $result;
}
}//END OF CLASS
?>
Database Class
<?php
require_once('config.php');
class Database
{
private $connection;
public $last_query; //stores the lastquerys, measn we can call this from the DB.
function __construct()
{
$this->open_connection();
}
public function open_connection()
{
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(!$this->connection)
{
$output = "A connection to the database has failed, please check the config file.";
$output .= "<br />" . mysqli_connect_error();
die($output);
}
}
public function close_connection()
{
if(isset($this->connection))
{
mysqli_close($this->connection);
unset($this->connection);
}
}
public function query($sql) //Takes in a paramater (sql query)
{
$this->last_query = $sql;
$result = mysqli_query($this->connection,$sql);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result)
{
if(!$result)
{
//IF the result failes, takes the connection error and displays that, on a new line it displays the last query used.
$output = "This query has failed" . mysqli_error($this->connection);
$output .= '<br />' . $this->last_query;
die($output); //Kills the script and outputs the error message
}
}
public function sanitize_admin_input($data)
{
$data = trim($data);
$data = filter_var($data, FILTER_SANITIZE_STRING);
$data = ereg_replace("[^A-Za-z0-9]", "", $data );
return $data;
}
public function check_user_exists($username)
{
global $database;
$results = $database->query("SELECT * FROM users WHERE 'username' ='{$username}'");
$row_cnt = $results->num_rows;
return $row_cnt;
}
public function make_password_hash($password,$salt)
{
$hash = hash('sha256', $password);
$password_hash = hash('sha256', $salt . $hash);
return $password_hash;
}
public function createSalt()
{
$string = md5(uniqid(rand(), true));
return substr($string, 0, 9);
}
}
$database = new Database();
?>
REVIEWED CODE
function __construct( Database $database, $username, $password=NULL)
{
$this->connection = $database;
$this->password = $password;
$this->check_user_exists($username);
}
Index.php
require_once('class.userAction.php');
require_once('class.database.php');
$database = new Database();
$userOne = new userActions($database, 'marinello12','2312');
$userOne->create();