Dil*_*ral 13 php custom-error-pages
如果用户到达存在的页面但我不希望他/她看到,我想显示404错误.
我不想做重定向(这会导致地址栏在地址栏中显示错误页面的链接),如下所示:
if ($this_page_should_not_be_seen)
header("Location: err.php?e=404");
Run Code Online (Sandbox Code Playgroud)
相反,如果浏览器的地址中的URL没有更改,看起来该页面确实不存在.
Emi*_*röm 27
在当前页面中包含错误页面并发送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.
}
?>
Run Code Online (Sandbox Code Playgroud)
请注意,如果永远不会看到该页面,则应使用此选项.未授权访问的更好状态代码(如果某些登录用户应该看到该页面)是403(未授权).
您可以通过以下方式以编程方式实现404(或其他任何HTTP响应代码)
header('HTTP/1.0 404 Not Found');
die;
Run Code Online (Sandbox Code Playgroud)