【发布时间】:2012-07-13 19:41:36
【问题描述】:
我终于找到了一种我认为是执行查询的好、安全和快速的方法,但我想在我在整个网站上实施它之前完全确定。
我的代码:
$email = $_POST['email'];
$displayName = $_POST['displayName'];
$pass = $_POST['pass1'];
if($stmt = $link -> prepare("INSERT INTO profiles (email, displayName, password) VALUES (?, ?, md5(?))")) {
/* Bind parameters
s - string, b - boolean, i - int, etc */
$stmt -> bind_param("sss", $email, $displayName, $pass);
/* Execute it */
$stmt -> execute();
echo "You are now registered.<br />";
echo "<a href=\"login.php\">Login</a>";
/* Close statement */
$stmt -> close();
}
顺便说一句,stmt 是什么意思/代表什么?
编辑,新代码:
/* Create a prepared statement */
$stmt = $link -> prepare("INSERT INTO profiles (email, displayName, password,
dateRegistered) VALUES (?, ?, md5(?), NOW())");
if ( false===$stmt ) {
die('prepare() failed: ' . htmlspecialchars($link->error));
}
$rc = $stmt -> bind_param("sss", $email, $displayName, $pass);
if ( false===$rc ) {
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}
/* Execute it */
$rc = $stmt->execute();
if ( false===$rc ) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
echo "You are now registered.<br />";
echo "<a href=\"login.php\">Login</a>";
/* Close statement */
$stmt -> close();
【问题讨论】:
-
谢谢,应该知道了。 :P
-
你不应该使用 md5() 作为密码,它不再安全了。 (并且不使用盐就永远不会安全)。您应该始终在 PHP 端进行散列,否则密码可能/将最终出现在 MySQL 日志文件中。
标签: php security prepared-statement