php - 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']));
解决方案
推荐阅读
- php - Laravel 5.5 中 m:m 雄辩关系的 1:1 变体:示例有效吗?
- django - Django-Rest-Framework 无法解析超链接关系的 URL
- java - 我应该如何将我的 Java 代码转换为 J-meter BeanShell PreProcessor
- javascript - `Hooks` 是否可以用来替代 React 中的 `Redux`?
- regex - 使用正则表达式 python3 找到固定日期格式?
- swift - Swift 5 中切换案例“@unknown default”和“default”之间的区别
- php - 插入时出现Mysqlierror
- java - 从另一个抽象类扩展的抽象类的重要性是什么
- azure - 如何从 adalservice userInfo 获取角色
- python - Python + Pandas + 数据框:Keyerror