发生故障时,尝试在PHP脚本内的FAIL上停止电子邮件警报。

时间:2012-04-28 01:36:47

标签: php mysql email if-statement exit

我刚刚发现,当出现错误时,我仍然收到一封电子邮件(reg_add_fail.php)。如果客户端被定向到reg_add_fail.php,是否可以阻止脚本通过电子邮件发送给我?困惑...

我简化了脚本以缩小。

非常感谢。

埃里克

<?

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
 <tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ; 

$order = "INSERT INTO reg_add (submit_date, 
connect_date, 
reg, 
first_name, 
)

VALUES

('$submit_date',
'$_POST[connect_date]', 
'{$_POST[reg]}nv', 
'$_POST[first_name]')";

$result = mysql_query($order);

if ($result) { 
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}
?>

2 个答案:

答案 0 :(得分:0)

exit()放在重定向

之后
header("location: reg_add_fail.php");
exit();

仅仅因为你调用header()并不意味着脚本会立即停止执行。致电exit()将会。

答案 1 :(得分:0)

在您的代码中,PHP mail()函数在IF语句之外被调用,因此,您始终可以收到电子邮件。

要仅在查询运行且没有错误时发送电子邮件,请将mail()放在if语句中!

<强> PHP

if ($result) { 
  mail($to, $subject, $msg, $headers);  
  $reg =          $_REQUEST['reg'] ; 
  $first_name =   $_REQUEST['first_name']; 
  header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
  header("location: reg_add_fail.php"); 
  exit(); // as sugested by John Conde
}

编辑显示完整的代码:

<?php

// Email Recipient
$to = 'newreg@41q.org';

// Email Subject
$subject = 'New Homeless Connection';


// Email Message
$msg = '
<html>
  <head>
    <title>New Homeless Connection</title>
  </head>
  <body>
    <table cellspacing="0" cellpadding="10" border="1" align="left">
      <tr>
        <td align="left" width="150px">Registery No.:</td>
        <td align="left">'.$reg.'</td>
      </tr>
      <tr>
        <td align="left">First Name:</td>
        <td align="left">'.$first_name.'</td>
      </tr>
      <tr>
        <td align="left">Connection Date:</td>
        <td align="left">'.$connect_date.'</td>
      </tr>
      <tr>
        <td align="left" colspan="2"><a href="http://www.41q.org/admin/" title="">http://www.41q.org/admin/</a></td>
      </tr>
    </table>
    <br>
    <br>
  </body>
</html>';

// Email Headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";


date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ; 

// Prepare Database Query
$order = "
  INSERT INTO reg_add (
    submit_date,
    connect_date,
    reg,
    first_name
  )
  VALUES (
    '".$submit_date."',
    '".$_POST['connect_date']."', 
    '".$_POST['reg']."nv', 
    '".$_POST['first_name']."'
  )";

// Query Database
$result = mysql_query($order);


// Check If the result is valid
if ($result) { 

  // send email
  mail($to, $subject, $msg, $headers);  

  // prepare and direct the user to the reg_add_success Page
  $reg =          $_REQUEST['reg'] ; 
  $first_name =   $_REQUEST['first_name']; 
  header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 

} 
else {

  // send the user to the reg_add_fail Page
  header("location: reg_add_fail.php"); 

  // exit from the script
  exit();

}

?>