php - 安全问题,php/mysql

标签 php mysql hash

在我的 CMS 中,我没有注册系统。只是一个登录密码,这意味着密码是在表中预定义的。即我输入了我的用户名和设置的密码,我可以成功登录。但是该密码在我的表中可见。我怎样才能散列它?当我使用 sha1 执行此操作时,我无法登录。我必须使用表中存储的确切密码登录。

这是我的代码。

<?php
 session_start();
 ?>
 <!DOCTYPE HTML>
 <html>
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>Admin Area</title>
<link rel="stylesheet" href="login-css.css" media="all"/>
</head>

 <body>
 <div class="login">
 <h1>Admin Login</h1>
   <form method="post">
    <input type="text" name="user_name" placeholder="Username"   
    required="required" />
    <input type="password" name="user_pass" placeholder="Password" 
    required="required" />
    <button type="submit" class="btn btn-primary btn-block btn-large" 
     name="login">Admin Login</button>
</form>

</div>

<h2 style="color:#FFF; text-align:center"><?php echo 
@$_GET['not_authorized']; ?></h2>

</body>


</html>

<?php

  include("includes/connect.php");
if(isset($_POST['login'])){
    $user_name = mysqli_real_escape_string($con,$_POST['user_name']);
    $user_pass = mysqli_real_escape_string($con,$_POST['user_pass']);


    $check_user = "select * from user where user_name='$user_name' AND user_password='$user_pass'";

    $run_user = mysqli_query($con,$check_user);



    if(mysqli_num_rows($run_user)>0){

        $_SESSION['user_name'] = $user_name;

        echo "<script>window.open('index.php?logged=You have successfully   
      Logged In','_self')</script>";
    }else{
        echo "<script>alert('Username or password is incorrect')</script>";
    }
  }
 ?>

还有我的表sql代码。

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) NOT NULL,
  `user_password` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

INSERT INTO `user` (`id`, `user_name`, `user_password`) VALUES
(7, 'theo', 'test');

因此密码不应显示为 test。

任何想法,

谢谢。

最佳答案

您可以将密码 $user_pass = mysqli_real_escape_string($con,$_POST['user_pass']); 转换为 sha1 并将其传递给查询。

但更好的想法是收集带有用户名的用户信息,并使用从查询中检索到的密码列值检查转换后的密码(sha1)字符串。

关于php - 安全问题,php/mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36820634/

相关文章:

php - 使用 PHP 绘制带有嵌套循环的图案

mysql - 运行 Sqoop 演示。无主机可用异常

java - 计算 java.util.hash 的 hashcode 值时使用的常量说明

powershell - 删除空键 powershell

php - 我应该使用什么格式在 MySQL 表中存储用户的首选时区?

php - 插入 session ID 和段

php - MYSQL 按其他表中出现次数最多的值对行进行排序

当插入到主数据库上有外键的两个数据库时,MySql 事务锁定超时(1205)

将字符串散列为动态字符数的算法

PHP-MySQL : Create variable for undefined index