验证用户输入(搜索过滤器)

发布于 2024-09-29 03:02:00 字数 370 浏览 2 评论 0原文

我正在构建一个搜索页面,但我有点不确定在清理和验证用户输入方面应该做什么。

搜索页面具有通过查询字符串传递的过滤器。 (有些是从输入传递的,有些是从具有复选框/单选类似行为的链接传递的)

在这种情况下我应该注意什么?我可以安全地使用 preg_replace (删除所有但)并转义吗?

另外 - 在将查询字符串放入链接之前,我是否需要对查询字符串(使用用户输入)执行任何操作?我是否应该添加另一个查询来检索可能的值、循环结果并排除那些未找到的用户输入? (防止链接附加用户可能放入的虚假过滤器选项。虽然这不会影响更改后的网址的用户吗?)

我不知道这是否重要,但查询字符串中传递的一些过滤器是数组。

你怎么认为?我对此很陌生,很感谢您的帮助。谢谢!

I'm building a search page and I'm a bit unsure on what I should in regard to sanitizing and validating user input.

The search page has filters which are passed via querystrings. (some passed from inputs and others from links with checkbox/radio like behavior)

What should I look out for in this situation? Can I be safe using just preg_replace (strip all but) and escaping?

Also - do I need to do anything to the querystrings (with the user input) before putting them in links? Should I add another query to retrieve possible values, loop through the results and exclude those user inputs that aren't found? (preventing the links from appending a bogus filter option the user might have put in. Although wouldn't that just affect the user with the altered url?)

I don't know if it matters but some filters passed in the querystrings are arrays.

What do you think? I'm quite new to this and I appreciate the help. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2024-10-06 03:02:00

从您的问题中不清楚您想要保护什么,但清理输入的常见问题是数据库。

在这种情况下,最重要的是:使用参数化查询。这可以立即解决您的大部分问题。这是一个很好的答案。

再多的转义也比不上更好。那。

至于清理查询字符串本身,问问自己:如果攻击者手动构建 URL,会发生不好的事情吗?或者他们只会得到一个错误页面?

It's not clear from your question what you are trying to protect, but the usual issue with sanitizing input is the database.

In that case, the most important thing is: use parameterized queries. This solves most of your problems immediately. Here's a good answer.

No amount of escaping is better than that.

As for sanitizing the querystring itself, ask yourself: if an attacker constructed a URL by hand, could something bad happen? Or would they just get an error page?

沉默的熊 2024-10-06 03:02:00

PHP 附带了专门用于清理 MySQL 查询中使用的字符串的函数。

如果您使用(老式)PHP 函数 mysql_query() 向数据库发出查询,请查看 PHP 函数 mysql_real_escape_string

$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
        mysql_real_escape_string($user),
        mysql_real_escape_string($password));

PHP comes with functions specifically for sanitizing strings for use in a MySQL query.

If you are using the (old-fashioned) PHP function mysql_query() to issue your queries to the database, have a look at the PHP function mysql_real_escape_string.

$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
        mysql_real_escape_string($user),
        mysql_real_escape_string($password));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文