2016-04-26 58 views
0

好吧,我有这样的PHP代码:阿贾克斯提供不同的结果比PHP服务器端做

$_you = $_GET['you']; 
$_answer = $_GET['answer']; 
$_pass = md5($_GET['pass']); 
$sql = "select password from users where username='$_you'"; 
$query = mysqli_query($db_conx, $sql); 
while ($row = mysqli_fetch_array($query)) { 
    $_oldpass = $row['password']; 
} 
if ($_pass !== $_oldpass) { 
    $_passresult = "not ok"; 
} else { 
    $_passresult = "ok"; 
} 
$sql2 = "select answer from useroptions where username='$_you'"; 
$query2 = mysqli_query($db_conx, $sql2); 
while ($row2 = mysqli_fetch_array($query2)) { 
    $_veranswer = $row2['answer']; 
} 
if ($_answer !== $_veranswer) { 
    $_answerresult = "not ok"; 
} else { 
    $_answerresult = "ok"; 
} 
$_result = array("Password" =>$_passresult, "Answer" =>$_answerresult); 
header("Content-Type: application/json"); 
echo json_encode($_result); 

?> 

和jQuery的这一块(片段)调用上面的PHP:

$.get("verifydp.php?u="+you+"&pass="+pass+"&answer="+answer, function(result) { 
    if (result.Password === "not ok") { 
     $('#requiredp').html('<i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Password is not correct'); 
     $('#cpassword').css("border", "solid 1px #f60"); 
     $('#cpassword').focus(); 
     $('#cpassword').val(''); 
     return false; 
    } else { 
     $('#requiredp').html(''); 
     $('#cpassword').css("border", "solid 1px #ccc"); 
    } 
    if (result.Answer === "not ok") { 
     $('#requireda').html('<i class="fa fa-exclamation-triangle" aria-hidden="true"></i> This is not the correct answer.'); 
     $('#answer').css("border", "solid 1px #f60"); 
     $('#answer').focus(); 
     $('#answer').val(''); 
     return false; 
    } else { 
     $('#requireda').html(''); 
     $('#answer').css("border", "solid 1px #ccc"); 
    } 
}); 

,你可能已经猜到了,这是我做的一个小密码验证thingie,它还没有完成,所以不要提到它不是百分之百安全的事实。只是想在以下显着的问题中得到你的帮助,我得到这两个代码之间的交互作用:

它验证正确或不正确的密码和答案,但是,当我输入正确的密码时,说密码不正确。然而有趣的是,当我使用相同的数据自行运行php部分时,它告诉我密码是正确的。 因此,PHP向Ajax提供“OK”,Ajax以某种方式将其变为“NOT OK”,然后在脚本的其余行为中导致相应的混乱。有人知道为什么会发生这种情况?任何帮助非常感谢,因为它正在做我的头: -/ 如果您需要更多信息,请让我知道。

非常感谢!

+0

感谢您的评论 - 但是,即使答案部分有问题,它仍然会处理密码部分,然后在回答问题时停下来?甚至完全停止?编辑:PHP确实给出了正确的答案 - 例如:如果我直接运行它说{密码:好,答案:好}但我警告了Ajax响应和警报说密码下的“不好”。 –

+0

[Little Bobby](http://bobby-tables.com/)说[你的脚本存在SQL注入攻击风险。](http://stackoverflow.com/questions/60174/how-can-i-prevent -sql喷射在-PHP)。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

+0

你真的不应该使用[MD5密码哈希](http://security.stackexchange.com/questions/19906/is-md5-considered-insecure),你真的应该使用PHP的[内置函数](http: //jayblanchard.net/proper_password_hashing_with_PHP.html)来处理密码安全性。确保你[不要逃避密码](http://stackoverflow.com/q/36628418/1011527)或在哈希之前使用其他任何清理机制。这样做*更改密码并导致不必要的附加编码。 –

回答

4

我只是会忽略这是多么不安全,只是因为你说。

所以,继续前进。 Ajax不以任何方式操纵数据。 Ajax只是让您的网页异步访问网络服务器并获取一些数据的“途径”。

这是不工作密码总是之所以“不正常”,是因为你的PHP期待的用户名是在GET参数,但你在GET参数ü发送。

+1

你一定是在跟我开玩笑 - 我必须完全忽略这一点,我有多愚蠢 - 只是表明它是杀人的小事,而不是大事:)无论如何,至于安全性 - 当然这都是会被消毒和所有,但因为这还没有完成,我只是想让它远离等式。非常感谢! –