2016-03-23 114 views
-1
<?php 
$conn=mysqli_connect("localhost", "root", "","users"); 

if (!$conn) { 
    echo "Bad connection!!!"; 
} 

     $user_name=$_POST['user_name']; 
     $user_password=$_POST['user_password']; 


$sql_check=mysqli_query($conn, "SELECT user_name, password FROM user_info WHERE user_name='$user_name' AND password='$user_password'") or die("Bad sql query"); 

if (mysqli_num_rows($sql_check)>0) { 
    echo "user exists"; 
} 



else { 
    $sql_insert=mysqli_query($conn, "INSERT INTO user_info (id, user_name, password) VALUES (null,'$user_name', '$user_password')"); 
    echo "New user added!!!"; 
} 
?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 



<form method="POST" action="pdo_konekcija.php"> 
<input type="text" name="user_name"> 
<input type="password" name="user_name"> 
<input type="submit" name="btn_submit" value="REGISTER"> 
</form> 


</body> 
</html> 

我有用戶註冊的基本形式。我無法找到檢查用戶是否存在的最佳方式,所以如果不是,我想要註冊新用戶,就像您從sql語句中看到的一樣。我如何在user_password字段中包含hash()作爲密碼?這兩個字段必須填寫以便檢查和註冊過程。我可以使用這種mysql我程序的方式來防止SQL注入或不?我正在建立註冊/登錄從頭開始,所以需要幫助,謝謝大家。安全註冊,用哈希登錄()

+1

**危險**:您很容易[SQL注入攻擊](http://bobby-tables.com/)**,您需要[防禦](http://stackoverflow.com/問題/ 60174/best-way-to-prevent-sql -injection-in-php)自己從。 – Quentin

回答

0

爲了檢查用戶密碼是否匹配,首先檢查用戶是否存在並檢索他們的密碼散列,然後通過你的散列驗證功能運行它。

回答你的第二個問題,是的,你現在的系統很容易被SQL注入。

1

使用PHP的內置函數進行哈希和驗證密碼,但這需要版本5.5.0及以上版本。 http://php.net/manual/en/function.password-hash.php

將salt存儲在註冊時創建的數據庫中。 當用戶登錄時,檢查它們是否存在,獲取它們的salt並使用password_verify進行驗證。

使用這種數據處理停止SQL注入

有在github(https://github.com/ircmaxell/password_compat)兼容的版本,如果你沒有5.5.0及以上時,做預處理語句。