php mail()发送多个重复的电子邮件

时间:2013-06-05 20:49:45

标签: php mysql

我遇到了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>";
  }
}
?>

谢谢!

5 个答案:

答案 0 :(得分:1)

你的while循环没有任何意义。
实际上,您遍历所有用户(数据库中的所有行),并且每次新用户与while循环中的当前行不匹配时,您都会将新用户添加到数据库并每次都发送电子邮件。

这是你应该做的:
您的查询

$sql = "SELECT * FROM members";

是通用的方式 使用MySql来获得它的好处,让数据库通过迭代结果集来找到匹配而不是你的php脚本。
使用这样的查询:

$sql = "SELECT count(*) as count FROM members WHERE username LIKE '$username'";
$result = mysql_query($sql);

然后检查$result['count']是否等于0.如果是这样的话,新用户还不存在,您可以创建新用户并发送电子邮件。

答案 1 :(得分:1)

您的问题在于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,则调用mail函数一次,否则显示错误。

// 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>";
}

答案 2 :(得分:0)

您正在mail()循环中执行while(),其中包含数据库中的所有用户。

在该条件下基于if语句,每次用户提供的用户名与当前行匹配且密码匹配时,您正在执行插入并发送电子邮件。据推测,你的几个用户有很多相同的密码。

您需要更新查询以包含条件,以便从结果集中排除不匹配的用户。

答案 3 :(得分:0)

如果用户名不匹配,但是如果用户名不匹配,则会循环显示所有成员,但是添加用户并发送电子邮件。用户名几乎永远不会相同,密码可能......

您应该将查询更改为仅查询该特定用户的数据库,例如

$sql = "SELECT * FROM {成员{1}}

不是您问题的答案,但您可以将代码的第一行缩短为:

WHERE username LIKE \"" . $username . \"";

短得多。

答案 4 :(得分:0)

您的邮件功能在while循环中,因此它发送了很多电子邮件。请剪切该代码并将其放在循环的上方或下方。其次,查询错误,$ sql =“SELECT * FROM members”;将选择所有成员,使用$ sql =“SELECT * FROM members where .....”;我不知道列名。阅读,http://www.w3schools.com/php/php_mysql_where.asp