Easy to use PHP anti-Injection vulnerability filter function code

Source: Internet
Author: User
<?php//php Whole station anti-injection program, need to require_once this file in public files//Judge MAGIC_QUOTES_GPC status if (@get_magic_quotes_gpc ()) {$_get = sec ($_get
    );
    $_post = sec ($_post);
    $_cookie = sec ($_cookie);
$_files = sec ($_files);
} $_server = sec ($_server);
            Function sec (& $array) {//If an array, iterate through the array, recursively call if (Is_array ($array)) {foreach ($array as $k => $v) {
        $array [$k] = sec ($v);
    } else if (is_string ($array)) {//Addslashes function is used to process $array = Addslashes ($array);
    else if (Is_numeric ($array)) {$array = Intval ($array);
return $array; }//Integer filter functions function Num_check ($id) {if (! $id) {die (' parameter cannot be empty.
    ');
    }//Is NULL to judge else if (Inject_check ($id)) {die (' illegal parameter ');
    }//Injection to judge else if (!is_numetic ($id)) {die (' illegal parameter ');
    }//Digital judgment $id = Intval ($id);
Integration return $id;
    }//Character filter functions function Str_check ($STR) {if (Inject_check ($str)) {die (' illegal parameters '); }//injection judgment $STR = htmlsPecialchars ($STR);
Convert HTML return $STR;
    function Search_check ($str) {$str = Str_replace ("_", "\_", $str);
    The "_" filter out $str = Str_replace ("%", "\%", $str);
    The "%" filter out $str = Htmlspecialchars ($STR);
Convert HTML return $STR; //Form filter functions function Post_check ($STR, $min, $max) {if (Isset ($min) && strlen ($STR) < $min) {die (' most
    Less $min bytes ');
    else if (isset ($max) && strlen ($STR) > $max) {die (' Maximum $max bytes ');
Return Stripslashes_array ($STR); }//anti-injection functions function Inject_check ($sql _str) {return eregi (' select|inert|update|delete|\ ' |\/\*|\*|\.\.\/|\.\/|
    Union|into|load_file|outfile ', $sql _str); Www.jb51.net filter, anti-injection} function Stripslashes_array (& $array) {if Is_array ($array)) {foreach ($a
        Rray as $k => $v) {$array [$k] = Stripslashes_array ($v);
    } else if (is_string ($array)) {$array = Stripslashes ($array);
return $array; 
 }?>

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.