使用password_hash或password_verify时,php响应文本为空

时间:2016-07-12 17:59:58

标签: javascript php html5 passwords

我有一个应用程序,它使用javascript获取字符串并将其POST到服务器上的接收器php文件以进行进一步处理。接收者的工作是解析字符串,传递信息,并向javascript报告事情的进展情况。最近我试图为整个shebang添加基于密码的安全性,但现在receiver.php传回了一个空响应。

我发现如果我在接收器的任何地方调用password_verify(无论我用它做什么......我甚至可以在不使用它的情况下调用它)脚本中的以下回声不会运行 - 来自那些回声的任何responseText将是空的 - 我不知道为什么。但是,如果我只是从命令行运行php脚本,那么一切都可以工作。

在继续之前,我会注意到我对Web开发和基于密码的安全性都很陌生,所以不要太苛刻。不过好消息 - 不像我在网上找到的每一个其他问题,我得到了正确的哈希和正确的验证回复。

使用php 5.6.23

JS文件“test.html”的缩小版本:

<!doctype html>
<html lang="en">
<head>
<script type="text/javascript">
        function reviewAndSubmit() {
            //bigOlString is usually built as a result of TONS of script on this page, use this as a test
            var bigOlString = "password=1234 name=test otherParams=barglebargle"
            var postString = new XMLHttpRequest();

            //build function to receive post response
            postString.onreadystatechange = function() {
                if (postString.readyState == 4) {
                  window.alert("responseText: " + postString.responseText)
                }
            }

            postString.open("POST", "receivertest.php", true);
            postString.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
            postString.send(bigOlString);
        }


</script>
</head>
<body>
<button class="button" id="pushMe" onClick="reviewAndSubmit()">Why does PHP hate me so bad?</button>

并且receivertest.php:

<?php

//hashed version of '1234' and debug name 'test'
$debugPass = '$2y$10$b.08/4NfawKOwrBYJqguc.AWsI3mQiGGaz1eYvfc9Uid1auQKKABm';
$debugName = 'test';

//get incoming string
$inString = file_get_contents('php://input');

//get challenge content
$challengePass = substr($inString, strpos($inString, "password=") + 9, strpos($inString, "name=") - 10); //10 because of the space
$name = substr($inString, strpos($inString, "name=") +5, (strpos($inString, "otherParams=") - strpos($inString, "name=") - 6)); //ugly!

//begin authentication
$auth = False;
echo $name;
echo $challengePass;


password_verify($challengePass, $debugPass); //yes, I'm not doing anything with this.  Doesn't matter.
echo "this line won't echo";
?>

如果您在receivertest.php中注释掉'password_verify'行,那么一切都会完美无缺。如果你不这样做,没有运气 - test.html中的'alert'窗口只是吐出'responseText test123'。但是,如果你在控制台(php receivertest.php)中运行receivertest.php,一切都正确回声。

为什么我的responseText为空?

编辑:我编辑了php脚本以更好地说明问题。是的,我知道我没有使用password_verify做任何事情。没关系。 '此行不会回显'行不会像其他行一样在test.html中回显。 我的问题是:为什么不呢?

1 个答案:

答案 0 :(得分:0)

代码很好。我在我的httpd.conf文件中快速编辑并重新启动了apache,一切都开始工作了。我取消了那个编辑以检查并重新启动 - 一切仍然有效。所以我想我需要的只是一个apache重启。

Apache似乎与我想的更多的关系,我想?