前端之家收集整理的这篇文章主要介绍了
PHP – 显示404错误,而不重定向到另一个页面,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果
用户到达存在的
页面但是我不希望他/她看到,我想
显示404错误.
我不想做重定向(这将导致地址栏在地址栏中显示错误页面的链接),如下所示:
if ($this_page_should_not_be_seen)
header("Location: err.PHP?e=404");
相反,似乎该页面真的不存在,而不改变浏览器地址中的URL.
@H_
403_8@
在
当前页面中包含
错误页面,并发送
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(未授权).