Php anti-injection and anti-character filtering code

Source: Internet
Author: User

It provides php developers with a php SQL anti-injection, character filtering, and various filtering codes.

It provides php developers with a php SQL anti-injection, character filtering, and various filtering codes.

/// = Automatic Anti-injection filtering [Low program efficiency after enabling] ====================== ========================================================== ======================================
/*
Function inject_checks ($ SQL _str) {return eregi ('select | insert | update | delete | '|/* | .. /|. /| union | into | load_file | outfile ', $ SQL _str );}
Foreach ($ _ REQUEST as $ value) {if (inject_checks ($ value) {echo "<script language = javascript> alert ('your submitted data is invalid, please check and submit again! '); </Script> "; exit ;}}
*/
// = [Inject_check ($ SQL _str)] ========================================================== ========================================================== ============
Function inject_check ($ SQL _str ){
If (eregi ('select | insert | update | delete | union | into | load_file | outfile ', $ SQL _str )) {echo "<script language = javascript> alert ('the data you submitted is invalid. Check it and submit it again! '); </Script> "; exit ;}
Return $ SQL _str;
}
// = Character filtering [safe_convert ($ string)] ========================================================== ==============================================
Function safe_convert ($ string) {// Words Filter
If (get_magic_quotes_gpc () {// escape character with backslash
$ String = htmlspecialchars ($ string, ENT_QUOTES); // convert special characters into HTML string formats, such as "&" to "& amp ;"
$ String = str_replace ("<", "& lt;", $ string); // replace
$ String = str_replace (">", "& gt;", $ string); // replace
$ String = str_replace ("\", '& #92;', $ string); // replace
} Else {
$ String = addslashes ($ string); // escape character with backslash // $ string = stripslashes ($ string); // remove the backslash
$ String = str_replace ("\", '& #92;', $ string );
}
// $ String = str_replace ("r", "<br/>", $ string); // line feed
// $ String = str_replace ("n", "", $ string); // Space
$ String = str_replace ("t", "& nbsp;", $ string); // Space
$ String = str_replace ("", "& nbsp;", $ string); // Space
// $ String = str_replace ('|', '& #124;', $ string); // The Replacement Operation conflicts with the classification system.
$ String = str_replace ("& amp; #96;", "& #96;", $ string); // replace
$ String = str_replace ("& amp; #92;", "& #92;", $ string); // replace
Return $ string;
}
// = [Unsafe_convert ($ string)] ========================================================== ==============================================
Function unsafe_convert ($ string) {// Words Filter
$ String = str_replace ("& #92; & quot;", "& quot;", $ string); // replace
Return $ string;
}

// = Character filtering [filter ($ string)] ========================================================== ========================================================== ================
Function filter ($ string) {// Words Filter
Include ("Filter. php"); // word Filter list
Foreach ($ badwords as $ badword ){
If (stristr ($ string, $ badword) = true) {echo "<script language = javascript> alert ('Warning: Your submitted content contains sensitive words. Please change the content. '); </Script> "; exit ;}
}
Return $ string;
}

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.