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?

  1. <?php
  2.  
  3.  
  4. class auth
  5. {
  6.    public $username;
  7.    private $password;
  8.    public $message;
  9.    private $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-> password== $this->encode($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.    
  87.    public function salt()
  88.    {
  89.        return md5 (microtime() . rand(1 , 2000));
  90.    }
  91.    
  92.    public function hash($pass, $salt)
  93.    {
  94.        return md5 ($pass . $salt);
  95.    }
  96.    
  97.    public function encode ($passCompare, $pass, $salt)
  98.    {
  99.        return $passCompare== $this->hash ($pass, $salt);
  100.    }
  101. }
  102.  
  103. ?>