简体   繁体   中英

PHP counterpart for jQuery processData

While transmitting PHP code via jQuery ajax post requests I ran into problems with string quoting. When jQuery's ajax's method's processData is not set or set to true, the passed code, according to jQuery manual:

will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded"

This leads me to think I should urldecode() the input in PHP, but that's not correct, since that's about transforming percent encoded strings back to actual characters, and not about quoting.

I've tried using stripslashes() and stripcslashes() and both seem to work, but since they are not identical, this leaves me a bit uncertain as to which is the correct way, that will reliably leave me with 100% bitwise identical input?

As a workaround I'm thinking of Base64 encoding the code in javascript before submitting, but this adds possibly unnecessary overhead.

So: What is the correct PHP counterpart to jQuery's processData ?

Edit: To clarify what's happening, here's an example:

Code input:

/** Textfield has the following contents:
 *  <?php
 *  echo 'Hello world!';
 */
var code = $("textarea").val();
$.ajax({
    url: "gateway.php",
    type: "POST",
    data: {code : code}
});

And in the PHP backend:

var_dump($_POST['code']);
/** Output is:
 *  <?php
 *  echo \'Hello world\';
 */

That looks like you have magic_quotes_gpc on, which drives php to escape some characters of GET, POST and COOKIES (especially quotes) automatically. It's very unlikely that the escaping is done in your Javascript.

Have a look at the php-manual: http://php.net/manual/de/security.magicquotes.disabling.php

You can disable that feature on server basis and manually in your scripts but that is a bit ugly. The preferable solution is to change your php.ini or to search for such option in your hosting providers panel if you don't have access to the ini-file.

Hope i could help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM