比較好用的PHP防注入漏洞過濾函數代碼

來源:互聯網
上載者:User
<?php//PHP整站防注入程式,需要在公用檔案中require_once本檔案//判斷magic_quotes_gpc狀態if (@get_magic_quotes_gpc()) {    $_GET = sec($_GET);    $_POST = sec($_POST);    $_COOKIE = sec($_COOKIE);    $_FILES = sec($_FILES);}$_SERVER = sec($_SERVER);function sec(&$array) {    //如果是數組,遍曆數組,遞迴調用    if (is_array($array)) {        foreach ($array as $k => $v) {            $array[$k] = sec($v);        }    } else if (is_string($array)) {        //使用addslashes函數來處理        $array = addslashes($array);    } else if (is_numeric($array)) {        $array = intval($array);    }    return $array;}//整型過濾函數function num_check($id) {    if (!$id) {        die('參數不可為空。');    } //是否為空白的判斷    else if (inject_check($id)) {        die('非法參數');    } //注入判斷    else if (!is_numetic($id)) {        die('非法參數');    }    //數字判斷    $id = intval($id);    //整型化    return $id;}//字元過濾函數function str_check($str) {    if (inject_check($str)) {        die('非法參數');    }    //注入判斷    $str = htmlspecialchars($str);    //轉換html    return $str;}function search_check($str) {    $str = str_replace("_", "\_", $str);    //把"_"過濾掉    $str = str_replace("%", "\%", $str);    //把"%"過濾掉    $str = htmlspecialchars($str);    //轉換html    return $str;}//表單過濾函數function post_check($str, $min, $max) {    if (isset($min) && strlen($str) < $min) {        die('最少$min位元組');    } else if (isset($max) && strlen($str) > $max) {        die('最多$max位元組');    }    return stripslashes_array($str);}//防注入函數function inject_check($sql_str) {    return eregi('select|inert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|UNION|into|load_file|outfile', $sql_str);    // www.jb51.net 進行過濾,防注入    }function stripslashes_array(&$array) {    if (is_array($array)) {        foreach ($array as $k => $v) {            $array[$k] = stripslashes_array($v);        }    } else if (is_string($array)) {        $array = stripslashes($array);    }    return $array;}?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.