简体   繁体   中英

If $_SERVER['HTTP_REFERER'] was 404 Page Not Found

In PHP is there any way to catch a 404 not found?

If a user ends up on a 404, I want them to be redirected to a known page, but to display something different if it is due to a 404 error.

Something like

if($_SERVER['HTTP_REFERER'] == 404){
    echo("You've been redirected!");
}

Thanks in advance

PHP is only given the path to the page that referred you to the current page, it can't retrieve the status code for that page.

You shouldn't need to redirect the page when the page specified doesn't exist. In Apache you can specify the ErrorDocument to be a PHP file, just append a query string to the file such as ErrorDocument /index.php?error=404 so that your PHP file knows the page being requested doesn't exist. This way you can display the error straight to the user on the same page and the URL isn't lost. You don't need session or server variables, just a $_GET['error'] to check if there is an error code provided to the page.

Or, you can specify a pre-built document that is just a 404 error page, such as an ErrorDocument my404error.html , which will be displayed without any server-side processing for the page which is not found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM