How can I force a “404 Not Found” error

The short version is that you will need to do the lookup much sooner. As pointed out in comments, a 404 status can only be sent with the headers.

There are a few ways that you can solve this issue. I would do it with a class. That class would use a few hooks but most importantly, one would be early on, say “init”, for example. That would be when I would have it figure out if it will be needed and do the lookup. That result, I would store in a protected variable.

Then, when it comes time to output the data, I could have another function in the class (called a method) just deal with the output (as the lookup already happened).

This would allow you to respond 404 if the lookup failed long before any content is sent.

There are other ways using hooks and global variables but I prefer classes.

  • If you need help getting to grips with hooks ask about them here.

  • If you want help understanding classes, that’s Stack Exchange.

  • If this answer is not helpful enough, another one will be along sooner or later.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)