I am currently making a very big blogging system for my Website where I want really neat code! So, I was wondering: is this method safe/does it have any problems with security? Yes, I'll be encrypting the password anyway. I also want to know if there is a better way to achieve the same idea. Also any tips on implementing high security and high performance?
<?php
class Users {
protected $db;
public $id;
public $username;
public $password;
public $lastlogin;
public $email;
public $rank;
public $creationdate;
public $country;
public $status;
public function __construct() {
$this->db = new mysqli('localhost', 'root', 'php123', 'ht');
}
public function login($username, $password) {
$query = $this->db->prepare("SELECT username, password FROM blog_users WHERE username = ? AND password = ? LIMIT 1");
$query->bind_param('ss', $username, $password);
$query->execute();
$query->bind_result($username, $password);
$query->store_result();
if($query->num_rows == 1) {
while($query->fetch()) {
$userData = getData($username);
$this->id = $this->userData['id'];
$this->username = $username;
$this->password = $password;
$this->lastlogin = time();
$this->email = $this->userData['email'];
$this->rank = $this->userData['rank'];
$this->creationdate = $this->userData['creationdate'];
$this->country = $this->userData['country'];
$this->status = $this->userData['status'];
$_SESSION['loggedin'] = 1;
return true;
}
} else {
return false;
}
$query->close();
$query->free_result();
}
public function getData($username) {
$query = $this->db->prepare("SELECT id, email, rank, creationdate, country, status FROM blog_users WHERE username = ?");
$query->bind_param('s', $username);
$query->execute();
$query->bind_result($id, $email, $rank, $creationdate, $country, $status);
$query->store_result();
$userData = array(
'id' => $id,
'email' => $email,
'rank' => $rank,
'creationdate' => $creationdate,
'country' => $country,
'status' => $status
);
return $userData;
$query->close();
$query->free_result;
}
}
?>
$this->db = $db;
when$db
is not in scope, typo's: (likeeles
instead ofelse
). Debug your code first, before you can get it reviewed \$\endgroup\$$db
, but still: the code, as posted here, doesn't work. You are required to post working code to get a review here. That was my point \$\endgroup\$login
method, there's a call to thegetData
method, but you wrotegetData($username);
when it should've been$this->userName
\$\endgroup\$