脚本不返回404

时间:2012-01-04 17:26:45

标签: php http-status-code-404

我有一个PHP脚本可以进行一些验证,如果验证关闭,它应该返回404.但它没有。

这是脚本的开头:

<?php
include '../connect.php';
include '../global.php';
include '../utils/api/problems.php';
include '../utils/api/suggested_solutions.php';
include '../utils/api/attempted_solutions.php';
include '../utils/api/categories.php';

$problem_id = mysql_real_escape_string($_GET["problem_id"]);

// Get member_id from session
$member_id = $_SESSION['user_id'];

// Validate the call
if ( empty ( $problem_id ) || !isset ( $problem_id ) || !is_numeric ( $problem_id ) )
{
    $referer = $_SERVER['HTTP_REFERER']; 

    // Send me an email with the error:
    $from = "from: problem_url_error@problemio.com";  
    $to_email_address = 'my_email';
    $error_subject = 'Error happened when getting problem_id from request';
    $contents = 'Error in problem.php - here is the referer: '.$referer;

    //mail($to_email_address, $error_subject, $contents, $from);    

    error_log ( ".......error validating problem id in problem.php");
    header('HTTP/1.1 404 Not Found');
}

但由于某种原因,这不会返回404 - 任何想法为什么?

谢谢!

2 个答案:

答案 0 :(得分:4)

标题名为status:

header("Status: 404 Not Found");

修改 现在我看到你的方法应该也可以工作,如果你满足使用header("HTTP/xxx ...")的要求,研究标题documentation,有一些限制。

答案 1 :(得分:3)

header("HTTP/1.0 404 Not Found");

根据文档应该足够 - 使用FastCGI时使用Status标头 - Docs

您将获得一个空白页面,您可以添加如下内容:

header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";