gpt4 book ai didi

php mail() 发送多封重复邮件

转载 作者:行者123 更新时间:2023-11-29 06:48:39 26 4
gpt4 key购买 nike

我在使用 PHP mail() 函数时遇到了一个主要问题。我有一个用户注册页面,该页面会生成一封电子邮件以验证他们的电子邮件地址。不幸且奇怪的是,该功能会向同一用户发送 6 或 7 到 90 多封电子邮件。我不明白这是怎么发生的或为什么会发生,即使在浏览了这里其他人的帖子后也是如此。

有人可以帮我调试吗?

这是代码:

$first_name = mysql_real_escape_string($_POST['first_name']);
$last_name = mysql_real_escape_string($_POST['last_name']);
$email = mysql_real_escape_string($_POST['email']);
$username = strtoupper(mysql_real_escape_string($_POST['username']));
$password1 = mysql_real_escape_string($_POST['password1']);
$password2 = mysql_real_escape_string($_POST['password2']);
$termsofuse = mysql_real_escape_string($_POST['termsofuse']);
$status = mysql_real_escape_string($_POST['status']);
$approved = mysql_real_escape_string($_POST['approved']);
$acctype = mysql_real_escape_string($_POST['acctype']);
$industry = mysql_real_escape_string($_POST['industry']);
$newsletter = mysql_real_escape_string($_POST['newsletter']);
$contactname = mysql_real_escape_string($_POST['contactname']);
$contactnumber = mysql_real_escape_string($_POST['contactnumber']);

// Hashing of $password1
$password1 = sha256($password1);
$password2 = sha256($password2);

$hash = hash('sha256', $username);

// Check for existing username
$sql = "SELECT * FROM `members`";
$result2=mysql_query($sql);
while($row=mysql_fetch_array($result2)){
$username2 = $row['username'];

// If $username doesn't equal $username2 (meaning there isn't an existing username, and both passwords match, write to database
if($username <> $username2 && $password1 === $password2){
$sql = "INSERT INTO `members` (`id`, `first_name`, `last_name`, `email`, `username`, `password`, `termsofuse`, `status`, `approved`, `acctype`, `industry`, `newsletter`, `contactnumber`, `hash`, `since`) VALUES (NULL, '$first_name' , '$last_name' , '$email' , '$username' , '$password1' , '$termsofuse', 'Reg', '$approved', '$acctype', '$industry', '$newsletter', '$contactnumber', '$hash', NOW())";
$result = mysql_query($sql) or die ("Can't insert".mysql_error());
$to = $email; // Send email to user
$subject = 'Signup Verification'; //Subject line in email
$message = 'Welcome ' . $first_name . ','
. "\r\n\r\n"
. 'Thanks for signing up!'
. "\r\n\r\n"
. 'Your account has been created. To activate your account, click on the link below to get started!'
. "\r\n\r\n"
. 'http://www.radioman911.com/pages/CAD/verify.php?email=' . $email . '&hash=' . $hash . '';
$headers = 'From: xxxx' . "\r\n" .
'Reply-To: same xxxx as above' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers, '-fxxxx same as above'); //Send the email
header("location:new_member_sucess.php"); //yes, i know i spelled success wrong, but i also spelled it wrong in the page filename lol

} else {
echo "<style type='text/css'>A{text-decoration:none}</style>";
echo "<body bgcolor='black'><font color='white' style='font-family:trebuchet ms;'>";
echo "Passwords do not match or that username is already taken, please try again!<br>";
echo "<a href='javascript: history.go(-1)'><font color='red'>Go back</a></font>";
}
}
?>

谢谢!

最佳答案

您的问题在于检查重复用户名的 SQL。

// Check for existing username
$sql = "SELECT * FROM `members`";
$result2=mysql_query($sql);
while($row=mysql_fetch_array($result2)){
$username2 = $row['username'];

...

}}

我已经获取了您的代码,并做了一些小改动。我已更改您的 SQL 查询以检索具有相同用户名的用户数,而不是返回每个用户名以单独检查。

我还将 mail() 函数周围的代码从循环中取出。如果没有找到重复的用户名,则 $duplicateUsername 变量设置为 false,否则设置为 true。

如果$duplicateUsername为false,那么邮件函数被调用...一次,否则显示错误。

请从 //Check for existing username 使用以下内容:

// Check for existing username
$username = mysql_real_escape_string($username);
$duplicateUsername = false;


$sql = "SELECT COUNT(username) AS usernameCount FROM members WHERE username = '{$username}'";
$result2=mysql_query($sql);
while($row=mysql_fetch_array($result2)){
$duplicateUsername = $row['usernameCount']>0 ? true : false;
}

if(!$duplicateUsername){
$sql = "INSERT INTO `members` (`id`, `first_name`, `last_name`, `email`, `username`, `password`, `termsofuse`, `status`, `approved`, `acctype`, `industry`, `newsletter`, `contactnumber`, `hash`, `since`) VALUES (NULL, '$first_name' , '$last_name' , '$email' , '$username' , '$password1' , '$termsofuse', 'Reg', '$approved', '$acctype', '$industry', '$newsletter', '$contactnumber', '$hash', NOW())";

$result = mysql_query($sql) or die ("Can't insert".mysql_error());

$to = $email; // Send email to user
$subject = 'Signup Verification'; //Subject line in email
$message = 'Welcome ' . $first_name . ','
. "\r\n\r\n"
. 'Thanks for signing up!'
. "\r\n\r\n"
. 'Your account has been created. To activate your account, click on the link below to get started!'
. "\r\n\r\n"
. 'http://www.radioman911.com/pages/CAD/verify.php?email=' . $email . '&hash=' . $hash . '';
$headers = 'From: xxxx' . "\r\n" .
'Reply-To: same xxxx as above' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers, '-fxxxx same as above');

header("location:new_member_sucess.php");
} else {
echo "<style type='text/css'>A{text-decoration:none}</style>";
echo "<body bgcolor='black'><font color='white' style='font-family:trebuchet ms;'>";
echo "Passwords do not match or that username is already taken, please try again!<br>";
echo "<a href='javascript: history.go(-1)'><font color='red'>Go back</a></font>";
}

关于php mail() 发送多封重复邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16949608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com