从PHP文件显示404错误页面,无需重定向

编程入门 行业动态 更新时间:2024-10-24 00:28:15
本文介绍了从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/secret.php )在浏览器的浏览器中可见显示错误页面时显示的网址栏,而不是重定向.

in secure.php is not a solution, because I want the URL the user typed (e.g. example/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错误页面,无需重定向

本文发布于:2023-10-31 15:41:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   错误   页面   文件   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!