本文介绍了要解决魔术引号,还是要确保已取消引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否值得将我的代码更改为更可移植"并能够处理恐怖的魔术引号,还是我应该确保始终通过.htaccess文件关闭该代码?

Is it worth changing my code to be "more portable" and able to deal with the horror of magic quotes, or should I just make sure that it's always off via a .htaccess file?

if (get_magic_quotes_gpc()) {
    $var = stripslashes($_POST['var']);
} else {
    $var = $_POST['var'];
}

php_flag magic_quotes_gpc off

推荐答案

不能同时满足这两种情况.两条代码路径=令人头疼的两倍,而且很有可能您会遗忘而忘记在某处处理这两种情况.

Don't accommodate both situations. Two code paths = twice the headaches, plus there's a good chance you'll slip up and forget to handle both situations somewhere.

我曾经检查魔术引号是打开还是关闭,如果它们已打开,请撤消其魔术(如线程中的其他人所建议的那样).问题是,您正在更改另一个程序员可能期望的已配置环境(无论多么愚蠢).

I used to check if magic quotes were on or off, and if they were on, undo their magic (as others in the thread have suggested). The problem with this is, you're changing the configured environment (no matter how stupid) that another programmer may expect.

这几天,我写代码就像魔术引号关闭一样,在我的主要include/bootstrap/always-runs文件中,我检查魔术引号是打开还是关闭.如果它们打开,我会抛出一个异常,解释为什么这是一件坏事,并提供有关如何将其关闭的说明.

These days I write code as though magic quotes are off, and in my main include/bootstrap/always-runs file I check if magic quotes are on or off. If they're on I throw an Exception that explains why this is a bad thing, and provide instructions on how they can be turned off.

这种方法允许您将代码编码为单一行为,鼓励其他人使用您的代码正确配置服务器(PHP 6中不再使用魔术引号),并且如果某人确实需要魔术引号他们可以处理您的异常情况并将自己的生活掌握在自己手中.

This approach allows you to code to a single behavior, encourages other folks using your code to configure their servers correctly (magic quotes is going away in PHP 6), and if someone really needs magic quotes on they can handle your exception and take their lives into their own hands.

这篇关于要解决魔术引号,还是要确保已取消引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:26