1

Background: I am not a php programmer, and I only know enough to get the web service parts of my iOS apps working.

I am finding that my php is adding backslashes to my text, when I don't want it to. For example if I set up some variables like this:

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

then I output via a JSON array back to my device:

$directory . $file 

and I get

"directory\/file.jpg"

I have run get_magic_quotes_gpc with in my php and it reports that magic quotes is turned off. I do this this way:

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
}

I have tried to use stripslashes() (on either the original variables or the output) and nothing changes.

So how do I stop it from doing this? As it means that I can't tell my php to delete files in specific directories.

thanks in advance

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
narco
  • 830
  • 8
  • 21
  • 4
    Why do you care? In JSON strings ``\/`` and ``/`` are equivalent. – Quentin Apr 07 '14 at 19:57
  • I didn't realise that this was only happening in JSON, I thought it represented what was really happening in the php. I thought this was a symptom of an issue I was having elsewhere, but it turns out that I had been barking up the wrong tree. – narco Apr 07 '14 at 20:56

2 Answers2

5

When you encode your elements do it as so:

json_encode($str, JSON_UNESCAPED_SLASHES);

Using JSON_UNESCAPED_SLASHESshould prevent the addition of unwanted slashes.

0x9BD0
  • 1,542
  • 18
  • 41
3

If you really, really want to do this, then set the JSON_UNESCAPED_SLASHES option.

json_encode ( $value, JSON_UNESCAPED_SLASHES );

But the slashes do no harm and provide protection against </script> being injected into a <script> element when you inject your JSON into a JavaScript program.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • thank for the reply. I actually don't need the JSON reply (at least not for this), I am just using it to see what is going on (as my files are not getting found by the php). Perhaps my problem is elsewhere then. – narco Apr 07 '14 at 20:04