Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] klasa dodawająca użytkowników + wysyłanie maila, pobranie ID
Lejto
post
Post #1





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 48
Dołączył: 23.05.2007

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


Mam problem z klasą

Tak wykonuje rejestracje:
  1. $u->username = htmlspecialchars($_POST['username']);
  2. $u->password = htmlspecialchars($_POST['password1']);
  3. $u->emailAddr = htmlspecialchars($_POST['mail']);
  4. $u->imie = htmlspecialchars($_POST['imie']);
  5. $u->nazwisko = htmlspecialchars($_POST['nazwisko']);
  6. $token = $u->setInactive();
  7. echo '<p><strong>Dziękujemy za ' .
  8. 'zarejestrowanie się.</strong></p><p>Należy pamiętać o zweryfikowaniu konta i kliknąć w łącze załączone w mailu który sotał wysłany';


Dodaje pomyślnie dane do bazy.
Gorzej jest z pobraniem id zarejestrowanego użytkownika. (jak wysyłam maila to potrzebuje te dane)
pobieram tak:
  1. $user->userId;

nic nie zwraca

Klasa:
  1. <?
  2. class User extends functions
  3. {
  4. public $uid;
  5. public $fields = array();
  6. protected $sql;
  7. public $userId;
  8.  
  9. public function __construct($sql)
  10. {
  11. $this->sql = $sql;
  12. $this->uid = null;
  13. $this->fields = array('username' => '',
  14. 'password' => '',
  15. 'emailAddr' => '',
  16. 'gg' => '',
  17. 'imie' => '',
  18. 'nazwisko' => '',
  19. 'isActive' => false);
  20. }
  21. public function __get($field)
  22. {
  23. if($field == 'userId')
  24. {
  25. return $this->uid;
  26. }
  27. else
  28. {
  29. return $this->fields[$field];
  30. }
  31. }
  32. public function __set($field, $value)
  33. {
  34. if(array_key_exists($field, $this->fields))
  35. {
  36. $this->fields[$field] = $value;
  37. }
  38. }
  39. public function validateUserName($username)
  40. {
  41. if(strlen($username)<3)
  42. {
  43. echo 'Nazwa użytkownika musi zawierać co najmniej 3 znaki.';
  44. }
  45. else
  46. {
  47. return true;
  48. }
  49. if(strlen($username)>20)
  50. {
  51. echo 'Za długa nazwa użytkownika max 20 znaków.';
  52. }
  53. else
  54. {
  55. return true;
  56. }
  57. }
  58. public function validateEmailAddr($email)
  59. {
  60. return filter_var($email, FILTER_VALIDATE_EMAIL);
  61. if(false)
  62. {
  63. echo 'Niepoprawny adres e-mail';
  64. }
  65. }
  66. public function getById($uid)
  67. {
  68. $db = $this->sql;
  69.  
  70. $u = new User($db);
  71. $sql = $db->query('select * from users where user_id = "'.$uid.'"');
  72.  
  73. if($sql->num_rows)
  74. {
  75. $row = $sql->fetch_array();
  76. $u->username = $row['username'];
  77. $u->password = $row['password'];
  78. $u->emailAddr = $row['email_addr'];
  79. $u->isActive = $row['is_active'];
  80. $u->imie = $row['imie'];
  81. $u->nazwisko = $row['nazwisko'];
  82. $u->uid = $uid;
  83. }
  84. return $u;
  85. }
  86.  
  87. public function getByUsername($username)
  88. {
  89. $db = $this->sql;
  90. $u = new User($db);
  91. $sql = $db->query('select * from users where username = "'.$username.'" and is_active = 1');
  92.  
  93. if($sql->num_rows)
  94. {
  95. $row = $sql->fetch_array();
  96. $u->password = $row['password'];
  97. $u->username = $row['username'];
  98. $u->emailAddr = $row['email_addr'];
  99. $u->isActive = $row['is_active'];
  100. $u->imie = $row['imie'];
  101. $u->nazwisko = $row['nazwisko'];
  102. $u->uid = $row['user_id'];
  103. }
  104. return $u;
  105. }
  106.  
  107. public function save()
  108. {
  109.  
  110.  
  111. $db = $this->sql;
  112. $data = date('d.m.y H:i:s');
  113.  
  114. if($this->uid)
  115. {
  116. $sql = $db->query('update USERS set username = "'.$this->username.'", password = "'.$this->password.'",
  117. email_addr = "'.$this->emailAddr.'", is_active = "'.$this->isActive.'" where user_id = "'.$this->userId.'"');
  118.  
  119.  
  120. }
  121. else
  122. {
  123. $sql = $db->query('insert into users (username, password, email_addr, is_active, gg,imie,nazwisko,data)
  124. values ("'.$this->username.'", "'.sha1($this->password).'", "'.$this->emailAddr.'", "0",
  125. "'.htmlspecialchars($_POST['gg']).'","'.$this->imie.'","'.$this->nazwisko.'","'.$data.'")');
  126. if(!$sql)
  127. {
  128. var_dump($this->password);
  129. }
  130. }
  131. }
  132.  
  133. public function setInactive()
  134. {
  135. $db = $this->sql;
  136. $this->isActive = false;
  137. $this->save();
  138.  
  139. $token = $this->random_text(5);
  140. $sql = $db->query('insert into users_pending (user_id, token) values ("'.$this->uid.'", "'.$token.'")');
  141.  
  142. return $token;
  143. }
  144.  
  145. public function setActive($token)
  146. {
  147. $db = $this->sql;
  148.  
  149. $sql = $db->query('select token from users_pending where user_id = "'.$this->uid.'" and token = "'.$token.'"');
  150.  
  151. if(!$sql->num_rows)
  152. {
  153. return false;
  154. }
  155. else
  156. {
  157. $sql = $db->query('delete from users_pending where user_id = "'.$this->uid.'" and token = "'.$this->token.'"');
  158. $this->isAdcive = true;
  159. $this->save();
  160. return true;
  161. }
  162. }
  163. }
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Co robi obiekt klasy User?

1) Reprezentuje jeden konkretny rekord z bazy danych
2) Sprawdza poprawność danych wprowadzonych w formularzu
3) Wyświetla sformatowaną odpowiedź
4) Pobiera inne obiekty klasy User z bazy danych
5) Generuje losowe tokeny
6) Przetwarza formularz
7) Wyświetla formularz

A zgodnie z ideą programowania obiektowego: jeden obiekt - jedno zadanie.

Poza tym:
1) W User::__get() masz tzw. dead-code - czyli fragment kodu, który się nigdy nie wykona (chodzi o ten drugi IF z showUserId)
2) Brak jakiegokolwiek powiadamiania czy coś co zostało wykonane (nie) wykonało się poprawnie
3) Wielokrotnie powtarzasz dokładnie ten sam kod różniący się przykładowo jednym waruniem. Google: DRY reguła, Google: KISS reguła
4) htmlspecialchars używa się przed wyświetleniem danych w dokumentach XML/HTML i im podobnych

Tak więc:
1) Poczytaj co nieco teorii na temat programowania obiektowego - bo brniesz w coś czego nie da się sklasyfikować jako OOP - co więcej... to jedynie utrudnia Ci życie
2) Podziel sobie różne zadania na różne obiekty
3) Zaplanuj jak owe obiekty będą ze sobą współpracować
4) Dopiero wtedy bierz się za pisanie
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: 8.12.2025 - 16:19