PHP电子邮件验证 - wrong_code不会调用正确的操作

时间:2011-08-21 22:50:39

标签: php

我有以下电子邮件表格:

    <form action="mailer.php" method="post" name="form1" id="form1" onsubmit="MM_validateForm('from','','RisEmail','name','','R','verif_box','','R','message','','R');return document.MM_returnValue">


     <table width="500" border="0" cellpadding="2" cellspacing="0" bgcolor="#000000"><tr valign="top" align="right"> <td nowrap><font face="Verdana" size="3" color="#666666" >first name (<span class="R">*</span>)</font></td> 

    <td width="500" align="left"><input type="text" name="name" size="37" border="0" id="name" value="<?php echo $_GET['name'];?>"> </td></tr><tr valign="top" align="right"> <td nowrap><font face="Verdana" size="3" color="#666666">last name</font></td> 

   <td align="left"><input type="text" name="lastname" size="37" border="0" id="lastname" value="<?php echo $_GET['lastname'];?>"> </td></tr><tr valign="top" align="right"> <td nowrap><font face="Verdana" size="3" color="#666666">email (<span class="R">*</span>)</font></td> 

   <td align="left"><input type="text" name="from" size="37" border="0" id="from" value="<?php echo $_GET['from'];?>"> </td>
    </tr><tr valign="top" align="right"> <td nowrap><font face="Verdana" size="3" color="#666666"></font></td> 

  <td align="left"><input type=checkbox name="mailinglist" id="mailinglist" value="<?php echo $_GET['mailinglist'];?>"><font face="Verdana" size="3" color="#666666"></font><br> </td></tr><tr valign="top" align="right"> <td nowrap><font face="Verdana" size="3" color="#666666">comments (<span class="R">*</span>)</font></td> 

   <td align="left"><textarea name="message" cols="35" rows="10" border="0" id="message"><?php echo $_GET['message'];?></textarea><br> </td></tr><tr> <td colspan="2"><table cellpadding=5 cellspacing=0 bgcolor="#000000" width="100%"><tr bgcolor="#000000">

   <td class="label" colspan="2"><font color="#cccccc" face="Verdana" size="2"><b>Image Verification</b></font></td></tr><tr>

    <td> <input name="verif_box" type="text" id="verif_box" style="padding:2px; border:1px solid #CCCCCC; width:80px; height:14px;"/>&nbsp;&nbsp;<img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="top" /><br />
    <br />

    <!-- if the variable "wrong_code" is sent from previous page then display the error field -->
    <?php if(isset($_GET['wrong_code'])){?>
    <div style="border:1px solid #990000; background-color:#D70000; color:#FFFFFF; padding:4px; padding-left:6px;width:295px;">Wrong verification code</div><br /> 
    <?php }?>


   </td><td class="field" valign="bottom">

  <div><input name="Submit" type="submit" style="margin-top:10px; display:block; border:1px solid #000000; width:100px; height:20px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; padding-left:2px; padding-right:2px; padding-top:0px; padding-bottom:2px; line-height:14px; background-color:#EFEFEF;" value="Send Message"/>

  <input type="reset" class="btn" value="  clear  " name="Clear" border="0" style="margin-top:10px; display:block; border:1px solid #000000; width:100px; height:20px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; padding-left:2px;  padding-top:0px; padding-bottom:2px; line-height:14px; background-color:#EFEFEF;">

  </td></tr></table></td></tr></table></form>

以及我的mailer.php中的以下代码

    // check to see if verificaton code was correct
    if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("myemail@gmail.com", 'Online Form: '.$subject, "\n".$message." \n\n".$name."\n\n".$lastname."\n\n".$from."\n\n".$_SERVER['REMOTE_ADDR']."\n\n".'mailinglist: '.$mailinglist, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
    } else if(isset($message) and $message!=""){
// if verification code was incorrect then return to contact page and show error
header("Location: http://{$_SERVER['HTTP_HOST']}".dirname($_SERVER['PHP_SELF'])."contactform.php?subject=$subject&email=$email&message=".urlencode($message)."&wrong_code=true"); 
exit;
    } else {
echo "no variables received, this page cannot be accessed directly";
exit;
}
    ?>

出于某种原因,我在验证码正确时收到电子邮件,但在输入错误的验证码时,“错误代码警告”将不会显示。

是的,有人能帮帮我吗?我是php的新手,我花了很长时间才开始工作。输入错误的验证码后,所有发生的事情都是在浏览器中调用空白的mailer.php。

抱歉,不知道为什么代码会在很多不同的窗口中复制。

2 个答案:

答案 0 :(得分:1)

最好的想法是将mailer.php的内容实际放在contactform.php中,这样你就不需要任何重定向,没有满载变量的URL,也没有任何内容。

这样做的结果将是:

<?php
$state = 0;
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
    // if verification code was correct send the message and show this page
    mail("myemail@gmail.com", 'Online Form: '.$subject, "\n".$message." \n\n".$name."\n\n".$lastname."\n\n".$from."\n\n".$_SERVER['REMOTE_ADDR']."\n\n".'mailinglist: '.$mailinglist, "From: $from");
    // delete the cookie so it cannot sent again by refreshing this page
    setcookie('tntcon','');
    $state = 2;
} else if(isset($message) and $message!=""){
    // if verification code was incorrect then return to contact page and show error
    $state = 1;
}

if ($state == 0) {  ?>
    <form action="" method="post" name="form1" id="form1" onsubmit="MM_validateForm('from','','RisEmail','name','','R','verif_box','','R','message','','R');return document.MM_returnValue">

    <!-- All the form that I dont want to copy paste... -->

    </form>


<?php } else if ($state == 1) { ?>
       Message for wrong verification code.
<?php } else if ($state == 2) { ?>
       Message for email sent.
<?php } ?>

我更改了表单目标属性,因此它没有转到mailer.php并稍微更改了开放的php以使其适用于这种分布。您应该更改输入代码以使用POST数据填充它,而不是GET,您将避免使用那个讨厌的URL。

答案 1 :(得分:0)

在mailer.php中,执行以下操作:

// check to see if verificaton code was correct
if (md5($verif_box).'a4xn' == $_COOKIE['tntcon']) {
  // if verification code was correct send the message and show this page
  mail("myemail@gmail.com", 'Online Form: '.$subject, "\n".$message." \n\n".$name."\n\n".$lastname."\n\n".$from."\n\n".$_SERVER['REMOTE_ADDR']."\n\n".'mailinglist: '.$mailinglist, "From: $from");
  // delete the cookie so it cannot sent again by refreshing this page
  setcookie('tntcon','');
} else if (isset($message) && $message != "") {
  // if verification code was incorrect then return to contact page and show error
  exit("<html><head><title>Redirect</title><meta http-equiv=\"refresh\" content=\"0;contactform.php?subject=$subject&amp;email=$email&amp;message=".htmlspecialchars(urlencode($message))."&amp;wrong_code=true\" /></head><body>You should be redirected, if you aren't click <a href=\"contactform.php?subject=$subject&amp;email=$email&amp;message=".htmlspecialchars(urlencode($message))."&amp;wrong_code=true\">here</a>.</body></html>");
} else exit("no variables received, this page cannot be accessed directly");