login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Contact formulieren > Prevent multi submit

Prevent multi submit

Auteur: olaf - 20 maart 2005 - 09:58 - Gekeurd door: Thomas - Hits: 9095 - Aantal punten: 3.50 (1 stem)





Bij het posten van formulieren wordt de data vaak meervoudig verstuurd zodat je meer dan een keer een e-mail ontvangt of je applicatie foutief reageert. Dit script voorkomt dit probleem zonder het gebruik van javascript.

Code:
  1. <?php
  2. session_start(); // plaats deze code aan het begin van je document (gew. 21-3-2005)
  3.  
  4. function prevent_multi_submit($type = "post") { // use "get" if this is your form method
  5.  
  6. $string = "";
  7. $posted_array = ($type == "get") ? $_GET : $_POST;
  8. foreach ($posted_array as $val) {
  9. $string .= $val;
  10. }
  11. if (isset($_SESSION['last'])) {
  12. if ($_SESSION['last'] === md5($string)) {
  13. return false;
  14. } else {
  15. $_SESSION['last'] = md5($string);
  16. return true;
  17. }
  18. } else {
  19. $_SESSION['last'] = md5($string);
  20. return true;
  21. }
  22. }
  23. /* example of use:
  24. if (isset($_POST)) {
  25.   if ($_POST['field'] != "" && strlen < 25) { // place here the form validation and other controls
  26.   if (prevent_multi_submit()) { // use the function before you call the database
  27.   mysql_query("INSERT INTO tabel..."); // or send a mail like...
  28.   mail($mailto, $sub, $body);
  29.   } else {
  30.   echo "The form is already processed";
  31.   }
  32.   } else {
  33.   // your error about invalid fiels
  34.   }
  35. } */
  36. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (4)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.019s