无法让代码在 PHP 5.5.9 中工作

Can't get code to work in PHP 5.5.9

此代码在 PHP 5.3 中运行良好,但我不知道如何才能使其在 LAMP 服务器上的 PHP 5.5.9 中运行。我尝试四处搜索,但没有找到任何解决方案。有什么解决办法吗?

所以;用户正确地看到了发件人,而事实上它总是说邮件已发送,甚至在他们点击提交按钮之前也是如此。第二,它最后应该发的邮件,发不出去,什么都没有。

<body> 

<?php
//Variabals and validastions
$firstnameErr = $secondnameErr = $surenameErr = $emailErr = $dateErr = $gateErr = $pnumErr = "";
$firstname = $secondname = $surename = $email = $date = $gate = $pnum = "";
$Err = "1";
$date = date("d/m/Y");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["firstname"])) {
     $firstnameErr = "Firstname is needed";
     $Err = "2";
   } else {
     $firstname = test_input($_POST["firstname"]);
     if (!preg_match("/^[a-åA-Å ]*$/",$firstname)) {
       $firstnameErr = "Only letter and spaces";
       $Err = "2";
     }
   }
   if (empty($_POST["secondname"])) {
     $secondname = " ";
    } else {
     $secondname = test_input($_POST["secondname"]);
     if (!preg_match("/^[a-åA-Å ]*$/",$secondname)) {
       $secondnameErr = "Only letter and spaces";
       $Err = "2";     
     }
   }

   if (empty($_POST["surename"])) {
     $surenameErr = "Surename is needed";
     $Err = "2";
   } else {
     $surename = test_input($_POST["surename"]);
     if (!preg_match("/^[a-åA-Å ]*$/",$surename)) {
       $surenameErr = "Only letter and spaces";
       $Err = "2";
     }
   }

   if (empty($_POST["email"])) {
     $emailErr = "E-postadresse is needed";
     $Err = "2";
   } else {
     $email = test_input($_POST["email"]);
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Not valid email"; 
       $Err = "2";
     }
   }
    if (empty ($_POST["date"])) {
     $dateErr = "Fødselsdate is needed";
     $Err = "2";
    } else {
     $date = test_input ($_POST["date"]);
    }

    if (empty($_POST["gate"])) {
     $gateErr = "Gateadresse is needed";
     $Err = "2";
   } else {
     $gate = test_input($_POST["gate"]);
     if (!preg_match("/^[a-åA-Å0-9. ]*$/",$gate)) {
       $gateErr = "Invalid characters, only letters, numbers and space"; 
       $Err = "2";
     }
   }

   if (empty($_POST["pnum"])) {
     $pnumErr = "Post number is needed";
     $Err = "2";
   } else {
     $pnum = test_input($_POST["pnum"]);
     if (!preg_match("/^\d{4}$/",$pnum)) {
       $pnumErr = "Invalid post number"; 
       $Err = "2";
     }
   }
   if ($Err == "1") {
     $Err = "";
   }
}
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>

<!--Form-->
<div>
<div>
<img src="https://wiki.piratpartiet.no/images/1/18/Ole.png" alt="uPir logo" width="400px">
<h1 style="text-align:center">Application form</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
<table style="width:100%">
    <tr>
    <td>
    Firstname: <br>
    <input name="firstname" type="text" value="<?php echo $firstname;?>">
    <span class="error">* <br>
    <?php echo $firstnameErr;?></span>
    <br><br>
    </td>
    <td>
    Secondname:<br>
    <input name="secondname" type="text" value="<?php echo $secondname;?>">
    <span class="error"><br>
    <?php echo $secondnameErr;?></span>
    <br><br>
    </td>
    </tr>
    <tr>
    <td>
    Surename:<br>
    <input name="surename" type="text" value="<?php echo $surename;?>">
    <span class="error">* <br>
    <?php echo $surenameErr;?></span>
    <br><br>
    </td>
    </tr>
    <tr>
    <td>
    E-mail:<br> 
    <input name="email" type="mail" value="<?php echo $email;?>">
    <span class="error">* <br>
    <?php echo $emailErr;?></span>
    <br><br>
    </td>
    <td>
    Brithdate (dd.mm.yyyy):<br>
    <input name="date" type="date" min="1900-01-01" max="2015-12-31" value="<?php echo $date;?>">
    <span class="error">* <br>
    <?php echo $dateErr;?></span>
    <br><br>
    </td>
    </tr>
    <tr>
    <td>
    Adress:<br>
    <input name="gate" type="text" value="<?php echo $gate;?>">
    <span class="error">* <br>
    <?php echo $gateErr;?></span>
    <br><br>
    </td>
    <td>
    Post number:<br>
    <input name="pnum" type="number" value="<?php echo $pnum;?>">
    <span class="error">* <br>
    <?php echo $pnumErr;?></span>
    <br><br>
    </td>
    </tr>
</table>
    <?php
//Controll and mailsender
if (empty($Err["Err"])) {
    $to = 'email@adress.com';
    $subject = 'subject';
    $message = "$firstname $secondname $surename ønsker å bli medlem.
E-post: $email
Fødselsdate: $date
Gateadresse: $gate
Postnummer: $pnum

Sendt: $date
--massages info here--";
    mail($to, $subject, $message);
    echo '<p class="sucsess">Messages sent</p>';
}
?>
    <p style="text-align:center"><input type="submit" name="sumbit" value="Register" id="submit"></p>
</div>
</form>
</div>

</body>

问题是在 5.5.9 上它不发送信息邮件(给我)并且用户在他们点击 sumbit 按钮之前得到邮件发送的信息,即使它是错误的归档。

  • action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" -> action=""
  • if (empty($Err["Err"])) { -> $Err 可能仍为“1”(无数组),这将引发警告
  • 您可能在此处遇到 UTF-8 问题(我在 html 中没有看到 html 元标记/或 xml 通知或字符编码标记)

但请先告诉我们哪里出了问题! 在代码开头开启显示errors/warnings:

ini_set('error_reporting', E_ALL); ini_set("display_errors", "1");

我的第一个猜测是,虽然您的 PHP 5.3 服务器配置为发送电子邮件,但您的 PHP 5.5.9 不是。简单测试 - 使用硬编码值调用 mail()

even before they hit the submit button

我不确定你说的那一点是什么意思,但也许你在用 AJAX 做某事。查看浏览器的控制台日志以查看所有 HTTP 请求和响应。

在上次测试中 if (empty($Err["Err"])) { 应该是 if (empty($Err)) { 的问题。

问题已解决。