PHP anti-injection and character anti-filter code

Source: Internet
Author: User
Tags foreach string format

= = anti-injection automatic filtering [low]========================================================================================= efficiency after activation] =
/*
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 (' The data you submitted is illegal, Please check and resubmit! ');</script> '; exit;}}
*/
= = Anti-note [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 (' You submit the data illegally, please check and resubmit! ');</script> "; exit;}
return $sql _str;
}
= = Character filter [Safe_convert ($string)]==============================================================================
function Safe_convert ($string) {//words Filter
if (GET_MAGIC_QUOTES_GPC ()) {//escape character plus backslash
$string =htmlspecialchars ($string, ent_quotes); Convert a special character to an HTML string format like "&" 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 plus backslash//$string =stripslashes ($string); Remove backslash
$string =str_replace (" \ \, ' & #92; ', $string);
   }
   //$string =str_replace ("R", "<br/>", $string);//linefeed
   //$string =STR _replace ("n", "", $string); Spaces
    $string =str_replace ("T", "&nbsp;&nbsp;", $string);//spaces
    $ String=str_replace (" ", "&nbsp;", $string); Space
   //$string =str_replace (' | ', ' & #124; ', $string);//replace conflict with classification system
    $ String=str_replace ("&amp; #96;", "& #96;", $string); Replace
    $string =str_replace ("&amp; #92;", "& #92;", $string);//Replace
    return $string;
}
//== character anti-filter [Unsafe_convert ($string)]=================================================================== ===========
Function Unsafe_convert ($string) {//words Filter
    $string =str_replace ("&# 92;&quot; "," &quot; ", $string); Replace the
    return $string;
}

//== character filter [Filter ($string)]=============================================================================== =============
Function Filter ($string) {//words filter
include ("filter.php");//Glossary filter List
foreach ($badwords As $badword) {
if (stristr ($string, $badword) ==true) {echo <script Language=javascript>alert (' Warning: The content you submitted contains sensitive words, please change the content. ');</script> "; exit;}
}
return $string;
}

Related Article

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.