Mayfish data warehouse receiving verification code

Source: Internet
Author: User

Before writing data to the database, you should first verify the data to be written to avoid serious security issues (such as general SQL injection attacks ).
Mayfish can flexibly customize validation rules for the data content to be written to reduce the trouble for developers to manually verify the data of each field.
I. first define the database module CopyCode The Code is as follows: <? PHP
Class membermodel extends appmodel
/** Set the database table name **/
Protected $ tablename = "members ";
* Data verification rules
Protected $ verify = array (
Array ("notempty", "username", "user name cannot be blank "),
Array ("hasone", "username", "this user already exists. Please try again with another user name "),
Array ("notempty", "password", "password cannot be blank "),
Array ("notempty", "email", "email address cannot be left blank "),
Array ("isemail", "email", "Incorrect email address format "),
Array ("hasone", "email", "email address occupied ")
* Overwrite the method for adding data to the database of the parent class.
* Perform MD5 encryption on the user password first, and then call the parent class method to write the data to the database.
Public Function create ($ data ){
$ DATA = array_map ("addslashes", $ data); // escape punctuation marks (single or double quotation marks) in the data
$ Data ["password"] = MD5 ($ data ["password"]);
Return parent: Create ($ data );

Ii. Write Data Copy code The Code is as follows: // execute the segment for writing data...
// Perform the data warehouse receiving operation
Private function postdata (){
$ Fields = array ("username", "password", "email ");
$ Post = array_map ("trims", $ _ post); // clear unnecessary spaces on both sides of all data
$ Post = parsehtml ($ post, $ fields); // clear the specified field content in HTML.
$ DATA = parsefields ($ post, $ fields); // extract fields that can be written to the database (prevent others from submitting some ulterior motives on your page)
$ Db = & M ("member ");
// Perform data verification
If (! $ Db-> verify ($ data )){
// The verification fails. Retrieve the cause of the failure and submit it to the template page.
$ This-> assign ("error", $ db-> getverifyerror ());
// Submit the submitted data to the template (to make the user feel as though they have not left the page)
$ This-> assign ("default", $ post );
// Render registration page template
$ This-> display ("/register.html ");
Else {
// Write data to the database
$ Result = $ db-> Create ($ data );
// Return a Boolean value, indicating that Data Writing fails and the registration page template is rendered.
If (is_bool ($ result )){
$ This-> assign ("default", $ post );
$ This-> display ("/register.html ");
Else {
// Registration successful. The rendering registration successful Page Template
$ This-> assign ("username", $ data ["username"]);
$ This-> display ("/reg_success.html ");

The verifiable rules are:
Notempty cannot be blank
Number can only be an integer.
Is the isemail address correct?
Whether hasone is unique (whether it is repeated, whether it already exists)
RegEx custom Regular Expression

The verification format is
Array (verification method, verification field name, verification error prompt information)
Verification of Regular Expression
Array ("RegEx", "mobile", '/^ 13 \ D {9} $/', "user name cannot be blank ")

Mayfish download

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: 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.