0

Let's say I have a test.php file, configured to handle Apache 404 errors with ErrorDocument 404 /test.php

What PHP code do I have to add to check if we're really handling a 404, and test.php has not been called directly?

Like:

<?php
if (server_response_code == 404) {
    echo "It's a 404!"
}
?>

P.S. There's no 404 forwarding or so…

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Can you clarify - Do you want to determine the response code of an HTTP request you've made to another server from PHP? It sounds like you want the response code for the request you are currently handling, and that would normally be 200 OK, unless you override it using header() – Paul Dixon Jan 28 '17 at 08:51
  • See http://stackoverflow.com/questions/408405/easy-way-to-test-a-url-for-404-in-php for checking remote URLs for 404 responses – Paul Dixon Jan 28 '17 at 08:53
  • @Paul Dixon I mean: How can I check if a current url is 404 or not? –  Jan 28 '17 at 08:56
  • I do not want to check remote URL's. I want to check the URL where the code is on. –  Jan 28 '17 at 08:59
  • The fact that you are processing it in a script means it's not a 404 by default. You can return a 404 response with header() - see M Maavia's answer below for an example. – Paul Dixon Jan 28 '17 at 09:03
  • Okay, I'll try to explain: I have this in my `.htaccess`: `ErrorDocument 404 /`. `www.example.com/existing_file.php` will open `existing_file.php` and response server code 200. But `www.example.com/NOT_existing_file.php` will redirect to main path (`/`) und send a 404 server response code. How can I now echo "It's 404!" e.g.? –  Jan 28 '17 at 09:08
  • Wouldn't it be easier to set your ErrorDocument to a special script, e.g. 404.php? Then you know you're handling a 404! Otherwise, in your main script you'll have to look the $_SERVER['REQUEST_URI'] and similar vars to see if you can detect if the request would have been a 404. – Paul Dixon Jan 28 '17 at 09:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134235/discussion-between-user7128548-and-paul-dixon). –  Jan 28 '17 at 09:20

1 Answers1

0

One way to do this would be simply look at the $_SERVER['REQUEST_URI'] and determine if it points at something invalid.

While your example is probably simplified for this question, it might be as simple as

if ($_SERVER['REQUEST_URI'] != 'test.php') {
    //we are handling a 404
    header("HTTP/1.0 404 Not Found");
    echo "These aren't the droids you're looking for";
}

You may need more complex logic, but will all boil down to looking at the request and detecting it was invalid.

As long as the ErrorDocument is a relative URL (doesn't start with http://...), then you should find your script has some extra $_SERVER variables, e.g.

$_SERVER["REDIRECT_URL"]; // /original/path
$_SERVER['REDIRECT_STATUS']; // 404

See the ErrorDocument documentation for more details.

Alternatively, just pass a query string arg to the 404 handler, e.g. ErrorDocument 404 /test.php?error=404 :)

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Thanks, solution was to add `ErrorDocument 404 /?404=yes` in my `.htaccess` and the following in my file: ``. –  Jan 28 '17 at 10:22