我的一个注册页面上有以下代码。我正试图弄清楚如何只在没有错误的情况下生成电子邮件。。。现在,它无论如何都会发送电子邮件,所以我收到了相互矛盾的电子邮件。
<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
//Prevent the user visiting the logged in page if he/she is already logged in
if(isUserLoggedIn()) { header("Location: account.php"); die(); }
//Forms posted
if(!empty($_POST))
{
$errors = array();
$email = trim($_POST["email"]);
$username = trim($_POST["username"]);
$displayname = trim($_POST["displayname"]);
$password = trim($_POST["password"]);
$confirm_pass = trim($_POST["passwordc"]);
$captcha = md5($_POST["captcha"]);
if ($captcha != $_SESSION['captcha'])
{
$errors[] = lang("CAPTCHA_FAIL");
}
if(minMaxRange(4,25,$username))
{
$errors[] = lang("ACCOUNT_USER_CHAR_LIMIT",array(4,25));
}
if(!ctype_alnum($username)){
$errors[] = lang("ACCOUNT_USER_INVALID_CHARACTERS");
}
if(minMaxRange(4,60,$displayname))
{
$errors[] = lang("ACCOUNT_DISPLAY_CHAR_LIMIT",array(4,60));
}
if(minMaxRange(4,50,$password) && minMaxRange(4,50,$confirm_pass))
{
$errors[] = lang("ACCOUNT_PASS_CHAR_LIMIT",array(4,50));
}
else if($password != $confirm_pass)
{
$errors[] = lang("ACCOUNT_PASS_MISMATCH");
}
if(!isValidEmail($email))
{
$errors[] = lang("ACCOUNT_INVALID_EMAIL");
}
//End data validation
if(count($errors) == 0)
{
//Construct a user object
$user = new User($username,$displayname,$password,$email);
//Checking this flag tells us whether there were any errors such as possible data duplication occured
if(!$user->status)
{
if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
if($user->displayname_taken) $errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
if($user->email_taken) $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
}
else
{
//Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
if(!$user->userCakeAddUser())
{
if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
if($user->sql_failure) $errors[] = lang("SQL_ERROR");
}
}
}
if(count($errors) == 0) {
$successes[] = $user->success;
}
}
echo resultBlock($errors,$successes);
$to = 'myemail@domain.com';
$subject = 'New User Signup';
$url = 'mydomain.com/account.php';
$headers .= "From: myemail@domain.com'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$message .= '<html><body>';
$message .= "<p>...message contents...</p>";
mail($to, $subject, $message, $headers);
echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
?>
我确信这是因为我把电子邮件放错了地方,但当我试图把它转移到其他地方时,我会遇到各种错误。
提前感谢您提供的任何帮助。
像这样将邮件代码包装在里面:
if(count($errors) == 0) {
$to = 'myemail@domain.com';
$subject = 'New User Signup';
$url = 'mydomain.com/account.php';
$headers .= "From: myemail@domain.com'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$message .= '<html><body>';
$message .= "<p>...message contents...</p>";
mail($to, $subject, $message, $headers);
}
两个if(count($errors) == 0)
条件都在做完全相同的事情。您不需要添加新条件,只需删除一个即可。您的脚本可以简化如下(伪代码):
// some processing
// ...
//Forms posted
if(!empty($_POST))
{
// filling $errors[]
//End data validation
if(count($errors) == 0)
{
//Construct a user object
// ...
//Checking this flag tells us whether there were any errors such as possible data duplication occured
// ...
$successes[] = $user->success;
// Send email if no error
// ...
mail($to, $subject, $message, $headers);
}
}
echo resultBlock($errors,$successes);
$url = 'mydomain.com/account.php';
echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';