class Login {
private $username;
private $password;
private $con;
function __construct($username, $password) {
$this->setData($username, $password);
$this->connectToDb();
$this->getData();
}
private function setData($username, $password) {
$this->username = $username;
$this->password = $password;
}
private function connectToDb(){
include 'Database.php';
$this->con = new Database();
}
function getData(){
try{
$query = $this->con->prepare("SELECT * FROM users WHERE username = :username");
$query->execute(array(':username'=>$this->username));
$wynik = $query->fetch(PDO::FETCH_ASSOC);
//var_dump($wynik)
//var_dump($this->password);
//exit;
}catch(Exception $exc){
echo $exc->getTraceAsString(); }
if($query->rowCount() > 0){
if(password_verify($this->password , $wynik['password'])){
}else{
echo "Password invalid!"; }
}else{
throw new Exception("Login lub hasło są niepoprawne");
}
}
function close(){
$this->con->close();
}
}
a w kontrolerze tworze obiekt.
try{
$login = new Login($username, $password);
if($login == TRUE){
$_SESSION['username'] = $username;
}else{
$_SESSION['username'] = false;
}
}catch (Exception $exc){
}
chodzi o to że loguje mnie z dowolnym hasłem.
przy rejestracji mam:
function rgisterUser(){
$sql = "INSERT INTO users (username, password, email) VALUES (:username,:password,:email)";
try{
$new_password = password_hash($this->password, PASSWORD_BCRYPT);
$query = $this->con->pdo->prepare($sql);
$query->bindparam(":username", $this->username);
$query->bindparam(":email", $this->email);
$query->bindparam(":password", $new_password);
$query->execute();
}catch(Exception $exc){
echo $exc->getTraceAsString(); }
}
Ps. to pilotażowy projekt do nauki PDO, niekomercyjny