【问题标题】:How to post the password using ajax and php?如何使用 ajax 和 php 发布密码?
【发布时间】:2012-07-21 09:36:22
【问题描述】:

我有一个固定的用户名密码和一个可变文本。

这是第一种方法,但不安全

<form action="http://site.com/foo.php" method="post">
  <input type="hidden" name="username" value="user123" />
  <input type="hidden" name="password" value="pass123" />
<input type="text" name="text" />
<input type="submit" />

</form> 

这是第二种方法请填写:

index.html

<form action="foo.php" method="post">
<input type="text" name="text" />
<input type="submit" />
</form> 

foo.php

$username = "user123";
$password = "pass123";

$text = $_POST["text"];

$url  = "http://site.com/foo.php?text=".$text."&password=".$password."&username=".$username;

如何安全地发布 $url? (无 HTTPS)

【问题讨论】:

标签: php jquery ajax security post


【解决方案1】:

更新:

如果没有 HTTPS,您将无法安全登录。
这是非常不安全的,并且不会阻止人们登录 如果他们拦截了哈希。
只需使用 HTTPS。


使用MD5 function

例如

$url = "http://example.com/foo.php?text=".$text."&password=".md5($password)."&username=".$username;

然后在接收站点 (http://example.com/foo.php?...) 上,使用实际密码的哈希 (MD5) 检查接收到的密码。

示例:

发送文件:

$username = "user123";
$password = "pass123";

$text = $_POST["text"];

$url = "http://example.com/foo.php";
$data = "text=".$text."&password=".md5($password)."&username=".$username;

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($handle);
curl_close($handle);
if($result) {
    // Success
} else {
    // Failed
}

接收文件:

$username = $_POST["username"];
$password = $_POST["password"];

// Insert code here to escape the username with mysqli_real_escape_string,
// then retrieve data from database with MySQLi.

if($password == md5($db_password)) {
    // Correct password
} else {
    echo 'Incorrect password.';
}
unset($username, $password, $db_password); // For security, remove variables from memory

【讨论】:

  • 我没有收到数据,服务器收到数据。
  • @Sinac - 你为什么这么说? curl 可以发送数据,就像我的回答一样。
  • 第 8 行 (md5) 出现错误:语法错误,意外 T_VARIABLE,我在 localhost easyphp 中测试
  • @Sinac - 抱歉,我忘了分号。上一行应为$url = "http://site.com/foo.php";。我已经编辑了我的答案。
  • 如果中间的人监控到服务器对密码进行哈希处理的线路,他不会仍然得到明文密码吗?另外,如果他监控到另一台服务器的线路,他也会得到哈希,所以他不能用它来登录吗?
【解决方案2】:

没有 HTTPS 就没有安全。

因为当您发送密码时,即使您对其进行了加密,网络中继节点也将获得访问权限,并且可以这样使用。

您只能使用 MD5 来防止密码观察,但它仍然可以访问。

但在 HTTPS 本身是一种加密方式中,密码不能被破解,因为有一个只有客户端和服务器知道的公钥和私钥。

也许您可以通过 HTTPS 进行登录。无需购买证书。您可以轻松地自己发布一个并在您的主机上进行设置。

为重要业务使用 HTTPS。

【讨论】:

  • 如何发布 $url ?我想发布老化 $url。
  • 我不明白你。发布 $url 是什么意思?
  • (persian?) 在 foo.php 我们从 index.php 中获取一些信息,例如文本并添加用户名和密码,现在我们有一个包含用户+密码+文本的 $url。现在我们要将其发送到服务器。
  • 是的,我是。为什么要将数据再次发送到另一个文件。只需在第一个文件上进行登录,如果成功则之后重定向用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
相关资源
最近更新 更多