Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klasa Autoryzacji - prośba o opinie
dopelganger
post
Post #1





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Witam,
jestem początkujący w OOP i postanowiłem napisać własną klasę logowania. Poniżej kod klasy i jej użycie. Proszę o opinie, uwagi, będę wdzięczny. Nie wiem czy dobrze rozumię niektóre aspekty oop stąd moje zapytanie.
Dzięki.

  1. // użycie klasy na stronie poniżej, pomijam formularz logowania HTML, jedynie kod PHP wklejam:
  2.  
  3. <?php
  4.  
  5. $user = strip_tags(addslashes(trim($_POST["user"])));
  6. $password = strip_tags(addslashes(trim($_POST["password"])));
  7.  
  8. $log = new Authorization();
  9. $log->setUser($user);
  10. $log->setPassword($password);
  11. $log->Login();
  12.  
  13. ?>


Kod klasy:

  1. <?php
  2.  
  3. class Authorization {
  4.  
  5. private $_user;
  6. private $_password;
  7. private $_dbpdo;
  8.  
  9. public function __construct() {
  10. try
  11. {
  12. global $db;
  13. $pdo = new PDO('mysql:host='.$db["Host"].';dbname='.$db["Name"], $db["User"], $db["Password"]);
  14. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15. $this->_dbpdo = $pdo;
  16. }
  17. catch(PDOException $error)
  18. {
  19. echo $error->getMessage();
  20. }
  21. }
  22.  
  23. public function setUser($user) {
  24. $this->_user = $user;
  25. }
  26.  
  27. public function setPassword($password) {
  28. $this->_password = $password;
  29. }
  30.  
  31. public function getUser() {
  32. return $this->_user;
  33. }
  34.  
  35. public function getPassword() {
  36. return $this->_password;
  37. }
  38.  
  39. public function validateUsr() {
  40. if ( ($this->getUser())=='' || ($this->getPassword())=='' ) {
  41. echo '<p class="errorMsg">Wprowadź login i hasło!</p>';
  42. }
  43. }
  44.  
  45. public function checkIsUsr() {
  46. $password = @md5($this->getPassword());
  47. $sql = $this->_dbpdo->prepare("SELECT * FROM users WHERE login=:user AND password=:password LIMIT 1");
  48. $sql->bindValue(':user',$this->getUser(),PDO::PARAM_STR);
  49. $sql->bindValue(':password',$password,PDO::PARAM_STR);
  50. $sql->execute();
  51. if ($row = $sql->fetch()) {
  52. return array($row['id'], $row['login']);
  53. } else {
  54. echo '<p class="errorMsg">Nieprawidłowy login i/lub hasło!</p>';
  55. }
  56. $sql->closeCursor();
  57. }
  58.  
  59. public function Login() {
  60. $this->validateUsr();
  61. $data_user = $this->checkIsUsr();
  62. $_SESSION['id_usr'] = $data_user[0];
  63. $_SESSION['login_usr'] = $data_user[1];
  64. ini_set('session.cookie_httponly', 1);
  65. }
  66.  
  67. }
  68.  
  69. ?>


Ten post edytował dopelganger 21.08.2013, 09:20:11
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dopelganger
post
Post #2





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


no dobra, troche pozmieniałem, ale też troche sie pogubiłem w tym wszystkim, niby klasa działa, ale ... czuje to "ale" wewnętrzny głos, który mówi mi, że nadal coś może być nie tak. Głównie chodzi mi o usunięcie "echo z klasy. Hmmm wobec tego muszę użyć metod podczas użycia klasy, czy tak? :

  1. <?php
  2.  
  3. $user = strip_tags(trim($_POST["user"]));
  4. $password = strip_tags(trim($_POST["password"]));
  5.  
  6. $log = new Authorization();
  7. $log->setUser($user);
  8. $log->setPassword($password);
  9.  
  10. if ( ($log->validateUsr()) == false ) {
  11. echo '<p class="errorMsg">Wprowadź login i hasło!</p>';
  12. } else if ( ($log->Login()) == false ) {
  13. echo '<p class="errorMsg">Nieprawidłowy login i/lub hasło!</p>';
  14. } else {
  15. echo 'Witaj: '.$_SESSION['login_usr'];
  16. }
  17. ?>


a dalej kod klasy zmieniony wg. wskazówek (oprócz hashowania, to jeszcze badam teren)

  1. <?php
  2.  
  3. class Authorization {
  4.  
  5. private $_user;
  6. private $_password;
  7. private $_dbpdo;
  8.  
  9. public function __construct() {
  10. try
  11. {
  12. global $pdo;
  13. $this->_dbpdo = $pdo;
  14. }
  15. catch(PDOException $error)
  16. {
  17. return $error->getMessage();
  18. }
  19. }
  20.  
  21. public function setUser($user) {
  22. $this->_user = $user;
  23. }
  24.  
  25. public function setPassword($password) {
  26. $this->_password = $password;
  27. }
  28.  
  29. public function getUser() {
  30. return $this->_user;
  31. }
  32.  
  33. public function getPassword() {
  34. return $this->_password;
  35. }
  36.  
  37. public function validateUsr() {
  38. if ( ($this->getUser())=='' || ($this->getPassword())=='' ) {
  39. return false;
  40. } else {
  41. return true;
  42. }
  43. }
  44.  
  45. public function Login() {
  46. $password = md5($this->getPassword());
  47. $sql = $this->_dbpdo->prepare("SELECT * FROM users WHERE login=:user AND password=:password LIMIT 1");
  48. $sql->bindValue(':user',$this->getUser(),PDO::PARAM_STR);
  49. $sql->bindValue(':password',$password,PDO::PARAM_STR);
  50. $sql->execute();
  51. if ($row = $sql->fetch()) {
  52. $_SESSION['id_usr'] = $row['id'];
  53. $_SESSION['login_usr'] = $row['login'];
  54. ini_set('session.cookie_httponly', 1);
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. $sql->closeCursor();
  60. }
  61.  
  62.  
  63. }
  64.  
  65. ?>
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: 17.10.2025 - 20:03