2016-03-23 116 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及以上时,做预处理语句。