Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> PHP PDO password_verify nie działa
alberthus
post
Post #1





Grupa: Zarejestrowani
Postów: 31
Pomógł: 0
Dołączył: 16.01.2014

Ostrzeżenie: (0%)
-----


Witam

Mam problem z logowanie, ponieważ nie wiem jak odczytać zaszyfrowane hasło.

W rejestracji wrzucam je tym:
  1. $new_password = password_hash($this->password, PASSWORD_DEFAULT);


W logowaniu próbuję wybrać i odczytać tym ale nie przepuszcza. Hasła nie szyfrowane loguje normalnie.

  1. function logowanie(){
  2.  
  3.  
  4. try{
  5.  
  6. $query = $this->pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
  7.  
  8. $query->execute(array(':username'=>$this->username,':password'=>$this->password));
  9.  
  10. }catch(Exception $e){
  11.  
  12. echo $e->getMessage();
  13. }
  14.  
  15. $wynik = $query->fetch(PDO::FETCH_ASSOC);
  16.  
  17.  
  18. if(password_verify($this->password, $wynik['password'])){
  19.  
  20.  
  21. return TRUE;
  22.  
  23. }else{
  24. throw new Exception("error");
  25. }
  26.  
  27. }


Jak to poprawić?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
alberthus
post
Post #2





Grupa: Zarejestrowani
Postów: 31
Pomógł: 0
Dołączył: 16.01.2014

Ostrzeżenie: (0%)
-----


  1. class Login {
  2.  
  3. private $username;
  4. private $password;
  5. private $con;
  6.  
  7. function __construct($username, $password) {
  8.  
  9. $this->setData($username, $password);
  10. $this->connectToDb();
  11. $this->getData();
  12. }
  13.  
  14. private function setData($username, $password) {
  15. $this->username = $username;
  16. $this->password = $password;
  17. }
  18.  
  19. private function connectToDb(){
  20.  
  21. include 'Database.php';
  22.  
  23. $this->con = new Database();
  24.  
  25. }
  26.  
  27.  
  28.  
  29. function getData(){
  30.  
  31. try{
  32.  
  33. $query = $this->con->prepare("SELECT * FROM users WHERE username = :username");
  34.  
  35. $query->execute(array(':username'=>$this->username));
  36.  
  37. $wynik = $query->fetch(PDO::FETCH_ASSOC);
  38.  
  39.  
  40. //var_dump($wynik)
  41. //var_dump($this->password);
  42. //exit;
  43.  
  44. }catch(Exception $exc){
  45.  
  46. echo $exc->getTraceAsString();
  47. }
  48.  
  49.  
  50.  
  51. if($query->rowCount() > 0){
  52.  
  53.  
  54. if(password_verify($this->password , $wynik['password'])){
  55.  
  56. echo "Password Valid!";
  57.  
  58. }else{
  59. echo "Password invalid!";
  60. }
  61. }else{
  62. throw new Exception("Login lub hasło są niepoprawne");
  63. }
  64.  
  65. }
  66.  
  67. function close(){
  68. $this->con->close();
  69. }
  70.  
  71. }
  72.  


a w kontrolerze tworze obiekt.

  1. try{
  2. $login = new Login($username, $password);
  3.  
  4. if($login == TRUE){
  5.  
  6. $_SESSION['username'] = $username;
  7.  
  8. }else{
  9. $_SESSION['username'] = false;
  10. }
  11.  
  12. }catch (Exception $exc){
  13.  
  14. echo $exc->getMessage();
  15. }
  16.  


chodzi o to że loguje mnie z dowolnym hasłem.

przy rejestracji mam:
  1. function rgisterUser(){
  2.  
  3. $sql = "INSERT INTO users (username, password, email) VALUES (:username,:password,:email)";
  4.  
  5. try{
  6.  
  7. $new_password = password_hash($this->password, PASSWORD_BCRYPT);
  8.  
  9. $query = $this->con->pdo->prepare($sql);
  10.  
  11. $query->bindparam(":username", $this->username);
  12. $query->bindparam(":email", $this->email);
  13. $query->bindparam(":password", $new_password);
  14. $query->execute();
  15.  
  16.  
  17. }catch(Exception $exc){
  18.  
  19. echo $exc->getTraceAsString();
  20. }
  21.  
  22.  
  23. }


Ps. to pilotażowy projekt do nauki PDO, niekomercyjny
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.12.2025 - 23:59