Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> obiektowy system autoryzacji
tab
post
Post #1





Grupa: Zarejestrowani
Postów: 75
Pomógł: 20
Dołączył: 8.10.2012

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


troche mi zajelo zeby ogarnac ten caly oop, wiec jak juz mi sie w koncu udalo to postanowilem sobie cos napisac. moglibyscie to ocenic pod katem bezpieczenstwa i wydajnosci? pozdrawiam

index.php
  1. <?php ob_start() ?>
  2.  
  3. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  4. <a href="index.php">Logowanie</a> <a href="reg.php">Rejestracja</a>
  5.  
  6. <form action="index.php" method="post">
  7. Login: <input type="text" name="login"><br>
  8. Hasło: <input type="password" name="passwd"><br>
  9. <input type="submit" name="log" value="Zaloguj">
  10. </form>
  11.  
  12. <?php
  13. class User
  14. {
  15. private $_login;
  16. private $_passwd;
  17. private $_id;
  18.  
  19. public function __get ($name)
  20. {
  21. return $this->getValue($name);
  22. }
  23.  
  24. public function __construct ($login,$passwd)
  25. {
  26. $this->_login = $login;
  27. $this->_passwd = sha1($passwd);
  28. }
  29.  
  30. public function getValue ($name)
  31. {
  32. return $this->$name;
  33. }
  34.  
  35. public function auth ()
  36. {
  37. $pdo = new PDO('mysql:host=localhost;dbname=DBNAME', 'LOGIN', 'HASLO');
  38. $stmt=$pdo->query("SELECT id FROM users WHERE login='$this->_login' and passwd='$this->_passwd'");
  39. $res=$stmt->fetch(PDO::FETCH_ASSOC);
  40. $stmt->closeCursor();
  41.  
  42. $this->_id = $res['id'];
  43.  
  44. if (empty($res)) die('Niepoprawne dane');
  45. }
  46.  
  47. public function logme ()
  48. {
  49.  
  50. if (!isset($_SESSION['init']))
  51. {
  52. $_SESSION['init'] = true;
  53. }
  54.  
  55. $_SESSION['id'] = $this->_id;
  56. $_SESSION['login'] = $this->_login;
  57. header('Location: main.php');
  58. }
  59. }
  60.  
  61. if (isset($_POST['log']))
  62. {
  63. $user = new User(strip_tags(trim($_POST['login'])),($_POST['passwd']));
  64. $user->auth();
  65. $user->logme();
  66. }
  67. ?>

reg.php
  1. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  2. <a href="index.php">Logowanie</a> <a href="reg.php">Rejestracja</a>
  3.  
  4. <form action="reg.php" method="post">
  5. Login: <input type="text" name="login"><br>
  6. Hasło: <input type="password" name="passwd"><br>
  7. E-mail: <input type="text" name="email"><br>
  8. <input type="submit" name="register" value="Zarejestruj">
  9. </form>
  10.  
  11. <?php
  12. class User
  13. {
  14. private $_login;
  15. private $_passwd;
  16. private $_email;
  17. private $_ip;
  18.  
  19. public function __get ($name)
  20. {
  21. return $this->getValue($name);
  22. }
  23.  
  24. public function __construct ($login,$passwd,$email,$ip)
  25. {
  26. $this->_login = $login;
  27. $this->_passwd = $passwd;
  28. $this->_email = $email;
  29. $this->_ip = $ip;
  30. }
  31.  
  32. public function getValue ($name)
  33. {
  34. return $this->$name;
  35. }
  36.  
  37. public function auth1 ()
  38. {
  39. if ((strlen($this->_login) < 3) or (strlen($this->_login) > 20)) die('Login musi mieć min. 3 i maks. 20 znaków');
  40. if (strlen($this->_passwd) < 6) die('Hasło musi mieć min. 6 znaków');
  41. if (!preg_match('/^[A-Za-z0-9\.\_\-]+\@[a-z0-9]+\.[a-z]{2,4}$/D',$this->_email)) die('Niepoprawny e-mail');
  42. }
  43.  
  44. public function auth2 ()
  45. {
  46. $pdo = new PDO('mysql:host=localhost;dbname=DBNAME', 'LOGIN', 'HASLO');
  47. $stmt=$pdo->query("SELECT login FROM users WHERE login='$this->_login'");
  48. $res=$stmt->fetch(PDO::FETCH_ASSOC);
  49. $stmt->closeCursor();
  50. if ($res > 0) die('Ten login jest już zajęty');
  51.  
  52. $stmt=$pdo->query("SELECT email FROM users WHERE email='$this->_email'");
  53. $res=$stmt->fetch(PDO::FETCH_ASSOC);
  54. $stmt->closeCursor();
  55. if ($res > 0) die('Ten e-mail jest już zajęty');
  56. }
  57.  
  58. public function addUser ()
  59. {
  60. $pdo = new PDO('mysql:host=localhost;dbname=DBNAME', 'LOGIN', 'HASLO');
  61. $stmt=$pdo->prepare('INSERT INTO users (login,passwd,email,ip) VALUES (:login,:passwd,:email,:ip)');
  62. $stmt->bindValue(':login',$this->_login,PDO::PARAM_STR);
  63. $stmt->bindValue(':passwd',sha1($this->_passwd),PDO::PARAM_STR);
  64. $stmt->bindValue(':email',$this->_email,PDO::PARAM_STR);
  65. $stmt->bindValue(':ip',$this->_ip,PDO::PARAM_STR);
  66. $num=$stmt->execute();
  67. }
  68. }
  69.  
  70. if (isset($_POST['register']))
  71. {
  72. $user = new User(strip_tags(trim($_POST['login'])),$_POST['passwd'],strip_tags(trim($_POST['email'])),$_SERVER['REMOTE_ADDR']);
  73. $user->auth1();
  74. $user->auth2();
  75. $user->addUser();
  76. echo 'Rejestracja ukonczona.';
  77. }

main.php
  1. <?php ob_start() ?>
  2.  
  3. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  4.  
  5. <?php
  6. if (!isset($_SESSION['id'])) die('Taka sesja nie istnieje!');
  7.  
  8. if (!isset($_SESSION['init']))
  9. {
  10. $_SESSION['init'] = true;
  11. }
  12.  
  13. echo 'Witaj '.$_SESSION['login'].' ';
  14. echo '<a href="main.php?action=0">wyloguj</a>';
  15.  
  16. if (isset($_GET['action']))
  17. {
  18. switch ($_GET['action'])
  19. {
  20. case 0:
  21. unset($_SESSION['id']);
  22. unset($_SESSION['login']);
  23. header('Location: index.php');
  24. break;
  25. }
  26. }
  27. ?>


PS: głupi problem ale co zrobic zebym nie musiał pisac $pdo = new PDO(...) w kazdej metodzie w ktorej potrzebuje wykonac zapytanie do bazy? np. w reg.php wpisuje to samo w metodzie auth2 i addUser. gdy chcialem wrzucic to do konstruktora to byl problem taki, ze te dwie metody nie wiedzialy czym jest $pdo w wyrazeniu $stmt=$pdo.. tak wiec jak to obejsc?
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 22.08.2025 - 00:30