如何用PHP屏蔽url中的特殊字符?
How to block special characters in the url with PHP?
url 中是否有阻止特殊字符的代码?如果您尝试将特殊字符放入站点的 URL 中(例如:/ ;),它会转换为“%”。
这可以防止 SLQ 注入,或者某种注入,对吧?
我该怎么做?
目前我的网站只转换撇号(')。
这叫做URL encoding and is not used to prevent SQL injection or any other kind of attack. You can do it in PHP through URL functions, notably urlencode
。
但是,如果您不理解其含义或用途,我建议您不要这样做 "blindly"。
如果您有要解决的实际问题,您应该 ask about that problem and not your perceived solution。
用特殊代码替换对 URL 无效的字符是 urlencode 功能。这与安全无关。但如果没有它,URL 可能会变得无效,因此您将得不到回复。
为了避免 SQL 注入漏洞,请使用准备好的语句(取决于 library/extension 你用来连接到数据库,它可能是 mysqli::prepare, pdo::prepare )或其他东西。检查文档文章中的示例。
你说的是URL encoding,与SQL注入无关。 URL编码是为了防止与下列具有特殊含义的字符混淆:
! * ' ( ) ; : @ & = + $ , / ? # [ ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
这是由浏览器自动完成的,您不需要为您的用户在服务器端设置任何东西。但是,如果您需要查找字符串曾经是什么,可以使用 urlencode()
if you need to pass an encoded string, and urldecode()
。
要防止SQL注入,最重要的是使用准备好的语句。在程序 MySQLi 中,这看起来像:
$stmt = $conn->prepare('SELECT * FROM table WHERE field = ?');
$stmt->bind_param('s', $variable);
$stmt->execute();
关于额外的预防措施,我建议参考this post。
url 中是否有阻止特殊字符的代码?如果您尝试将特殊字符放入站点的 URL 中(例如:/ ;),它会转换为“%”。
这可以防止 SLQ 注入,或者某种注入,对吧?
我该怎么做?
目前我的网站只转换撇号(')。
这叫做URL encoding and is not used to prevent SQL injection or any other kind of attack. You can do it in PHP through URL functions, notably urlencode
。
但是,如果您不理解其含义或用途,我建议您不要这样做 "blindly"。
如果您有要解决的实际问题,您应该 ask about that problem and not your perceived solution。
用特殊代码替换对 URL 无效的字符是 urlencode 功能。这与安全无关。但如果没有它,URL 可能会变得无效,因此您将得不到回复。
为了避免 SQL 注入漏洞,请使用准备好的语句(取决于 library/extension 你用来连接到数据库,它可能是 mysqli::prepare, pdo::prepare )或其他东西。检查文档文章中的示例。
你说的是URL encoding,与SQL注入无关。 URL编码是为了防止与下列具有特殊含义的字符混淆:
! * ' ( ) ; : @ & = + $ , / ? # [ ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
这是由浏览器自动完成的,您不需要为您的用户在服务器端设置任何东西。但是,如果您需要查找字符串曾经是什么,可以使用 urlencode()
if you need to pass an encoded string, and urldecode()
。
要防止SQL注入,最重要的是使用准备好的语句。在程序 MySQLi 中,这看起来像:
$stmt = $conn->prepare('SELECT * FROM table WHERE field = ?');
$stmt->bind_param('s', $variable);
$stmt->execute();
关于额外的预防措施,我建议参考this post。