Conversion tables A-K
  Conversion tables L-Z
  SPAM database query
  Reverse WHOIS utility
  North America area codes
  PHP Example forums

PHP example repository.

Web www.web-max.ca
Back to examples
Stopping multiple form posts of same data in PHP

If you have a user form and you want to stop multiple submissions of the same data by the same user ( especially if they have hit refresh and ignored the repost warnings ), you can using cookies and md5 to check for duplicates.

The script checks all variable in the $HTTP_POST_ARRAY and makes 1 long string containing all the data values. It then generates a MD5 hash of the string which should uniquely identify the data. This hash is then stored in a cookie ( or session if you whish ). The next time the form process is called it check the hash of the data against the one that was last stored. If they are the same we have duplicate data!

In the top of the destination page from your forms submission include the following, and process according to the results.

<?php

$lasthash 
$HTTP_COOKIE_VARS["lasthash"]; 

if(
count($HTTP_POST_VARS)) 

    
$long ""
    while(list(
$key,$value)=each($HTTP_POST_VARS))$long.=$value
    
$hash md5($long); 
    
setcookie("lasthash",$hash,time()+60*60*24*30); 


if(
$lasthash!=$hash

    
// This is new data - do what you want 

else 

    
// This is repeat data - boooo. 


?>