首页 > 解决方案 > PHP SQL Server 安全方法 - 哪个更好?

问题描述

我正在学习 PHP,想问一下这两种不同的方法中哪一种在 SQL Server 安全性方面更好?假设它将用于登录表单。

功能#1

function SanitizeInput($in, $len = 20)
    {
        return substr(preg_replace("/[^a-zA-Z0-9_]/", "", htmlspecialchars(htmlentities(strip_tags($in)))), 0, $len);
    }
    

用法:

$username = $thisDB->SanitizeInput($_POST['username']);


功能#2&3

    function security($text) {
    $text = trim($text);
    $search = array('', '', '', '', '', '', '', '', '', '', '', '', ',');
    $replace = array('C', 'c', 'G', 'g', 'i', 'I', 'O', 'o', 'S', 's', 'U', 'u');
    $new_text = str_replace($search, $replace, $text);
    return $new_text;
}
function SQLSecurity($text) {
    $text = trim(htmlspecialchars($text));
    $search = array("'", '"', "TRUNCATE", "truncate", "UPDATE", "update", "SELECT", "select", "DROP", "drop", "DELETE", "delete", "WHERE", "where", "EXEC", "exec", "INSERT INTO", "insert into", "PROCEDURE", "procedure", "--");
    $replace = array("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
    $new_text = str_replace($search, $replace, $text);
    return $new_text;
}

用法:

$username = $thisDB->SQLSecurity($thisDB->security($_POST['username']));

标签: phpsql-serversecuritysanitize

解决方案


推荐阅读