PHP anti-injection and character anti-filter Code _php tutorial

Source: Internet
Author: User
Well, for PHP developers, a PHP SQL anti-injection and character filtering and a variety of filter code OH.

Well, for PHP developers, a PHP SQL anti-injection and character filtering and a variety of filter code OH.

= = anti-injection automatic filtering [post-activation program low efficiency]========================================================================================= =
/*
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 ""; 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 ""; 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); Turn special characters into HTML string formats such as "&" to "&"
$string =str_replace ("<", "<", $string); Replace
$string =str_replace (">", ">", $string); Replace
$string =str_replace ("\", ' \ ', $string); Replace
} else {
$string =addslashes ($string); Escape character plus backslash//$string =stripslashes ($string); Remove backslashes
$string =str_replace ("\ \", ' \ ', $string);
}
$string =str_replace ("R", "
", $string); Line break
$string =str_replace ("n", "", $string); Space
$string =str_replace ("T", "", $string); Space
$string =str_replace ("", "", $string); Space
$string =str_replace (' | ', ' | ', $string); Conflicts with the classification system are replaced
$string =str_replace ("& #96;", "'", $string); Replace
$string =str_replace ("& #92;", "\", $string); Replace
return $string;
}
= = Character anti-filter [Unsafe_convert ($string)]============================================================================ ==
function Unsafe_convert ($string) {//words Filter
$string =str_replace ("\" "," "", $string); Replace
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 ""; exit;}
}
return $string;
}

http://www.bkjia.com/PHPjc/629745.html www.bkjia.com true http://www.bkjia.com/PHPjc/629745.html techarticle well, for PHP developers, a PHP SQL anti-injection and character filtering and a variety of filter code OH. Okay, for PHP developers, a PHP SQL anti-injection and character filtering and a variety of filtering ...

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