1

我在密码输入表单中遇到一些字符问题

" = \"

' = \'

\ = \\

& = empty

+ = empty

所以如果用户输入 n1N'11

在输出中他会得到 n1N\\\'11

1)我做错了什么?在 ajax-php 密码验证文件中,我简单地使用$password=$_POST['password'];并得到这样的结果。在 mysql 中记录数据的文件中,我使用$password = $mysqli->real_escape_string($_POST['password']);. 但是这些问题已经在密码验证中......

2)如何改变?我可以将\"改回",但不能将空改成&或+,因为不知道访客输入了什么。或者根本不允许输入 & 和 +

3)还有更多像提到的角色吗?

我是编程新手。可能这些问题很愚蠢。

对不起,这里是ajax代码。但似乎我已经解决了我的问题。通过前需要使用encodeURIComponent

<script type="text/javascript">
// Pasword validation
//pass data without page refresh
function ValidatePassword(){
// Create our XMLHttpRequest object
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
var hr = new XMLHttpRequest();
}
else
{// code for IE6, IE5
var hr = new ActiveXObject("Microsoft.XMLHTTP");
}
// Create some variables we need to send to our PHP file
var url = "_validator.php";
var pass = document.getElementById("password").value;

var vars_pass = "&password="+encodeURIComponent(pass);

hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("CheckPassword").innerHTML = return_data;

}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars_pass); // Actually execute the request
document.getElementById("CheckPassword").innerHTML = "processing...";
}
</script>
4

1 回答 1

0

Try wrapping the value you pass with javascript in encodeURIComponent(). That way it should preserve the values correctly.

于 2013-02-07T15:09:45.500 回答