Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Walidacja formularza - Kod działa, ale czy jest poprawny?
Forum PHP.pl > Forum > Przedszkole
-Andrzej-
Cześć,

Napisałem sobie drobny skrypt walidacji formularza. System oczywiście działa, wszystko ładnie, tylko mam pytanie. Robię to w ten sposób:
  1. <?php
  2. // Check if the passwords match
  3.  if ($_POST['password'] != $_POST['confirmpass'])
  4.  {
  5.  
  6. $reg_error = 'Your passwords do not match';
  7. include 'templates/head.tpl';
  8. include 'templates/top.tpl';
  9. include 'templates/register.tpl';
  10. include 'templates/left.tpl';
  11. include 'templates/footer.tpl';
  12.  }
  13.  
  14.  if (!check_password_and_username($password))
  15.  {
  16.  
  17. $reg_error = 'Your passwords and login could not be the same';
  18. include 'templates/head.tpl';
  19. include 'templates/top.tpl';
  20. include 'templates/register.tpl';
  21. include 'templates/left.tpl';
  22. include 'templates/footer.tpl';
  23.  }
  24.  
  25.  if (!validSecurity($security))
  26.  {
  27.  // Reshow the form with an error
  28. $reg_error = 'You provided wrong security answer';
  29. include 'templates/head.tpl';
  30. include 'templates/top.tpl';
  31. include 'templates/register.tpl';
  32. include 'templates/left.tpl';
  33. include 'templates/footer.tpl';
  34.  }
  35.  
  36.  $username = $_POST['username'];
  37. if (!check_username($username))
  38. {
  39. // Reshow the form with an error
  40. $reg_error = 'Requested username is aldready in use';
  41. include 'templates/head.tpl';
  42. include 'templates/top.tpl';
  43. include 'templates/register.tpl';
  44. include 'templates/left.tpl';
  45. include 'templates/footer.tpl';
  46. }
  47.  
  48. $email = $_POST['email'];
  49. if (!check_email($email))
  50. {
  51. // Reshow the form with an error
  52. $reg_error = 'Requested e-mail address is aldready in use';
  53. include 'templates/head.tpl';
  54. include 'templates/top.tpl';
  55. include 'templates/register.tpl';
  56. include 'templates/left.tpl';
  57. include 'templates/footer.tpl';
  58. }
  59. ?>


To tylko fragment kodu. Teraz kolej na pytanie: Czy ten kod jest napisany poprawnie?smile.gif To znaczy, czy funkcje są stosowane tak jak manual przykazał? Nie potrzebuję żadnego gotowca, tylko paru rad jak ewentualnie poprawić, zmienić. Formularz ma 11 pół, a kod walidacji zajmuje ponad 150 linijek. Dlatego własnie pytam.

Teraz pytanie poboczne. Czy muszę w każdym pliku serwisu include'ować plik init.php /łączenie z bazą/ czy tylko w tych, które tego wymagają. Register_globals mam wyłączone. Całe logowania odbywa się na sesjach, więc na chłopski rozum biorąc, nie potrzeba, ale chciałbym poznać opinie specjalistów.

Pozdrawiam
A.
-Andrzej-
Coś mi Firefox zamulił i wrzuciłem dwa tematy. Prosze Moderatorów o usunięcie jednego.
netmare
coś podobnego napewno Ci skróci kod

  1. <?php
  2. function blad($komunikat)
  3. {
  4. include 'templates/head.tpl';
  5. include 'templates/top.tpl';
  6. include 'templates/register.tpl';
  7. include 'templates/left.tpl';
  8. include 'templates/footer.tpl';
  9. die($komunikat);
  10. }
  11. ?>


Ale pomyśl żeby zebrać błędy z całego forumalrza o ile wystąpiły, jeśli tak to wyświetlić jakie i zatrzymać skrypt
-Andrzej-
Nie wiem jak mogłem na to nie wpaść.

Tylko mnie jeszcze oświećcie:)

Czyli walidacja poszczególnego pola to będzie coś takiego?
  1. <?php
  2. $email = $_POST['email'];
  3.  
  4. if (!check_email($email))
  5. {
  6. $komunikat = "tresc bledu";
  7. blad($komunikat);
  8. }
  9. ?>
netmare
  1. <?php
  2. if (!check_email($_POST['email'])) blad("tresc bledu");
  3. ?>


Uważaj na iniekcję SQL przy walidacji exclamation.gif!
-Andrzej-
Uważam, uważam. Stosuję htmlspecialchars(), addslashes() przed wysłaniem do bazy oraz strip_tags() przed wyświetleniem. netmare naprawdę dziękuję za pomoc. Niesamowicie skróciło to ten kod i jakoś tak...bardziej poprawnie mi wygląda.

Idę się rejestrowac na forum. Może ja komuś pomogę;)
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.