Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Logowanie OOP
Forum PHP.pl > Forum > PHP
borpaw
Próbuje zrobić logowanie do systemu z wykorzystaniem OOP.
Proszę o opinie czy to co robie ma sens.

  1. define( "DB_HOST", "localhost");
  2. define( "DB_NAME", "sys_rdp");
  3. define( "DB_USER", "root");
  4. define( "DB_PASS", "vertrigo");
  5.  
  6. //--------------------------------------------------------------------------------------------------------------------------------------------------
  7.  
  8. class DBconnect{
  9.  
  10. private static $instance;
  11. public $pdo;
  12. private $db_host;
  13. private $db_name;
  14. private $db_user;
  15. private $db_pass;
  16.  
  17. public function __construct($db_host, $db_name, $db_user, $db_pass)
  18. {
  19. try{
  20.  
  21. $this->db_host = $db_host;
  22. $this->db_name = $db_name;
  23. $this->db_user = $db_user;
  24. $this->db_pass = $db_pass;
  25.  
  26. $this->pdo = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  27. }catch(PDOException $e){
  28. exit('Połączenie nie mogło zostać utworzone: ' . $e->getMessage());
  29. }
  30. }
  31.  
  32. private function __clone(){}
  33.  
  34. public static function getInstance ()
  35. {
  36. if (self::$instance === null) {
  37. self::$instance = new DBconnect(DB_HOST, DB_NAME, DB_USER, DB_PASS);
  38. }
  39. return self::$instance;
  40. }
  41.  
  42. }
  43.  
  44. //-------------------------------------------------------------------------------------------------------------------------------------
  45.  
  46. class User
  47. {
  48. public $id = "";
  49. public $login = "";
  50. public $passwd = "";
  51. public $level = "";
  52. public $user_id = 0;
  53. public $email = "";
  54. public $mobile = "";
  55. public $data_add = "";
  56. public $username = "";
  57.  
  58. public function __construct(){}
  59. public function addUser(){}
  60. public function deleteUser(){}
  61. public function updateUser(){}
  62. }
  63. //-------------------------------------------------------------------------------------------------------------------------------------
  64.  
  65. class Authentication
  66. {
  67. private $db;
  68. public $user;
  69.  
  70. public function __construct(){
  71. $this->db = DBconnect::getInstance();
  72. $this->user = new User;
  73. }
  74.  
  75. public function get_param()
  76. {
  77. if( isset($_POST["login"]) and isset($_POST["passwd"]) ) {
  78. $this->user->login = $_POST["login"];
  79. $this->user->passwd = $_POST["passwd"];
  80. $_SESSION["login"] = $this->user->login;
  81. $_SESSION["passwd"] = $this->user->passwd;
  82. return true;
  83. }elseif(isset($_SESSION["login"]) and isset($_SESSION["passwd"]) ){
  84. $this->user->login = $_SESSION["login"];
  85. $this->user->passwd = $_SESSION["passwd"];
  86. return true;
  87. }else{
  88. return false;
  89. }
  90. }
  91.  
  92. public function login()
  93. {
  94. if( $this->get_param() )
  95. {
  96. $sql = "select * FROM users WHERE active = 1 and login = :login and passwd = md5(:passwd)";
  97. $query = $this->db->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  98. $query->bindParam(':login', $this->user->login, PDO::PARAM_STR);
  99. $query->bindParam(':passwd', $this->user->passwd, PDO::PARAM_STR);
  100. $query->execute();
  101. $row = $query->fetch();
  102.  
  103. if( $query->rowCount() > 0 ) {
  104. $this->user->login = "true";
  105. $this->user->level = $row["is_admin"];
  106. $this->user->user_id = $row["id"];
  107. $this->user->username = $row["firstname"]." ".$row["lastname"];
  108. }else{
  109. $this->user->login = "";
  110. $this->user->passwd = "";
  111. $this->user->level = "";
  112. $this->user->user_id = 0;
  113. $this->user->username = "";
  114. }
  115. }
  116. }
  117.  
  118. }
lukaskolista
rzucilem okiem na calosc i ma to sens. Kazdy pewne kwestie rozwiazuje naczej, ale ogolnie jest ok
borpaw
dzieki za opinie, nawiązując do tego, że każdy ma swoje rozwiązania to może byście pokazali jak rozwiązujecie ten fragment serwisu www.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.