Why Won't My PHP App Send a 404 Error

Why doesn't apache show a 404 error when I send a 404 header with php?

The header is not what tells Apache to display it's 404 page. Rather, when Apache displays its 404 page, it sends a 404 header along with it. The header is meant to have meaning to the browser, not the server. Apache displays a 404 when it can't find the proper file to display. Since you're in a PHP script, Apache has already found a file it can display, and thus won't show its own 404 page.

PHP 404 error page won't display content

If PHP is configured at your host to run through CGI, it may be impossible to generate 404 error pages in IIS (except by lying and returning 200 OK, which is a Bad Thing). Certainly I've been unable to persuade IIS 4-6.0 to allow my CGI 404 errors through to browsers.

You generally don't want PHP to run CGI anyway, there are other related problems as well as it being slow. On IIS, the PHP ISAPI extension should be preferred (though as I've not tried it I can't confirm it solves this specific problem).

Php 404 Not Found Header

If i remove the first line and i got a BOM at the document i get blank
page (No 404).
you get blank 404 because you have no content defined in there...

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

is only giving notice that user is on 404 error page site...
if you want to display some 404 notice for user you can do this by loading your 404.html file

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

or directly

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

exit function is there because you have to prevent execution of another php code, which may be directly after if or which may be excecuted later, simply it says END

Cannot POST in AngularJS and PHP (error 404)

You are addressing register.php in $http as if the register.php script is in the same directory as your base html file. However, your file list seems to indicate that your index.html file is in the root directory, while the the register.php file is in the "scripts" subdirectory.

Assuming you don't have any special server-side routing logic, you should update the address in the $http call from:

$http.post('register.php', data)

to

$http.post('scripts/register.php', data)





EDIT: Added info based on comments from OP

Above answer is still correct -- original code was improperly referencing the register.php file residing in "scripts" directory. Once fixed, another issue exists.

OP is using "grunt serve"; however, the grunt server doesn't process php files out of the box. It's a simple server meant to serve static files (html, js, css, etc.) without server side processing/execution (php). Check out this question/answers for discussion on php under grunt:

  • Grunt serve + PHP?

However, consider installing/running a more full featured web server (Apache, IIS, etc.), especially if you're wanting to deploy this at some point.

header('HTTP/1.0 404 Not Found'); not doing anything

No, it probably is actually working. It's just not readily visible. Instead of just using the header call, try doing that, then including 404.php, and then calling die.

You can test the fact that the HTTP/1.0 404 Not Found works by creating a PHP file named, say, test.php with this content:

<?php

header("HTTP/1.0 404 Not Found");
echo "PHP continues.\n";
die();
echo "Not after a die, however.\n";

Then viewing the result with curl -D /dev/stdout reveals:

HTTP/1.0 404 Not Found
Date: Mon, 04 Apr 2011 03:39:06 GMT
Server: Apache
X-Powered-By: PHP/5.3.2
Content-Length: 14
Connection: close
Content-Type: text/html

PHP continues.

Why does my Angular POST request for PHP file return 404?

add this in the top of your php file

<?php
header("Access-Control-Allow-Origin: *");


Related Topics



Leave a reply



Submit