从PHP文件显示404错误页面,无需重定向 [英] Show 404 error page from PHP file, without redirecting
本文介绍了从PHP文件显示404错误页面,无需重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 secret.php
文件,该文件包含在 index.php
中,当有人尝试访问 secret时,我想显示一个404错误页面.php
直接.但是
I have a file secret.php
which is included in index.php
and I want to show a 404 error page when someone tries to access secret.php
directly. But
header("Location: this_site_certainly_does_not_exist");
secure.php
中的
不是解决方案,因为我希望用户键入的URL(例如 example.com/secret.php
)在浏览器的浏览器中可见显示错误页面时显示的网址栏,而不是重定向.
in secure.php
is not a solution, because I want the URL the user typed (e.g. example.com/secret.php
) to be visible in the browser's URL bar when the error page is shown, instead of redirecting.
推荐答案
您可以使用标头来实现.
You can do it with headers.
header("HTTP/1.0 404 Not Found");
然后,您可以使用例如 readfile
方法添加404页面.
then you can add your 404 page using for example readfile
method.
header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
这篇关于从PHP文件显示404错误页面,无需重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文