所以我一直在寻找互联网来加密我的密码,我从 youtube 上得到了这个代码,我不确定这是否是我需要的,因为这是我第一次做这样的事情。而且我收到一个错误,说用户、密码和邮件是未定义的,在我使用的函数和上面的 if 语句中你的工作正常。这段代码正在进行中,我还没有在 if 语句中添加 salting 和 crypt 函数。继承人代码:
<?php require_once("session.php");?>
<?php require_once("connect.php");?>
<?php
if(isset($_POST["createUser"]) && isset($_POST["user"], $_POST["pass"], $_POST["mail"])){
createUser ($_POST["user"], $_POST["pass"], $_POST["mail"]);
}
else if(isset($_POST["userGames"])){
userGamesUpdate ($_POST("selectedGameId"));
}
else if(isset($_POST["userLogin"])){
login ($_POST["user"], $_POST["pass"]);
}
else if (!isset ($_POST)){
echo "error";
}
?>
<?php
$lenght = 21;
function random_string($lenght){
$charset = array_merge˙(range("a", "z"), range("A", "Z"), range("0", "9"));
shuffle($charset);
global $randozoz;
$randozoz = array_slice($charset, 0, $lenght);
return implode("", $randozoz);
}
$username = $_POST["user"];
$password = $_POST["pass"];
$email = $_POST["mail"];
function prevent($wha) {
$wha = stripslashes($wha);
$wha = mysql_real_escape_string($wha);
return $wha;
}
prevent($username);
prevent($password);
prevent($email);
$str = substr($username, 0, 6);
$salt = "$2a$12$".$randozoz."".$str."$";
//register
function createUser ($username, $password, $email ){
//Get from form
$hashedPassword = crypt($password);
//Submit to database
//make query
$query = ("INSERT INTO users ( username , email , hashPass ) VALUES ( '$username' , '$email' , '$hashedPassword')");
//use query
if (mysql_query($query)) {
//userMade.php
$query = ("SELECT id FROM users WHERE username = '{$username}'");
$userId = mysql_fetch_array($query);
$_SESSION["userId"] = $userId;
$_SESSION["username"] = $username;
header("Location: ../userMade.php");
exit;
} else {
echo "<p>".mysql_error()."</p>";
}
}
function login ($username, $password){
$hashedPassword = sha1($password);
//Submit to database
//make query
$query = ("SELECT id, username FROM users WHERE username = '{$username}' AND hashPass = '{$hashedPassword}'");
$result_set = mysql_query($query);
if (mysql_num_rows($result_set) == 1) {
//frontpage, session
//$userFound = mysql_fetch_array($result_set);
$foundUser = mysql_fetch_array($result_set);
$_SESSION["userId"] = $foundUser["id"];
$_SESSION["username"] = $foundUser["username"];
header("Location: ../frontpage.php");
exit;
} else {
$message = "Your username and/or password is wrong! Please try again.";
}
}
?>
登录系统未与 crypt 和 salting 系统集成。但是我做盐腌对吗?
以下是来自该系统的一些随机密码:
$1$gc5.pn/.$txhBJZBDG3xgraJYIYY0I1
$1$8e0.vX3.$RfFLRT/iE.MXfws2fnz30.
并且错误部分显示了我试图防止 SQL 注入的地方,该系统也可以工作吗?老实说,我真的不明白。