我正在尝试为我的网站设置登录/注册部分,我正在努力使用户需要创建一个不能全部大写或全部小写且不少于8个字符的密码。这是我的代码,我不太确定它出错的地方或为什么它不起作用。任何想法?
if (isset($_POST['submit'])) {
include_once 'dbh.php';
$first = mysqli_real_escape_string($conn, $_POST['first']);
$last = mysqli_real_escape_string($conn, $_POST['last']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
//Error handlers
//Check for empty fields
if(empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
header("Location: ../signup.php?signup=empty");
exit();
} else {
//Check if input char are valid
if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
header("Location: ../signup.php?signup=invalid");
exit();
} else {
//Check if email is valid
if(strlen($pwd) >=8) {
if (!ctype_upper($pwd) && !ctype_lower($pwd)){
echo "great"
}
}}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?signup=email");
exit();
} else {
$sql = "SELECT * FROM users WHERE user_uid= '$uid'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: ../signup.php?signup=usertaken");
exit();
} else {
//Hashing the password
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
//Insert the user into the database
$sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
mysqli_query($conn, $sql);
header("Location: ../signup.php?signup=success");
exit();
}
}
}
}
} else {
header("Location: ../signup.php");
exit();
}
答案 0 :(得分:-1)
您在;
echo "great";
)
if (!ctype_upper($pwd) && !ctype_lower($pwd))
{
echo "great";
}
在最后一个之前还有一个多余的右括号(}
):
}
else {
header("Location: ../signup.php");
exit();
}
尝试使用正则表达式的这些模板来检查密码:
<?php
$passw = "ABFaDAasS";
print_r(preg_match("/^[A-Za-z]{8,}$/",$passw)); // Lowercase and uppercase with length 8 or more. Prints 1 or true
print_r(preg_match("/^[a-z]{8,}$/",$passw)); // Lowercase with length 8 or more. Prints 0 or false
print_r(preg_match("/^[A-Z]{8,}$/",$passw)); // Uppercase with length 8 or more. Prints 0 or false
print_r(preg_match("/^[A-Za-z0-9]{8,}$/",$passw)); // Lowercase and uppercase and all numbers with length 8 or more. Prints 1 or true
但通常在检查语法后你应该使用给定密码的哈希,可以使用md5函数,如:
echo md5("password"); //prints 5f4dcc3b5aa765d61d8327deb882cf99