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?

  1. <?php
  2.  
  3.  
  4. class auth
  5. {
  6.    public $username;
  7.    private $password;
  8.    public $message;
  9.    public $salt;
  10.    private $pdo;
  11.    
  12.    public function __construct()
  13.    {
  14.        $this-> pdo= new PDO ('mysql:host=localhost;dbname=trial', 'root', '');
  15.    }
  16.    
  17.    public function get()
  18.    {
  19.        
  20.        $stmt= $this->pdo -> prepare ('SELECT name,password,salt FROM experiment WHERE name=:name ');
  21.        $stmt-> bindValue (':name', $_POST['username'], PDO::PARAM_STR);
  22.        $stmt-> execute();
  23.        
  24.        while ($row= $stmt-> fetch())
  25.        {
  26.            $this-> username= $row['name'];
  27.            $this-> password= $row['password'];
  28.            $this-> salt= $row['salt'];
  29.        }
  30.        $stmt-> closeCursor();
  31.    }
  32.    
  33.    public function login($login)
  34.    {
  35.        if (! empty($login) && ! empty($_POST['password']))
  36.        {
  37.            if ($this-> username== $login && $this->compare($this->password, $_POST['password'], $this->salt))
  38.            {
  39.                $_SESSION['user']= $this-> username;
  40.                $this-> message= 'Welcome ' . $_SESSION['user'] . ' <a href="users.php?signout=yes">sign out</a>';
  41.            }
  42.            else
  43.            {
  44.                $this-> message= 'Invalid username or password';
  45.            }
  46.        }
  47.        else
  48.        {
  49.            $this-> message= 'Fill in all fields';
  50.        }
  51.    }
  52.    
  53.    public function signout()
  54.    {
  55.       if ($_GET['signout']== "yes")
  56.       {
  57.            unset ($_SESSION['user']);
  58.            session_destroy();
  59.            header ("Location: login.php");
  60.       }
  61.    }
  62.    
  63.    public function register($name, $password, $email, $salt)
  64.    {
  65.        if (! empty($_POST['username']) && ! empty($_POST['password']) && ! empty($_POST['email']))
  66.        {
  67.            $stmt= $this->pdo -> prepare ('INSERT INTO experiment (name,password,email,salt)
  68.            VALUES (:name,:password,:email,:salt) ');
  69.            $stmt-> bindValue (':name', htmlspecialchars($name), PDO::PARAM_STR);
  70.            $stmt-> bindValue (':password', htmlspecialchars($password) , PDO::PARAM_STR);
  71.            $stmt-> bindValue (':email', htmlspecialchars($email), PDO::PARAM_STR);
  72.            $stmt-> bindValue (':salt', $salt ,PDO::PARAM_STR);
  73.            $stmt-> execute();
  74.            $stmt-> closeCursor();
  75.        }
  76.        else
  77.        {
  78.            $this-> message= 'Fill in all fields';
  79.        }
  80.    }
  81.    
  82.    public function show()
  83.    {
  84.        return $this-> message;
  85.    }
  86.    // solenie hasła <a href=\"http://athlan.pl/code/PassSalt\" target=\"_blank\">http://athlan.pl/code/PassSalt</a>
  87.       public function salt($sPass)
  88.    {
  89.        return substr(md5(microtime() . substr($sPass, 0, 3)), 0, 5);
  90.    }
  91.    
  92.    public function encode($sPass, $sSalt)
  93.    {
  94.        return md5($sSalt . md5($sPass . $sSalt));
  95.    }
  96.    
  97.    public function compare($sPassCompare, $sPass, $sSalt)
  98.    {
  99.        return ($sPassCompare == $this-> encode($sPass, $sSalt));
  100.    }
  101. }
  102.  
  103. ?>