<?php
namespace Users;
use mysqli;
class Users {
protected $db;
public function __construct() {
$this->db = new mysqli('localhost', 'root', 'php123', 'sms');
}
public function login($username, $password) {
$query = $this->db->prepare("SELECT username, password FROM users WHERE username=? AND password=? LIMIT 1");
$password = UserData::hash($password);
$query->bind_param('ss', $username, $password);
$query->execute();
$query->bind_result($username, $password);
$query->store_result();
if($query->num_rows ==1) {
while($query->fetch()) {
$_SESSION['loggedin'] = 1;
UserData::getAndSet($username);
header("Location: dashboard.php");
}
} else {
return false;
}
$query->free_result();
$query->close();
}
public function register($username, $password, $rank, $name) {
$errors = array();
$query = $this->db->prepare("INSERT INTO users VALUES('', $username, $password, $rank, $name);");
$password = UserData::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.";
}
$query->execute();
$query->close();
}
}
class UserData {
protected $db;
public $username;
public $password;
public $rank;
public $name;
public function __construct() {
$this->db = new mysqli('localhost', 'root', 'php123', 'sms');
}
public function getAndSet($username) {
$query = $this->db->prepare("SELECT username, password, rank, name FROM users WHERE username=?");
$query->bind_param('s', $username);
$query->execute();
$query->bind_result($username, $password, $rank, $name);
$query->store_result();
while($query->fetch()) {
$this->username = $username;
$this->password = $password;
$this->rank = $rank;
$this->name = $name;
}
$query->free_result();
$query->close();
}
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 UserUtil {
protected $db;
public function __construct() {
$this->db = new mysqli('localhost', 'root', 'php123', 'sms');
}
}
$Users = new Users();
$UserData = new UserData();
$UserUtil = new UserUtil();
?>
This is the beginning of my project, "Student Management System" and before I proceed I want this code to be rated, and how can I improve this code with better ways.