Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php]Walidacja formularza - Kod działa, ale czy jest poprawny?, Potrzebuję rady
--Andrzej--
post
Post #1





Goście







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?(IMG:http://forum.php.pl/style_emoticons/default/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.
Powód edycji: dodanie tagu ~Cienki1980
Go to the top of the page
+Quote Post
--Andrzej--
post
Post #2





Goście







Coś mi Firefox zamulił i wrzuciłem dwa tematy. Prosze Moderatorów o usunięcie jednego.
Go to the top of the page
+Quote Post
netmare
post
Post #3





Grupa: Zarejestrowani
Postów: 285
Pomógł: 37
Dołączył: 18.12.2007
Skąd: Łódź

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


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
Go to the top of the page
+Quote Post
--Andrzej--
post
Post #4





Goście







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. ?>
Go to the top of the page
+Quote Post
netmare
post
Post #5





Grupa: Zarejestrowani
Postów: 285
Pomógł: 37
Dołączył: 18.12.2007
Skąd: Łódź

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


  1. <?php
  2. if (!check_email($_POST['email'])) blad("tresc bledu");
  3. ?>


Uważaj na iniekcję SQL przy walidacji (IMG:http://forum.php.pl/style_emoticons/default/exclamation.gif) !
Go to the top of the page
+Quote Post
--Andrzej--
post
Post #6





Goście







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ę;)
Go to the top of the page
+Quote Post

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: 23.08.2025 - 04:14