PHP http_response_code();与 header();

标签 php

我已经根据本教程制作了一个联系表: http://blog.teamtreehouse.com/create-ajax-contact-form

我在我的服务器上使用 PHP 版本 5.3.10-1ubuntu3.4,我在使用 http_response_code(); 时遇到了问题,这就是示例上面链接的教程使用。我读过 http_response_code(); 仅适用于 PHP 5.4。因此,我转而使用 header();

我的表单工作正常并且在我提交时显示成功消息,而不是在我使用 http_response_code(); 时显示错误,但我的 PHP 不是那么好,我想要知道我所做的是否可以接受,或者我是否应该以不同的方式来做?如果是这样,请更正我的代码。

这是我的 mailer.php 文件的内容,您可以在其中看到我注释掉了 http_response_code(); 并正在使用 header();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
    $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = trim($_POST["phone"]);
    $company = trim($_POST["company"]);
    $minbudget = trim($_POST["minbudget"]);
    $maxbudget = trim($_POST["maxbudget"]);
    $message = trim($_POST["message"]);
    $deadline = trim($_POST["deadline"]);
    $referred = trim($_POST["referred"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($phone) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        //http_response_code(400);
        header("HTTP/1.1 400 Bad Request");
        echo "Error (400). That's not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "myemail@domain.com";

    // Set the email subject.
    $subject = "Website enquiry from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Phone: $phone\n";
    $email_content .= "Company: $company\n\n";

    $email_content .= "Budget: $minbudget $maxbudget\n";
    $email_content .= "Deadline: $deadline\n";
    //$email_content .= "Max Budget: $maxbudget\n";

    $email_content .= "\n$message\n\n";        

    $email_content .= "Referred: $referred\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        //http_response_code(200);
        header("HTTP/1.1 200 OK");
        echo "Thank You! I'll be in touch soon.";
    } else {
        // Set a 500 (internal server error) response code.
        //http_response_code(500);
        header("HTTP/1.0 500 Internal Server Error");
        echo "Error (500). That's not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    //http_response_code(403);
    header("HTTP/1.1 403 Forbidden");
    echo "Error (403). That's not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form.";
}

最佳答案

我已经设法在 my own similar question 上回答了这个问题通过查看 PHP 源代码来弄清楚到底发生了什么。

这两种方法在功能上本质上是等价的。 http_response_code 基本上是一种编写 http 状态 header 的简写方式,额外的好处是 PHP 将计算出合适的 Reason Phrase。通过将您的响应代码与它在 php-src/main/http_status_codes.h 中维护的枚举中的值之一进行匹配来提供.

请注意,这意味着您的响应代码必须与 PHP 知道的响应代码相匹配。您不能使用此方法创建自己的响应代码,但可以使用 header 方法。另请注意,http_response_code 仅在 PHP 5.4.0 及更高版本中可用。

总结——http_response_codeheader设置响应码的区别:

  1. 使用 http_response_code 将使 PHP 匹配并应用来自硬编码到 PHP 源代码中的 Reason Phrases 列表中的 Reason Phrase。

  2. 由于上面的第 1 点,如果您使用 http_response_code,您必须设置一个 PHP 知道的代码。您无法设置自己的自定义代码,但如果您使用 header 函数,则可以设置自定义代码(和 Reason Phrase)。

  3. http_response_code 仅适用于 PHP 5.4.0 及更高版本

关于PHP http_response_code();与 header();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25305961/

相关文章:

php - 如何知道 POST 数据是否从 Android 应用程序中发布?

php - 将 CSS 拆分为多维数组

php - 将 javascript 变量的值插入到 php 变量中

php - 重定向太多

php - 使用 PHP 和 MySQL 按字母顺序检索阿拉伯语单词

php - 如何在 Lumen 中定义外观?

javascript - 使用 jQuery 从 PHP 发布响应?

php - 缺少呈现此页面的数据

php - 如何从两个子表中获取 SUM

PHP 和 MySQL : query if data exist and create if it not exist