PHP 安全性 - (int) 与 FILTER_VALIDATE_INT
最近有人告诉我有 FILTER_VALIDATE_INT ,顺便说一下,它很棒。
我的问题是从网站获取整数值,无论它是来自用户还是从 Web 应用程序生成,并通过查询字符串传递。
该值(整数)可以在mysql查询中显示或使用。
我正在尝试为此构建最佳的安全方法。
考虑到这一点,简单地使用是否安全?
$myNum = (int)$_GET['num'];
或者
if (filter_var($_GET['num'], FILTER_VALIDATE_INT)) $myNum = $_GET['num'];
,请解释使用 (int)
和 FILTER_VALIDATE_INT
之间的区别
I recently was told there is FILTER_VALIDATE_INT which is great by the way.
My question is in terms of taking an integer value from the website whether it maybe from user or generated from the web application, and passed via query string.
The value (integer) may be displayed or used in mysql query.
I am trying to structure the best possible security method for this.
With that in mind, is it safe to simply use
$myNum = (int)$_GET['num'];
Or
if (filter_var($_GET['num'], FILTER_VALIDATE_INT)) $myNum = $_GET['num'];
Also, please explain what is the difference between using (int)
and FILTER_VALIDATE_INT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不同之处在于,转换为
int
总是会得到一个int
,它可能是也可能不是原始值。例如(int)'foobar'
结果为int
0
。这使得它对于大多数 SQL 目的来说是安全的,但与原始值无关,你甚至不会知道它。filter_var
与FILTER_VALIDATE_INT< /code> 告诉您该值是否为
int
,基于此您可以决定在 SQL 查询中使用它还是向用户显示错误消息。The difference is that a cast to
int
will always get you anint
, which may or may not be the original value. E.g.(int)'foobar'
results in theint
0
. This makes it safe for most SQL purposes, but has nothing to do with the original value, and you won't even know it.filter_var
withFILTER_VALIDATE_INT
tells you whether the value is anint
, based on which you can make the decision to use it in an SQL query or display an error message to the user.当值为 number 时,请尝试测试。
Please try test with when value is number .