在 PHP 中使用默认的魔术引号或用户定义的addslash/stripslash 哪一个更好?

发布于 2024-08-03 20:06:01 字数 68 浏览 2 评论 0原文

在 PHP 中使用默认的魔术引号或用户定义的addslash/stripslash 哪一个更好? 我想用最好的。请帮我。

which one will be better to use default magic quotes or user defined addslash/stripslash in PHP?
I want use the best one. please help me.

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

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

发布评论

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

评论(4

不气馁 2024-08-10 20:06:01

手动做吧!魔法引号在 PHP 5 中已被弃用,并且在 PHP 6 中被完全删除。不仅如此,它们是纯粹的邪恶!它们会在你无法想象的地方造成错误。显式总是比隐式好(正如 Python 所说)。

Do it manually! Magic quotes are deprecated in PHP 5, and they are being completely removed in PHP 6. Not only that, but they are pure evil! They will cause errors in places you can't even imagine. Explicit is always better than implicit (as Python would say).

爱的那么颓废 2024-08-10 20:06:01

如果您要关闭魔术引号,请不要只是将其替换为您自己的在所有输入中自动转义引号的系统。重点是:自动转义一切并不是一个好主意。您应该仅在将某些内容传递到应用程序的另一个系统或层之前对其进行转义。每个系统对转义都有不同的要求。

例如,如果要输出 HTML,请在输出字符串变量之前使用 htmlspecialchars()。如果要将 SQL 发送到数据库,请使用专为该数据库设计的转义函数之一,例如 mysql_real_escape_string()。如果您要创建 XML 文档,请使用 PHP 的 XML 库之一;这些将为你逃脱。

没有一种万能的转义解决方案。 这就是为什么魔术引号不好。

If you're going to turn off magic quotes, don't just replace it with your own system of automatically escaping quotes in all inputs. The point is: automatically escaping everything is a bad idea. You should only escape something before you passing it to another system or layer of your application. Each system has different requirements for escaping.

For example, if you're outputting HTML, use htmlspecialchars() before you output a string variable. If you're sending SQL to a database, use one of the escaping functions designed for that database, like mysql_real_escape_string(). If you're creating an XML document, use one of PHP's XML libraries; these will do the escaping for you.

There's no one-size-fits-all escaping solution. That's why magic quotes are bad.

倾其所爱 2024-08-10 20:06:01

两者都不。

魔法引号是万恶之源! (在我眼中甚至比过早优化更糟糕xD)

以及为什么你想创建一个用户定义的(add|strip)slashes 函数,当有原生 php 时那些?

如果您想转义数据库的日期,请使用数据库特定函数(例如 mysqli_real_escape_string)

编辑。完成我的答案(在评论中与 sadi 辩论后):

  • 如果您想显示/输出一些输入,请使用 htmlspecialchars

  • 如果您要在 URL 中使用您的输入,请使用 urlencode

因此,没有最好的或正确的单一方法来对数据进行编码。您必须在正确的时间、正确的地点使用正确的功能。每个都有其自己的目的(请参见此处:http://xkcd.com/163/

neither.

magic quotes is the root of all evil! (in my eyes even worse than premature optimization xD)

and why do you want to create a user defined (add|strip)slashes function, when there are native php ones?

if you want to escape your date for a database use the database specific functions (e.g. mysqli_real_escape_string)

EDIT. to complete my answer (after debating with sadi in the comments):

so, there is no best or correct single way to encode your data. you have to use the right functions in the right place at the right time. each has its own purpose (see here: http://xkcd.com/163/)

浅暮の光 2024-08-10 20:06:01

最好根本不要使用魔术引号。如果您在脚本的开头关闭它们:

set_magic_quotes_runtime(0);

您可以使您的应用程序向后兼容早期版本的 PHP,同时为当前版本的 PHP 进行编码,其中它们已被折旧,而未来版本则被完全删除。

自己从字符串中删除引号是一种很好的做法,当出现问题时,您知道它是在您自己的代码中,而不是 PHP 设置或与您当前使用的 PHP 版本不兼容。

希望有帮助。

It's best not to use Magic Quotes at all. If you turn them off at the beginning of your script with:

set_magic_quotes_runtime(0);

You can make your application that will be backward compatible with earlier versions of PHP, while coding for the current version of PHP where they are depreciated, and furture versions where they are completely removed.

Removing quotes from string yourself is good practice and when something goes wrong you know it's in your own code, rather than a PHP setting or being incompatible with the PHP version you are currrently using.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文