sha('$password') 返回空集
我有两个问题。 问题一: 我正在尝试创建一个注册表单,用户可以在我的网站上注册。 当我运行此 mysql 语句时,发现错误:
$sql "insert into users(username, password) values('$username, sha('$password'))";
重复条目 'da39a3ee5e6b4b0d3255bfef95601890afd80709' for key 'password' 尽管我多次更改了字符串 sha('$password') 。 请帮忙。
else{
include("databaseconnection.php");
$databaseconnect = connect($host,$user,$password,$database)
or die("couldnot connect to database serever.\n");
$database_select = mysql_select_db($database,$databaseconnect)
or die("could not select dabases.\n " .mysql_error());
$query2 = "insert into company(username,password)
values('$username',sha1('$password'))";
$result2 = mysql_query($query2,$databaseconnect);
echo "you have been registered as '$companyloginName' <br/>";
header("Location:/index.php");
我的登录php脚本如下:
$result ="select username, password form users where username ='$username' and password = sha('$password');
if(mysql_num_rows($reuslt)==1){
echo"welcome '$username";
}
I have two problems.
problem one:
I am trying to create a registeration form where users can register with my website.
when I run this mysql statement a get dublicate entry found error:
$sql "insert into users(username, password) values('$username, sha('$password'))";
Duplicate entry 'da39a3ee5e6b4b0d3255bfef95601890afd80709' for key 'password'
despite the fact that I changed the the string sha('$password') several times.
please help.
else{
include("databaseconnection.php");
$databaseconnect = connect($host,$user,$password,$database)
or die("couldnot connect to database serever.\n");
$database_select = mysql_select_db($database,$databaseconnect)
or die("could not select dabases.\n " .mysql_error());
$query2 = "insert into company(username,password)
values('$username',sha1('$password'))";
$result2 = mysql_query($query2,$databaseconnect);
echo "you have been registered as '$companyloginName' <br/>";
header("Location:/index.php");
my login php script is as follow:
$result ="select username, password form users where username ='$username' and password = sha('$password');
if(mysql_num_rows($reuslt)==1){
echo"welcome '$username";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我强烈建议不要单独使用 MySQL 的
sha()
或 PHP 的sha1()
来进行密码哈希处理。 如果您的数据库遭到破坏,这对您的用户来说将是一个巨大的安全风险。请花点时间阅读我之前关于密码哈希主题的回答以正确保护您的数据。
其次,您的代码容易受到 SQL 注入攻击。使用
mysql_real_escape_string()
转义之前要放入查询中的变量-手。第三,您的
$password
变量被databaseconnection.php
文件覆盖。为了强调...
因此,<稍后在查询中使用的 code>$password 仍然包含数据库连接的密码,而不是用户的密码。
更改databaseconnection.php中变量的名称,或者更好的是,使用数组来保存所有配置。
然后,按如下方式更改代码:
由于在调用
mysql_connect()
时已经传递了数据库,因此不需要调用mysql_select_db()
。First, I would STRONGLY advice against using MySQL's
sha()
or PHP'ssha1()
alone for password hashing purposes. This is a huge security risk for your users if your database gets compromised.Please take the time to read my previous answer on the subject of password hashing to properly secure your data.
Second, your code is vulnerable to an SQL Injection attack. Use
mysql_real_escape_string()
to escape the variables you are going to put in your query before-hand.Third, your
$password
variable is being overwritten by yourdatabaseconnection.php
file.To put emphasis...
Therefore, the
$password
used later on in your query still contains the password for the database connection, not your user's password.Change the name of your variable in
databaseconnection.php
or even better still, use an array to hold all the configuration.Then, change your code as follows:
Since you are already passing the database when calling
mysql_connect()
, you do no need to callmysql_select_db()
.da39a3ee5e6b4b0d3255bfef95601890afd80709
是空字符串的 sha1 哈希值。确保您确实将密码插入到 SQL 查询中,例如通过回显查询而不是将其发送到 SQL 服务器。编辑将新信息添加到您的问题后,请检查这两行:
这里,
$password
是用于连接到数据库的密码 。包含databaseconnection.php可能会覆盖之前在$password
变量中的内容。尝试
echo $query2
,您可能会亲眼看到,SQL 查询根本不包含任何密码,或者其中的密码与用户输入的密码不同。da39a3ee5e6b4b0d3255bfef95601890afd80709
is the sha1 hash of the empty string. Make sure that you actually insert the password into your SQL query, for example by echoing the query instead of sending it to the SQL server.Edit With the new information added to your question, check out these two lines:
Here,
$password
is the password used to connect to the database. The inclusion of databaseconnection.php probably overwrites what was previously in the$password
variable.Try to
echo $query2
and you'll probably see it for yourself, that the SQL query doesn't include any password at all or that the password therein is not the same as the one entered by the user.从注释行猜测,您可能不小心使用了“databaseconnection.php”中设置的连接密码而不是用户密码 - 您没有显示如何初始化 $password 字符串。
另请注意您的sql中不应该出现的逗号:
我还没有测试这是否是原因,但您可能应该摆脱它并再次测试它。
另外,请认真考虑 pdo/prepared 语句以防止 sql 注入,如果您想从用户输入插入密码,则更是如此。
Guessing from the commented line, it may be possible you accidentally use the connection password that is set in 'databaseconnection.php' rather than the user password - you don't show how you initialize the $password string.
Also note the comma in your sql that shouldn't be there:
I have not tested if that is the cause, but you should probably get rid of it and test it again.
Also, seriously consider pdo / prepared statements to prevent sql-injections, even more so if you want to insert the password from user input.