Mam mały problem z klasą do solenia. Ściągnąłem klasę do solenia haseł Athlana (http://athlan.pl/code/PassSalt), ale kiedy chcę się zalogować to w ogóle nie chce tego robić. Wyświetla mi komunikat z else: "Invalid username or password". Dlaczego to nie działa? Czy jest to wina złego sprawdzania hasła w if'ie $this->compare() ?
Jak taki warunek stworzyć, żeby metoda porównująca sól z hasłem chodziła i żeby logowało?
<?php
class auth
{
public $username;
private $password;
public $message;
public $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->compare($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;
}
// solenie hasła <a href=\"http://athlan.pl/code/PassSalt\" target=\"_blank\">http://athlan.pl/code/PassSalt</a>
public function salt($sPass)
{
}
public function encode($sPass, $sSalt)
{
return md5($sSalt . md5($sPass . $sSalt)); }
public function compare($sPassCompare, $sPass, $sSalt)
{
return ($sPassCompare == $this-> encode($sPass, $sSalt));
}
}
?>