Prawię kończę już klasę do logowania, jednak natrafiłem na problem. Kiedy dochodzi do sprawdzania hasła z formularza z tym z bazy, nie loguje mnie, chyba przez to, że hasło z bazy nie jest takie samo jak to z formularza + sól z bazy. Jak myślicie czy to jest przyczyną, czy coś innego?
<?php
class auth
{
public $username;
private $password;
public $message;
private $salt;
private $pdo;
public function __construct()
{
$this-> pdo= new PDO ('mysql:host=localhost;dbname=trial', 'root', '');
}
public function get()
{
$stmt= $this->pdo -> prepare ('SELECT name,password,salt FROM experiment WHERE name=:name ');
$stmt-> bindValue (':name', $_POST['username'], PDO::PARAM_STR);
$stmt-> execute();
while ($row= $stmt-> fetch())
{
$this-> username= $row['name'];
$this-> password= $row['password'];
$this-> salt= $row['salt'];
}
$stmt-> closeCursor();
}
public function login($login)
{
if (! empty($login) && ! empty($_POST['password'])) {
if ($this-> username== $login && $this-> password== $this->encode($this->password, $_POST['password'], $this->salt))
{
$_SESSION['user']= $this-> username;
$this-> message= 'Welcome ' . $_SESSION['user'] . ' <a href="users.php?signout=yes">sign out</a>';
}
else
{
$this-> message= 'Invalid username or password';
}
}
else
{
$this-> message= 'Fill in all fields';
}
}
public function signout()
{
if ($_GET['signout']== "yes")
{
unset ($_SESSION['user']); header ("Location: login.php"); }
}
public function register($name, $password, $email, $salt)
{
if (! empty($_POST['username']) && ! empty($_POST['password']) && ! empty($_POST['email'])) {
$stmt= $this->pdo -> prepare ('INSERT INTO experiment (name,password,email,salt)
VALUES (:name,:password,:email,:salt) ');
$stmt-> bindValue (':salt', $salt ,PDO::PARAM_STR);
$stmt-> execute();
$stmt-> closeCursor();
}
else
{
$this-> message= 'Fill in all fields';
}
}
public function show()
{
return $this-> message;
}
public function salt()
{
}
public function hash($pass, $salt)
{
return md5 ($pass . $salt); }
public function encode ($passCompare, $pass, $salt)
{
return $passCompare== $this->hash ($pass, $salt);
}
}
?>