PHP Best Forwarding Techniques

I am creating a PHP CMS and have some system pages, such as 404 page, service page and unauthorized access page. When page A is not found, CMS will redirect page 404; if the user does not have access to page B, he will be redirected to the unauthorized access page, etc.

I would like to use the correct status code in the title of each page, but I need to clarify how to handle the header / redirect. Should I put the 404 heading on page A and then redirect to page 404 or put the status 404 on page 404? Also, if the latter is the correct answer, what type of redirection should I use to get there 301 or 302?

+4
source share
2 answers

If the user comes to page A and this page does not exist, then do not redirect: just send the 404 error code from page A - and, to be pleasant for your user, HTML content indicating that the page does not exist.

Thus, the browser (and this is even more true for crawlers!) Will know that the page that is not found is page A, and not something else that you would try to redirect.

The same is true for other types of errors, by the way: if a specific URL matches the error, then the error code should be sent from that URL.


Basically, quite simple:

if (page not found) { header("404 Not Found"); echo "some nice message that says the page doesn't exist"; die; } 

(Well, you could deduce something nicer, of course, but you get the idea ;-))

+6
source

I'm not sure redirecting is the best way to do this. I'd rather use some of the built-in functions that are included in the project.

If the data is not found, do not redirect the user to another page, just send him an error message, for example Hey, this site does not exists! Try an other one Hey, this site does not exists! Try an other one and so on. And not at the end, you have to embed the code, part of the code from Pascal Martin's answer.

I would do it in a function and call it from bootstrap or something similar with similar behavior.

 function show_error($type="404", $header = true, $die = false) { if($header) header("404 Not Found"); echo file_get_contents($type.'.php'); if($die) die; // // and so on... } 
+1
source

Source: https://habr.com/ru/post/1313434/


All Articles