2016-04-24 64 views
0

在我的CMS中,我沒有註冊系統。只是一個登錄名,這意味着密碼是在表中預定義的。即我把我的用戶名和我的設置密碼,我可以成功登錄。但是,該密碼在我的表格中可見。我該如何散列它?當我使用sha1來做這件事時,我無法登錄。我必須使用存儲在表格中的確切密碼登錄。安全問題,php/mysql

這是我的代碼。

<?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'); 

所以密碼不應該顯示爲測試。

任何想法,

感謝。

+2

請使用PHP的[內置函數來處理密碼](http://jayblanchard.net/proper_password_hashing_with_PHP.html)。如果您使用的PHP版本低於5.5,則可以使用password_hash()[兼容包](https://github.com/ircmaxell/password_compat)。您還應該使用準備/參數化查詢'SELECT * FROM user WHERE id =?'來避免sql注入。關於主題:按名稱選擇用戶,然後使用'password_verify()' – JimL

+0

**比較存儲的散列**警告**:編寫您自己的訪問控制層並不容易,並且有很多機會使其嚴重錯誤。請不要在[Laravel](http://laravel.com/)等任何現代開發框架(http://codegeekz.com/best-php-frameworks-for-developers/)上編寫自己的認證系統,內置了強大的[認證系統](https://laravel.com/docs/5.2/authentication)。絕對不要遵循[推薦的安全最佳實踐](http://www.phptherightway.com/#security),也絕不要將密碼存儲爲純文本格式。 – tadman

回答

0

您可以將密碼$user_pass = mysqli_real_escape_string($con,$_POST['user_pass']); sha1轉換並將其傳遞給查詢。

但更好的辦法是用用戶名收集用戶信息,並檢查轉換後的密碼(sha1)字符串與您從查詢中檢索到的密碼列值。

+1

謝謝。我使它工作:) – Theo

+0

很高興幫助你:) – Mujahidh

+0

SHA1是完全不足的。至少使用'password_hash'。 – tadman