我张贴表单域的通过AJAX内容PHP脚本和使用JavaScript escape(field_contents)。问题是,任何加号正在被剥离出来,并用空格代替。我怎样才能安全地“编码”的加号,并在PHP端则适当地“解码”吗?

有帮助吗?

解决方案

使用encodeURIComponent()在JS和PHP,你应该得到正确的值。

请注意:当您访问$_GET$_POST$_REQUEST在PHP中,要检索已经被解码值。

示例:

在您的JS:

// url encode your string
var string = encodeURIComponent('+'); // "%2B"
// send it to your server
window.location = 'http://example.com/?string='+string; // http://example.com/?string=%2B

在您的服务器:

echo $_GET['string']; // "+"

有仅包含该网址编码数据中的原始的HTTP请求。

有关的GET请求可以从URI. $_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']检索此。对于urlencoded的POST,file_get_contents('php://stdin')

NB:

decode()仅适用于单字节编码字符。它不会为全UTF-8范围内工作。

例如:

text = "\u0100"; // Ā
// incorrect
escape(text); // %u0100 
// correct
encodeURIComponent(text); // "%C4%80"

注意:"%C4%80"等效于:escape('\xc4\x80')

哪些是表示在UTF-8 \xc4\x80的字节序列(Ā)。所以,如果你使用encodeURIComponent()您的服务器端必须知道它正在接收UTF-8。否则PHP将裂伤的编码。

其他提示

在JavaScript的尝试:

encodeURIComponent() 

和在PHP:

urldecode($_POST['field']);

你正在寻找的十六进制值是%2B

要自动PHP得到它通过urlencode($stringVal)运行字符串。然后运行它rhough urldecode($stringVal)把它找回来。

<击>如果你想在JavaScript来处理它,使用escape( str )

修改

@ bobince的评论之后,我做了更多的阅读,他是正确的。 使用encodeURIComponent(str)decodeURIComponent(str)。逃生不会转换角色,只有\的转义

为了使它更有趣,并希望使头发少拉别人。 使用Python,内置字典,我们可以使用卷曲配置的装置。

问题:{"timezone":"+5"} //throws an error " 5"

解决方案:{"timezone":"%2B"+"5"} //Works

因此,概括地说:

var = {"timezone":"%2B"+"5"}
json = JSONEncoder().encode(var)
subprocess.call(["curl",ipaddress,"-XPUT","-d","data="+json])

感谢这个职位!

如果您需要做在PHP卷曲,你应该使用urlencode()从PHP但个别!

strPOST = "Item1=" . $Value1 . "&Item2=" . urlencode("+")

如果你这样做urlencode(strPOST),你会为你带来另外一个问题,你将有一个项目1和&将改变XX%值,是作为一个值,在这里看到下来的回报!

实施例1

$strPOST = "Item1=" . $Value1 . "&Item2=" . urlencode("+") will give Item1=Value1&Item2=%2B

实施例2

$strPOST = urlencode("Item1=" . $Value1 . "&Item2=+") will give Item1%3DValue1%26Item2%3D%2B

实施例1是在卷曲制备字符串POST的好方法

实施例2表明,该受体将看不到等于和区分两个值与符号!

我的问题是与重音符号(A E N)和的加号(+)时,我试图保存的javascript“代码示例”到MySQL:

我的解决方案(没有更好的办法,但它的工作原理):

的javascript:

function replaceAll( text, busca, reemplaza ){
  while (text.toString().indexOf(busca) != -1)
  text = text.toString().replace(busca,reemplaza);return text;
}


function cleanCode(cod){
code = replaceAll(cod , "|", "{1}" ); // error | palos de explode en java
code = replaceAll(code, "+", "{0}" ); // error con los signos mas   
return code;
}

功能,以节省:

function save(pid,code){
code = cleanCode(code); // fix sign + and |
code = escape(code); // fix accents
var url = 'editor.php';
var variables = 'op=save';
var myData = variables +'&code='+ code +'&pid='+ pid +'&newdate=' +(new Date()).getTime();    
var result = null;
$.ajax({
datatype : "html",
data: myData,  
url: url,
success : function(result) {
    alert(result); // result ok                     
},
}); 
} // end function

在PHP函数:

<?php
function save($pid,$code){
    $code= preg_replace("[\{1\}]","|",$code);
    $code= preg_replace("[\{0\}]","+",$code);
    mysql_query("update table set code= '" . mysql_real_escape_string($code) . "' where pid='$pid'");
}
?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top