1

背景:我不是 php 程序员,我只知道让我的 iOS 应用程序的 Web 服务部分正常工作。

我发现我的 php 在我不想要的时候在我的文本中添加了反斜杠。例如,如果我设置一些这样的变量:

$directory = "directory/";
$file = "file.jpg"

然后我通过 JSON 数组输出回我的设备:

$directory . $file 

我得到

"directory\/file.jpg"

我在我的 php 中运行了 get_magic_quotes_gpc ,它报告魔术引号已关闭。我这样做:

if (get_magic_quotes_gpc()){
//then I send something back in the JSON array to let me know that get_magic_quotes_gpc is on
}

我尝试使用 stripslashes() (在原始变量或输出上)并且没有任何变化。

那么我该如何阻止它这样做呢?因为这意味着我不能告诉我的 php 删除特定目录中的文件。

提前致谢

4

2 回答 2

5

当你编码你的元素时,这样做:

json_encode($str, JSON_UNESCAPED_SLASHES);

使用JSON_UNESCAPED_SLASHES应该防止添加不需要的斜线。

于 2014-04-07T19:58:59.303 回答
3

如果您真的非常想这样做,请设置JSON_UNESCAPED_SLASHESoption

json_encode ( $value, JSON_UNESCAPED_SLASHES );

但是,当您将 JSON 注入 JavaScript 程序时,斜线不会造成任何伤害,并且可以防止</script>被注入元素。<script>

于 2014-04-07T19:59:21.873 回答