PHP code framework for form verification

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.