logincheck.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. session_start();
  3. include_once("../../untils/conn.php");
  4. mysqli_query($con, "set names utf8");
  5. $name = $_POST['username'];
  6. $pwd = $_POST['password'];
  7. $token = $_POST['token'];
  8. // 验证账号、密码和令牌是否为空
  9. if (empty($name) || empty($pwd) || empty($token)) {
  10. echo "<script>alert('账号、密码和令牌不能为空');history.go(-1);</script>";
  11. exit;
  12. }
  13. // 从数据库获取盐值和安全令牌
  14. $sql = "SELECT salt, token FROM admin WHERE user='$name';";
  15. $result = mysqli_query($con, $sql);
  16. $row = mysqli_fetch_assoc($result);
  17. $salt = $row['salt'];
  18. $dbToken = $row['token'];
  19. // 在查询中使用明文密码进行验证
  20. $sql = "SELECT user, password FROM admin WHERE user='$name' AND password='$pwd';";
  21. $result = mysqli_query($con, $sql);
  22. $row = mysqli_num_rows($result);
  23. $_SESSION["username"] = $name;
  24. if ($row) {
  25. // 验证通过
  26. $hashedToken = md5($token . $salt); // 使用盐值对用户输入的令牌进行加密处理
  27. if ($hashedToken === $dbToken) {
  28. echo "<script>alert('登录成功');location.href='../index.php';</script>";
  29. } else {
  30. echo "<script>alert('安全令牌错误');history.go(-1);</script>";
  31. }
  32. } else {
  33. // 验证失败
  34. echo "<script>alert('用户名或密码错误,请重新输入');history.go(-1);</script>";
  35. }
  36. ?>