我有一个小问题。
我制作了一个PHP脚本,应该将数据插入到我的数据库中,但遗憾的是......它不起作用,但它之前已经有效了。我现在正在主机上运行我的脚本,这可能是问题所在..但我认为不是。
代码:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$usernameErr = $emailErr = $passwordErr = $password_valErr = "";
$username = $email = $password = $password_val = "";
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(empty($_POST['username'])) {
$usernameErr = "Name is required";
} else {
$username = validate_input($_POST['username']);
if(strlen($username) <= 3) {
$usernameErr = "Username must be 4 characters or longer.";
}
if(strlen($username) > 26) {
$usernameErr = "Username can't be longer as 26 characters.";
}
if(!preg_match("/^[a-zA-Z ]*$/", $username)) {
$usernameErr = "Only letters and white space allowed.";
}
}
if(empty($_POST['email'])) {
$emailErr = "Email is required";
} else {
$email = validate_input($_POST['email']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format.";
}
}
if(empty($_POST['password'])) {
$passwordErr = "Password is required";
} else {
$password = validate_input($_POST['password']);
if(strlen($password) <= 5) {
$passwordErr = "Password must be 6 characters or longer.";
}
if(strlen($password) > 26) {
$passwordErr = "Password can't be longer as 26 characters.";
}
if(!preg_match("#[0-9]+#", $password)) {
$passwordErr = "Password must contain atleast 1 number.";
}
}
if(empty($_POST['password_val'])) {
$password_valErr = "Password_val is required";
} else {
$password_val = validate_input($_POST['password_val']);
if($password_val != $password) {
$password_valErr = "Password_val must be equal to password.";
}
}
if($usernameErr == '' && $emailErr == '' && $passwordErr == '' && $password_valErr == '') {
$check_user = mysqli_query($conn, "SELECT * FROM users WHERE username='".trim($username)."'");
$check_mail = mysqli_query($conn, "SELECT * FROM users WHERE email='".trim($email)."'");
if(mysqli_num_rows($check_user) > 0) {
echo 'This username allready exists';
} elseif(mysqli_num_rows($check_mail) > 0) {
echo 'This email address is already registered.';
} else {
$username = mysql_real_escape_string(trim($username));
$email = mysql_real_escape_string(trim($email));
$password = mysql_real_escape_string(trim($password));
$rand_salt = randString();
/*$final_pass = password_hash($password_val, PASSWORD_DEFAULT)."\n";*/
$final_pass = sha1($password.PASSWORD_SALT.$rand_salt);
$privileges = 0;
$sql = "INSERT INTO users (username,password,salt,email)
VALUES ('".$username."','".$final_pass."','".$rand_salt."','".$email."')";
if($conn->query($sql) === TRUE) {
echo "User registered.";
} else {
echo 'Error: ' . $sql . '<br>' . $conn->error;
}
}
}
}
?>
<table border="1">
<tr>
<td><label>Username</label><?=' <b>' . $usernameErr . '</b>';?></td>
<td><input type="text" name="username" value="<?=$username;?>" placeholder="Enter your desired username..." /></td>
</tr>
<tr>
<td><label>E-mail</label><?=' <b>' . $emailErr . '</b>';?></td>
<td><input type="text" name="email" value="<?=$email;?>" placeholder="Enter your email address..." /></td>
</tr>
<tr>
<td><label>Password<?=' <b>' . $passwordErr . '</b>';?></label></td>
<td><input type="password" name="password" placeholder="Enter your desired password..." /></td>
</tr>
<tr>
<td><label>Repeat Password<?=' <b>' . $password_valErr . '</b>';?></label></td>
<td><input type="password" name="password_val" placeholder="Repeat your chosen password.." /></td>
</tr>
<tr>
<td><input type="submit" name="register" value="Register" /></td>
</tr>
</table>
</form>
此代码应该有效,但第一个问题是:
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
在这种情况下,此代码必须将我发送到同一页面register.php,但不知何故它将我发送到index.php页面。因此,如果我删除此代码并将操作保留为空或只是在其中输入register.php,则会插入数据,但用户名和电子邮件除外,它们都是变体。
我希望有人可以帮助我,
谢谢你!答案 0 :(得分:1)
您正在使用mysql_
功能并连接mysqli_
API。
您无法混合这些功能。您必须使用与连接相同的MySQL API进行查询。
您需要将mysql_real_escape_string
的所有实例更改为mysqli_real_escape_string($conn, $_POST['variable'])
variable
是您的POST阵列。
另外,请确保您确实与mysqli_
而非mysql_
或PDO联系。
mysqli_
函数,也不确定要连接哪个MySQL API。在打开PHP标记后立即将错误报告添加到文件的顶部。
例如<?php error_reporting(E_ALL); ini_set('display_errors', 1);
然后代码的其余部分,看它是否产生任何结果,
以及or die(mysqli_error($conn))
到mysqli_query()
。
旁注: sha1()
不被认为是存储哈希值最安全的方法。
从ircmaxell的答案https://stackoverflow.com/a/29778421/拉出来,并使用PDO准备好的陈述和password_hash()
。
只需使用图书馆。认真。它们存在是有原因的。
password_hash()
password-compat
(上面的兼容包不要自己动手。如果你正在创造自己的盐,你做错了。你应该使用一个为你处理这个问题的库。
$dbh = new PDO(...);
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $dbh->prepare("insert into users set username=?, email=?, password=?");
$stmt->execute([$username, $email, $hash]);
登录时:
$sql = "SELECT * FROM users WHERE username = ?";
$stmt = $dbh->prepare($sql);
$result = $stmt->execute([$_POST['username']]);
$users = $result->fetchAll();
if (isset($users[0]) {
if (password_verify($_POST['password'], $users[0]->password) {
// valid login
} else {
// invalid password
}
} else {
// invalid username
}