IT

PHP 앱이 404 오류를 보내지 않는 이유는 무엇입니까?

lottoking 2020. 6. 19. 07:56
반응형

PHP 앱이 404 오류를 보내지 않는 이유는 무엇입니까?


if (strstr($_SERVER['REQUEST_URI'],'index.php')) {
    header('HTTP/1.0 404 Not Found');
}

왜 이것이 효과가 없을까요? 빈 페이지가 나타납니다.


코드가 기술적으로 정확합니다. 빈 페이지의 헤더를 보면 404 헤더가 표시되며 다른 컴퓨터 / 프로그램은 응답을 파일을 찾을 수 없음으로 올바르게 식별 할 수 있습니다.

물론 사용자는 여전히 SOL입니다. 일반적으로 404는 웹 서버에서 처리합니다.

  • User :이 URI 웹 서버에 저에게 필요한 것이 있습니까?
  • 웹 서버 : 아니요, 404입니다! 다음은 404에 표시 할 페이지입니다.

문제는 일단 웹 서버가 PHP 페이지 처리를 시작하면 404를 처리 할 지점을 이미 통과 한 것입니다.

  • User :이 URI 웹 서버에 저에게 필요한 것이 있습니까?
  • 웹 서버 : 예. PHP 페이지입니다. 응답 코드가 무엇인지 알려줍니다.
  • PHP : 야, OMG 404 !!!!!!!
  • 웹 서버 : 404 페이지의 사람들이 이미 집에 돌아 왔기 때문에 PHP가 나에게 준 것을 보내겠습니다.

PHP는 404 헤더를 제공 할뿐만 아니라 실제 404 페이지를 출력합니다.


if (strstr($_SERVER['REQUEST_URI'],'index.php')){
    header('HTTP/1.0 404 Not Found');
    echo "<h1>404 Not Found</h1>";
    echo "The page that you have requested could not be found.";
    exit();
}

마지막 두 개의 반향 선을 보면 내용이 표시됩니다. 원하는대로 사용자 정의 할 수 있습니다.


이것이 올바른 행동 입니다. 404 페이지의 컨텐츠를 작성하는 것은 사용자의 책임입니다.
404 헤더는 스파이더 및 다운로드 관리자가 파일이 존재하는지 확인하는 데 사용됩니다.
(404 헤더가있는 페이지는 Google 또는 다른 검색 엔진에 의해 색인이 생성되지 않습니다)

그러나 일반 사용자는 http- 헤더를 보지 않고 해당 페이지를 일반 페이지로 사용합니다.


레코드의 경우 이것은 모든 경우 처리기입니다.

<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
header("Status: 404 Not Found");

$_SERVER['REDIRECT_STATUS'] = 404;
?> <!-- 404 contents below this line -->

기본 서버 404 페이지를로드하십시오 (예 : 아파치에 대해 정의 된 경우).

if(strstr($_SERVER['REQUEST_URI'],'index.php')){
  header('HTTP/1.0 404 Not Found');
  readfile('404missing.html');
  exit();
}

PHP 5.4부터는 이제 할 수 있습니다 http_response_code(404);


@Kitet를 기반으로 한 또 다른 솔루션.

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
header("Status: 404 Not Found");

$_SERVER['REDIRECT_STATUS'] = 404;
//If you don't know which web page is in use, use any page that doesn't exists
$handle = curl_init('http://'. $_SERVER["HTTP_HOST"] .'/404missing.html');
curl_exec($handle);

제어 할 수없는 서버에서 호스팅되는 웹 사이트를 프로그래밍하는 경우 "404missing.html"파일을 알 수 없습니다. 그러나 여전히이 작업을 수행 할 수 있습니다.

이러한 방식으로 동일한 서버에서 일반 404 페이지와 동일한 결과를 제공했습니다. 관찰자는 기존 PHP 페이지 리턴 (404)과 존재하지 않는 페이지를 구별 할 수 없습니다.


시도해보십시오 :

header("Status: 404 Not Found");
header('HTTP/1.0 404 Not Found');

안녕!


조금 더 짧은 버전. 홀수 에코를 억제합니다.

if (strstr($_SERVER['REQUEST_URI'],'index.php')){
  header('HTTP/1.0 404 Not Found');
  exit("<h1>404 Not Found</h1>\nThe page that you have requested could not be found.");
}

if($_SERVER['PHP_SELF'] == '/index.php'){ 
   header('HTTP/1.0 404 Not Found');
   echo "<h1>404 Not Found</h1>";
   echo "The page that you have requested could not be found.";
   die;
}

echo 문을 단순화하지 말고 위와 같이 세미콜론을 잊지 마십시오. 또한 페이지에서 substr을 실행하는 이유는 php_self를 쉽게 실행할 수 있습니다.


서버의 기본 오류 페이지를 표시하려면 서버에서이 오류를 처리해야합니다.


정제를 사용할 수는 있지만 올바르게하고 있습니다. 이 문제가 해결 된 것처럼 보이므로 실용적인 응용 프로그램 이점에 대해 이야기 해 보겠습니다.

An old website of ours that has a large collection of multilingual tech docs was executing this inside an if else conditional:

    if (<no file found>){
        die("NO FILE HERE");
    }

The problem (besides the unhelpful message and bad user experience) being that we generally use a link crawler (in our case integrity) to check out bad links and missing documents. This means that we were getting a perfectly correct 200 no error response telling us that there was a file there. Integrity didn't know that we were expecting a PDF so we had to manually add a 404 header with php. By adding your code above the die (because nothing afterwards would execute and header should always be before any rendered html anyway), integrity (which behaves more or less like a browser) would return a 404 and we would know exactly where to look for missing files. There are more elegant ways of telling the user that there is an error, but by serving a 404 error you are not only notifying browsers and browser-like programs of the error but (I believe-correct me if I'm wrong) are also recording those errors in your server logs where you can easily grep for 404s.

    header('HTTP/1.0 404 Not Found');
    die("NO FILE HERE");

Try this:

if (strstr($_SERVER['REQUEST_URI'],'index.php')) {
    header('HTTP/1.0 404 Not Found');
    echo "<head><title>404 Not Found</title></head>";
    echo "<h1>Not Found</h1>";
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    echo "<p>The requested URL ".$uri." was not found on this server.</p>";
    exit();
}

You know, in my website i created something like this:

        $uri=$_SERVER['REQUEST_URI'];
        $strpos=strpos($uri, '.php');
        if ($strpos!==false){
        $e404="The page requested by you: &quot".$_SERVER['REQUEST_URI']."&quot, doesn't exists on this server.";
        $maybe=str_replace('.php','',$uri);
        $maybe=str_replace('/','',$maybe);
        die("<center><h1>404</h1><hr><h3>$e404</h3><h3>Maybe try <a href=$maybe>www.leaveyortexthere.p.ht/$maybe</a>?</center>");

}

i hope it helps you.


I came up to this problem.. I think that redirecting to a non existing link on your server might do the trick ! Because the server would return his 404:
header('Redirect abbb.404.nonexist'); < that doesnt exist for sure


If you want to show the server’s default 404 page, you can load it in a frame like this:

echo '<iframe src="/something-bogus" width="100%" height="100%" frameBorder="0" border="0" scrolling="no"></iframe>';

참고URL : https://stackoverflow.com/questions/437256/why-wont-my-php-app-send-a-404-error

반응형