PHP http_response_code();与header();

时间:2014-08-14 10:34:58

标签: 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.";
}

3 个答案:

答案 0 :(得分:16)

我已经设法通过PHP源代码在my own similar question上回答这个问题,以确切了解发生了什么。

这两种方法在功能上基本相同。 http_response_code基本上是编写http状态标头的简写方式,还有一个额外的好处,即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源代码中的原因短语。

  2. 由于上面的第1点,如果您使用http_response_code,则必须设置PHP知道的代码。您无法设置自己的自定义代码,但如果您使用header功能,则可以设置自定义代码(和原因词组)。

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

答案 1 :(得分:6)

简易解决方案:

/**
 * Sets the response code and reason
 *
 * @param int    $code
 * @param string $reason
 */
function setResponseCode($code, $reason = null) {
    $code = intval($code);

    if (version_compare(phpversion(), '5.4', '>') && is_null($reason))
        http_response_code($code);
    else
        header(trim("HTTP/1.0 $code $reason"));

}

您可以将其用作:

setResponseCode(404);

setResponseCode(401,'Get back to the shadow');

答案 2 :(得分:1)

要回答您的主要问题,我可以看到使用标头与http_response_code()的最大响应是,http_response_code()仅支持PHP 5.4及更高版本,旧版本将无法使用该功能。

在示例中使用标题可以确保您的代码可以在旧版本上运行。