How to create an error 404 page using PHP?(如何使用 PHP 创建错误 404 页面?)
问题描述
我的文件 .htaccess
处理从 /word_here
到我的内部端点 /page.php?name=word_here
的所有请求.PHP 脚本然后检查请求的页面是否在其页面数组中.
My file .htaccess
handles all requests from /word_here
to my internal endpoint /page.php?name=word_here
. The PHP script then checks if the requested page is in its array of pages.
如果没有,我如何模拟 404 错误?
If not, how can I simulate an error 404?
我试过这个,但它没有导致我通过 ErrorDocument
配置的 404 页面出现在 .htaccess
中.
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 或更高版本开始)是使用 http_response_code
:
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 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 PHP 创建错误 404 页面?


基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01