我刚开始学习php,并决定挑战编写注册和登录脚本。但是我不确定该如何继续。在你查看我的代码之前,请不要像那些喜欢唠叨sql注入和bobby表的人一样。我明白这些问题,但目前并没有考虑它们。
无论如何,我使用whirlpool算法和盐来加密密码。目前盐是静态的,我不确定如何使用Time()函数实现动态盐值,而不会在数据库中暴露。我正在尝试解决的问题是如何检查用户输入的用户名和哈希密码是否匹配。
这是我的当前代码,我认为最重要的部分将在login.php文件中。
<?php
//connect.php
$connection = mysql_connect("localhost","root","") or die("Couldn't connect to database");
mysql_select_db("test", $connection);
?>
<?php
//index.php
include 'connect.php';
define('salt','7hPqMO(m=F+!!L6(#Yhp-CdF, &Q}+cIrA;c@wcP(E--V<qRmq!v*aSnM;H=4cD0');
if(isset($_POST['username']) && isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['password'])) {
$username = mysql_real_escape_string($_POST['username']);
$fullname = mysql_real_escape_string($_POST['fullname']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$encryptedPassword = hash( 'whirlpool',salt.$password );
$sql="INSERT INTO `users` (username, fullname, email, password)
VALUES
('$username', '$fullname','$email','$encryptedPassword')";
if(!mysql_query($sql,$connection)) {
die('Error: ' . mysql_error());
} else {
mysql_close($connection);
header('Location: login.php?redirectedFromRegister=1');
}
}
?>
<html>
<body>
<form action="index.php" method="post">
<h1>REGISTER</h1><hr/>
Email: <input type="text" name="email"/><br/>
Full Name:<input type="text" name="fullname"/><br/>
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<input type="submit" value="REGISTER"/>
</form>
</body>
</html>
<?php
//login.php
include 'connect.php';
if(isset($_GET['redirectedFromRegister'])==1) {
echo 'Thank you for registering, you may now login';
} else {
echo '<h1>LOGIN</h1>';
}
?>
<html>
<body>
<form action="index.php" method="post">
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<input type="submit" value="LOGIN"/>
</form>
</body>
</html>
这个问题可能很简单,但是我想包含我的当前代码,以便人们可以从中学习。
如果给定盐或其他信息,如何解密哈希密码。就像我说的,我不确定该怎么做。
如果您没有在connect.php文件中看到它,我的数据库信息如下:
Database : Localhost
Username : Root
Password : ""
Table : users
Table layout :
------------------------------------------------------------------
| Id | Username | Password | Email | Fullname |
------------------------------------------------------------------
| 1 |BobbyTables | HASHED_PASS|email@E.com | Max Mastalerz |
| 2 | SteveJo | HASHED_PASS|steve@jobs.com| Steve Jobs |
| 3 | MarkZuck | HASHES_PASS|MarkZ@fb.com | Mark Zuckerberg|
------------------------------------------------------------------
这些代码也可以在pastebin上轻松获取:
Connect.php : http://pastebin.com/8ft12kG5 Index.php : http://pastebin.com/TqrJhzdu login.php : http://pastebin.com/9fR3HJhe