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 页面?


- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01