Old Post: Classes for user registration and authentication
This post is an update of the old post of mine. This is the code after suggestions were implemented in the review. Do you think I could make this even better?
Please criticize as thoroughly as possible for the smallest thing possible as it will be very useful for me.
<?php
namespace Users;
use mysqli;
class UserService {
public function handleUserLogin($username, $password) {
if($userMapper->checkLogin($username, $password) === true) {
$userMapper->fetch($username);
$userMapper->createSession($username);
return true;
} else {
return false;
}
}
public function handleUserCreation($username, $password, $rank, $name) {
if($userMapper->newUserCreation($username, $password, $rank, $name) === true) {
return true;
} else {
$errors['count'] = count($errors);
return $errors;
}
}
}
class User {
public $id;
public $username;
public $password;
public $rank;
public $name;
public function __construct($id, $username, $password, $rank, $name) {
$this->id = $id;
$this->username = $username;
$this->password = $password;
$this->rank = $rank;
$this->name = $name;
}
}
class UserMapper {
private $db;
public function __construct(DB $db) {
$this->db = $db;
}
public function checkLogin($username, $password) {
$query = $this->db->prepare("SELECT username, password FROM users WHERE username=? AND password=? LIMIT 1");
$password = $this->hash($password);
$query->bind_param('ss', $username, $password);
$query->execute();
$query->bind_result($username, $password);
$query->store_result();
$query->num_rows == 1 ? true : false;
$query->free_result();
$query->close();
}
public function fetch($username) {
$query = $this->db->prepare("SELECT id, username, password, rank, name FROM users WHERE username=? LIMIT 1");
$query->bind_param('s', $username);
$query->execute();
$query->bind_result($id, $username, $password, $rank, $name);
$query->store_result();
while($query->fetch()) {
$user = new User($id, $username, $password, $rank, $name);
}
$query->free_result();
$query->close();
}
public function createSession($username) {
$_SESSION['logged_in'] = 1;
$_SESSION['username'] = $username;
header("Location: dashboard.php");
}
public function newUserCreation($username, $password, $rank, $name) {
$errors = array();
$query = $this->db->prepare("INSERT INTO users VALUES('', $username, $password, $rank, $name);");
$password = $this->hash($password);
$query->bind_param('ssss', $username, $password, $rank, $name);
if(strlen($username) < 3 || strlen($username) > 15) {
$errors[] = "The length of the username can only range from 3 to 15 characters.";
}
if(strlen($password) < 3 || strlen($password) > 15) {
$errors[] = "The length of the password can only range from 3 to 15 characters.";
}
if(strlen($name) < 3 || strlen($name) > 15) {
$errors[] = "The length of the name can only range from 3 to 15 characters.";
}
if(count($errors) > 0) {
return $errors;
exit();
} else {
$query->execute();
$query->close();
return true;
}
}
public function hash($string) {
$fSalt = "{z6Q9&2eHn69,9/cXa=<,Nm[?^^Tw-";
$sSalt = ":L+;fwS<q%)3f?N_d)(Ta_*fN&ax}+";
$sting = md5(md5($fSalt) . md5($string) . md5($sSalt));
for($i=0; $i < 3; $i++) {
$string = md5($string);
$string = sha1($string);
$string = bin2hex($string);
$string = md5($string);
}
return $string;
}
}
class DB {
public $db;
public function __construct() {
$this->db = new mysqli('localhost', 'root', 'php123', 'sms');
}
}
$userService = new UserService();
$db = new DB();
$userMapper = new UserMapper($db);
?>