提问人:Даниел Димитров 提问时间:11/26/2013 更新时间:11/26/2013 访问量:159
PHP 块提交符号
PHP Block submit symbols
问:
我怎样才能阻止输入!@#$%^&*() 只有_和字母?
这是我的代码
$query = htmlspecialchars(trim($_GET['search']));
$min_length = 3;
if (strlen($query) >= $min_length) {
$query = htmlspecialchars(trim($query));
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM skins WHERE (`username` LIKE '%" . $query . "%')") or die(mysql_error());
}
答:
0赞
Romain
11/26/2013
#1
$query = htmlspecialchars(trim($_GET['search']));
$min_length = 3;
if (strlen($query) >= $min_length) {
if(preg_match('/^[A-Za-z0-9_]+$/',$query)){
$query = htmlspecialchars(trim($query));
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM skins WHERE (`username` LIKE '%" . $query . "%')") or die(mysql_error());
} else {
//do something if $query contains something else than alphanumeric and _ chars
}
}
评论