因此,每个页面都会查找4件事,$e
,$w
,$n
和$success
。
$ e是设置错误的数组。在每个函数中,错误都使用$e[] = 'error message';
相同的$w
用于警告,$n
用于通知,而明显用于$success
。
现在如果函数有错误,它不应该继续执行它设置的操作,因为通知和警告仅用于通知目的。
页面检查所有这些并以可关闭的彩色div显示每个页面。
所以这是我的问题......
我认为这不会按预期工作。
这会返回变量本身,还是只在函数运行时显示变量的内容?
function add_member()
{
global $dbh;
if (!isset($_POST['username'],$_POST['password'],$_POST['email'])) {
$e[] = 'We need the minimum of a username and password to add a new user';
}
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (!preg_match($regex, $_POST['email'])) {
$e[] 'The email address is wrong';
}
$stmt = $dbh->prepare("SELECT `username` FROM `1_members` WHERE `username`=? LIMIT 1");
$username = strtolower($_POST['username']);
$username = trim($username);
$stmt->bindValue(1, $username, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->rowCount()) {
$e[] = 'That username is already in use';
}
if (($_POST['password']) !== ($_POST['vpassword'])) {
$e[] = 'The passwords did not match.';
}
if (strlen($_POST['username']) <= '3') {
$e[] = 'Username too short - needs to be 4 or more chars.';
}
if (strlen($_POST['password']) <='5'){
$e[] = 'password not long enough, please make it 6 chars.';
}
if (!isset($_POST['vpassword'])) {
$w[] = 'Warning: You didn\'t verify the password. Incase of typo the password was ['..']';
}
if (!isset($_POST['email'])) {
$w[] = 'Warning: You didn\'t enter an email address the password. This member will not get email alerts without an email address.';
}
if ($e) {
return $e;
}
else {
$password = trim($_POST['password']);
$options = ['cost' => 12,];
$password = password_hash($password, PASSWORD_BCRYPT, $options);
$email = trim($_POST['email']);
$stmt = $dbh->prepare("INSERT INTO `1_members` (`username`, `password`, `email`) VALUES(?,?,?)");
$stmt->bindValue(1,$username,PDO::PARAM_STR);
$stmt->bindValue(2,$password,PDO::PARAM_STR);
$stmt->bindValue(3,$email,PDO::PARAM_STR);
$stmt->execute()
$success = 'added member '.$username.'';
if (isset($w)) {
return $w;
}
if (isset($n)) {
return $n
}
return $success;
}
}
答案 0 :(得分:0)
这个函数可能会返回4个不同的东西,除非在数据库设置的某处出现大量错误,否则它不会显示任何内容。相反,它会返回变量:
$e
(数组)如果由于验证错误或重复而无法创建帐户$w
(数组)如果帐户创建后出现警告$n
(数组)如果创建后有通知 - 除了从不发生。 $ n未声明。$success
(字符串)否则这个功能到处都是 - 不可预测的。无论如何调用它都需要做一堆逻辑来处理它的回报。可能更好的方法是返回更丰富的构造。例如:
function add_member() {
$return_info = array(
"errors" => array(),
"warnings" => array(),
"notices" => array(),
"created" => false # sometimes a simple true / false is good enough
);
// do validation as before
// ...
if( $_POST['password'] !== $_POST['vpassword'] ) {
$return_info["errors"][] = "Passwords didn't match.";
}
// if account IS created, just flip the created switch
// ...
$try_inserting = $stmt->execute();
if( $try_inserting ) { # replace this with an actual error check
$return_info["created"] = true;
}
// now everything comes back: creation status, warnings, errors, etc...
return( $return_info );
}
这个函数重构标准化了它的所有返回,因此它在处理它时是可预测的。你可以做简单的陈述,如:
$attempt = add_member();
if( $attempt["created"] === true ) {
echo "added member!";
}
无论调用什么,这也可以对错误,警告和通知进行非常简单的检查:
if( !empty($attempt["errors"]) ) {
// display the errors
}