【发布时间】:2021-07-12 00:32:15
【问题描述】:
可以使用一些帮助来整理在 XML 请求中发送密码的最佳实践。我需要从 HTML 表单中收集输入,如果提供的密码与服务器上的密码匹配,则将表单中的数据插入数据库。我想通过 Javascript/XML 请求来执行此操作,这样我就可以使用innerHTML 和responseText 在适当的位置显示确认消息,而无需导航到新页面。这是我所拥有的一个例子。此服务器上启用了 HTTPS。
HTML sn-p:
<form enctype='multipart/form-data' action='javascript:insert_and_confirm_data()'>
<input type='text' id='variable_1'>
<input type='text' id='variable_2>
<input type='password' id='password'>
<input type='submit'>
<div id='confirmation'></div>
Javascript 函数:
function insert_and_confirm_data() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("confirmation").innerHTML = this.responseText;
}
var password = document.getElementById("password").value;
var variable_1 = document.getElementById("variable_1 ").value;
var variable_2 = document.getElementById("variable_2").value;
url = 'process_data.php?password=' + password + '&variable_1=' + variable_1 + '&variable_2=' + variable_2;
xmlhttp.open("POST", url, true);
xmlhttp.send();
}
PHP sn-p (process_data.php):
$password = $_POST["password"];
if (password_verify($password, $uploadPass)) {
// Get the other variables, insert them to a table, echo a confirmation message
} else {
echo "<p>Incorrect password</p>";
}
这是发送密码以在服务器上验证的“有效”且安全的方式吗?我的直觉说,即使启用了 HTTPS 并使用 POST 而不是 GET,在 XML URL 中包含密码也是不行的。我知道如何修改它以直接调用 PHP 脚本,但我真的不想失去在同一页面上显示结果的能力。但我不知道如何在此设置中执行此操作的最佳实践。
【问题讨论】:
标签: javascript php xmlhttprequest