Input value/form submission parameter filtering effectively prevents SQL injection

Source: Internet
Author: User
Input value form submission parameter filtering can effectively prevent SQL injection or illegal attacks. The following describes some good methods. if you are interested, do not miss the input value/form submission parameter filtering, methods to prevent SQL injection or illegal attacks:
The code is as follows:
/**
* Filter keywords for SQL and PHP file operations
* @ Param string $ string
* @ Return string
* @ Author zyb
*/
Private function filter_keyword ($ string ){
$ Keyword = 'SELECT | insert | update | delete | \ '| \/\ * | \. \. \/| \. \/| union | into | load_file | outfile ';
$ Arr = explode ('|', $ keyword );
$ Result = str_ireplace ($ arr, '', $ string );
Return $ result;
}

/**
* Check whether the entered number is valid. the corresponding id is valid; otherwise, false is returned.
* @ Param integer $ id
* @ Return mixed
* @ Author zyb
*/
Protected function check_id ($ id ){
$ Result = false;
If ($ id! = ''&&! Is_null ($ id )){
$ Var = $ this-> filter_keyword ($ id); // filter keywords for SQL and PHP file operations
If ($ var! = ''&&! Is_null ($ var) & is_numeric ($ var )){
$ Result = intval ($ var );
}
}
Return $ result;
}

/**
* Check whether the entered characters are valid. the corresponding id is valid; otherwise, false is returned.
* @ Param string $ string
* @ Return mixed
* @ Author zyb
*/
Protected function check_str ($ string ){
$ Result = false;
$ Var = $ this-> filter_keyword ($ string); // filter keywords for SQL and PHP file operations
If (! Empty ($ var )){
If (! Get_magic_quotes_gpc () {// determines whether magic_quotes_gpc is enabled.
$ Var = addslashes ($ string); // filter submitted data when magic_quotes_gpc is not enabled
}
// $ Var = str_replace ("_", "\ _", $ var); // filter '_'
$ Var = str_replace ("%", "\ %", $ var); // filter '%'
$ Var = nl2br ($ var); // press enter to convert
$ Var = htmlspecialchars ($ var); // html tag conversion
$ Result = $ var;
}
Return $ result;
}

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.