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 页面?
基础教程推荐
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- php中的PDF导出 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
