This article mainly introduces php's method of preventing remote form submission outside the site. The example demonstrates the method of submitting and filtering through session and verification code, which is very practical, for more information about how php can prevent remote form submission outside the site, see the following example. The specific implementation method is as follows:
In general, preventing webmasters from submitting a form is nothing more than adding a token to each open form or submit data for verification. this is actually no different from the verification code, the following are several examples of preventing remote form submission outside the site.
Example 1: Each time we open the submit page, a token is generated and saved in the session. when the form is submitted, we can determine whether the current token value is consistent with the session value, if yes, the job is submitted normally. Otherwise, the job is submitted invalid.
The code is as follows:
The code is as follows:
<? Php
Session_start ();
If ($ _ POST ['submit '] = "go "){
// Check token
If ($ _ POST ['token'] = $ _ SESSION ['token']) {
// Strip_tags
$ Name = strip_tags ($ _ POST ['name']);
$ Name = substr ($ name, 0, 40 );
// Clean out any potential hexadecimal characters
$ Name = cleanHex ($ name );
// Continue processing ....
} Else {
// Stop all processing! Remote form posting attempt!
}
}
$ Token = md5 (uniqid (rand (), true ));
$ _ SESSION ['token'] = $ token;
Function cleanHex ($ input ){
$ Clean = preg_replace ("! [\] [XX] ([A-Fa-f0-9 })! "," ", $ Input );
Return $ clean;
}
?>
Another obvious method is to use the verification code, which is the same as other methods. let's look at a simple example below.
Example 2: Add a verification code
The verification code is added when the form is submitted, which effectively prevents the sprinkler from submitting data. However, as the graphic image recognition program becomes more powerful, verification code recognition is constantly improving its difficulty. some verification codes even include voice recognition, which can be used by some small sites.
The code is as follows:
If ($ _ POST ['vcode']! = Get_vcode ())
{
Exit ('verification code verification failed, unable to warehouse receiving ');
}
If you are interested in specific examples, you can find many verification examples online.
I hope this article will help you with PHP programming.