如何在PHP中创建错误404? [英] How can I create an error 404 in PHP?

查看:78
本文介绍了如何在PHP中创建错误404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.htaccess将所有请求重定向到/word_here/page.php?name=word_here.然后,PHP脚本检查所请求的页面是否在其页面数组中.

My .htaccess redirects all requests to /word_here to /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages.

如果没有,如何模拟错误404? 我尝试了此操作,但未显示通过.htaccess中的ErrorDocument配置的404页面.

If not, how can I simulate an error 404? I tried this, but it didn't result in my 404 page configured via ErrorDocument in the .htaccess showing up.

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

我是否正确地认为重定向到我的错误404页面是错误的?

Am I right in thinking that it's wrong to redirect to my error 404 page?

推荐答案

生成404页的最新答案(从PHP 5.4或更高版本开始)是使用

The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use http_response_code:

<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();

die()并非绝对必要,但可以确保您不会继续正常执行.

die() is not strictly necessary, but it makes sure that you don't continue the normal execution.

这篇关于如何在PHP中创建错误404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆