phpescapingmagic-quotes-gpc

PHP adds escape back slashes. How to stop this


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


Solution

  • 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.