PHP – 显示404错误,而不重定向到另一个页面

前端之家收集整理的这篇文章主要介绍了PHP – 显示404错误,而不重定向到另一个页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果用户到达存在的页面但是我不希望他/她看到,我想显示404错误.

我不想做重定向(这将导致地址栏在地址栏中显示错误页面链接),如下所示:

if ($this_page_should_not_be_seen)
   header("Location: err.PHP?e=404");

相反,似乎该页面真的不存在,而不改变浏览器地址中的URL.

当前页面中包含错误页面,并发送404错误状态代码
<?PHP
if ($nobody_should_ever_be_here) {
  header('HTTP/1.1 404 Not Found'); //This may be put inside err.PHP instead
  $_GET['e'] = 404; //Set the variable for the error code (you cannot have a
                    // querystring in an include directive).
  include 'err.PHP';
  exit; //Do not do any more work in this script.
}
?>

请注意,如果页面不应该被看到,应该使用这个.未授权访问的更好的状态代码(如果页面应该被某些登录用户看到)为403(未授权).

原文链接:https://www.f2er.com/php/130971.html

猜你在找的PHP相关文章