1

坦率地说,我不是一个编码狂。基本上,这个 php 脚本验证“时事通讯订阅”表单的名称和电子邮件字段。好吧,一切似乎在所有浏览器上都运行良好。2 天后,此脚本停止在 IE9 和 Firefox 14-16 上运行。它在 Chrome 上运行良好。请问各位大神,请问这个错误是什么原因造成的?

php 脚本名为 signup.php

代码中的“回声脚本警报”似乎不适用于 FF 和 IE9。它在 Chrome 上运行得非常好。这正是我的问题。

Firefox 上的错误 - Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。此问题有时可能是由禁用或拒绝接受 cookie 引起的。

Firefox 上的操作点 - 已采取所有必要措施来解决此错误。什么都没解决。

IE9 上的错误 - 它根本不响应并停留在同一页面上。

<?php

ob_start();
session_start();

/*Name Validation.*/
function checkName($name) 
{
$nAccept = array("&", "’", " ",  "-");

if (!ctype_alpha(str_replace ($nAccept, "", $name)))
   {
        return TRUE;
    }
}

/*Email Validation.*/
function checkEmail($email) 
{
   if(mb_eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }
    if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/i", $email))
    {

        return false;
    }

   list($Username, $Domain) = explode("@",$email);

   if(getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}


function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}

if(checkName($_REQUEST['name']) == TRUE) {
echo "<script>alert('Please enter a Valid Name.');history.back();</script>";
}

elseif (!isset($_POST['email'])) {
echo "<script>alert('Please ensure you have completed all fields before submitting the form. No fields to be left blank.');history.back();</script>";
}

elseif (empty($_POST['name']) || empty($_POST['email'])) {
echo "<script>alert('Please ensure you have completed all fields before submitting the form. No fields to be left blank.Type in only one valid email address.');history.back();</script>";
}

elseif ( isInjected($_POST['email']) ) {
echo "<script>alert('Please ensure you have completed all fields before submitting the form. No fields to be left blank.Type in only one valid email address.');history.back();</script>";
}

elseif(checkEmail($_REQUEST['email']) == FALSE) {
echo "<script>alert('Entered E-Mail is Invalid or The E-Mail does not belong to a valid domain.');history.back();</script>";
}

else{

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="connect_members_temp"; //table name

mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); 
mysql_select_db("$db_name")or die("cannot select DB");

$confirm_code=md5(uniqid(rand()));

$name=$_POST['name'];
$email=$_POST['email'];

$sql="INSERT INTO $tbl_name(confirm_code, name, email)VALUES('$confirm_code', '$_POST[name]', '$_POST[email]')";
$result=mysql_query($sql);

}
mysql_close();

if($result)
{

// send e-mail to
$to= 'me@localhost';
$thankyou_page = "before_subscription.html";
$email_page = "email_connectivity.html";
$data_page = "email_not_found.html";

$subject="Confirm Your Subscription with XXXXX";


$header="from: XXXXX <donotreply@XXXXX.com>";

$message="Your Confirmation link \r\n";
$message.="Click on this link to confirm your subscription \r\n";
$message.="http://localhost/XXXXX/confirmation.php?passkey=$confirm_code";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

else
{
header( "Location: $data_page" );
}

if($sentmail)
{
header( "Location: $thankyou_page" );
}
else{
header( "Location: $email_page" );
}

?>

这是 HTML 表单

<form action="signup.php" method="post">
        <fieldset>
          <legend>Digital Newsletter</legend>
          <div class="fl_left">
            <input type="text" name="name" value="Enter name here&hellip;" onfocus="this.value=(this.value=='Enter name here&hellip;')? '' : this.value ;" />
            <input type="text" name="email" value="Enter email address&hellip;" onfocus="this.value=(this.value=='Enter email address&hellip;')? '' : this.value ;"/>
          </div>
          <div class="fl_right">
            <input type="submit" name="newsletter_go" id="newsletter_go" value="&raquo;"/>
          </div>
        </fieldset>
      </form>
4

1 回答 1

2

您不能进行重定向,也不能在页面上回显消息。当验证失败时,您应该回显该消息然后退出。

您收到重定向错误的原因是由于提到的 GBD 问题。当验证失败时,你永远不会进入else设置的子句$data_page,但header("Location: $data_page");无论如何你都会这样做。

此外,在您这样做之后,您将进入发送$thankyou_page或的代码$email_page。你只能有一个Location:标题。您需要在发送所需的重定向后退出。

于 2012-10-13T05:58:50.023 回答