In the previous article, I talked about how to use javascript for integrated form verification. it is sufficient for client verification, but good form verification should be performed on both the client and the server -- this is the goal of writing this article. If general verification is used
In the previous article, I talked about how to use javascript for integrated form verification. it is sufficient for client verification, but good form verification should be performed on both the client and the server -- this is the goal of writing this article.
What should I do if a general verification method is used?
If ($ _ POST ['some'] does not meet condition ){
Die ('wrong ');
}
Similar to the javascript Integration verification method, you can also use arrays and eval statements in PHP to implement the form verification (server-side) framework. General steps:
1. collect user-submitted data from the $ _ GET or $ _ POST array to the custom array (optional ),
2. fill in the validation array (including the variable name, condition, and prompt ),
3. add fixed verification code.
Protocol and act. php. Form.html contains the form:
Form verification will be implemented in act. php:
$ Userinfo = array (); // Step 1
$ Userinfo ['name'] = isset ($ _ POST ['uname'])? $ _ POST ['uname']: '';
$ Userinfo ['age'] = isset ($ _ POST ['uage'])? (Int) $ _ POST ['uage']: 0;
$ Error_message = ''; // error message
// Step 2
$ Elems = array (
Array ('\ $ userinfo ['name']', 'strlen (#) <1', 'name cannot be blank! '),
// If Step 1 is omitted, rewrite it as follows:
// Array ('\ $ _ POST ['uname']', 'strlen (#) <1', 'name cannot be blank! '),
Array ('\ $ userinfo ['age']', '# <= 0', 'Age is not accurate! ')
);
// Step 3
For ($ I = 0; $ I <count ($ elems); $ I ){
If (eval ('If ('. preg_replace ('/[#]/', $ elems [$ I] [0], $ elems [$ I] [1]). ') return false;') = false ){
$ Error_message. = $ elems [$ I] [2];
}
}
In this way, form verification can be completed by filling in the validation array $ elems each time.