【问题标题】:require footer and header each page but error 500每页都需要页脚和页眉,但错误 500
【发布时间】:2021-12-18 17:03:30
【问题描述】:

我从 PHP 开始,然后创建我的网站。我创建了一个页眉和页脚组件以在每个页面上打印:require '/assets/components/header.php';require '/assets/components/footer.php';

所以,问题是在本地主机上,它工作,但在网站上,它没有工作,我有一个 http 错误 500

剧目: Folder tree

我试试:

1.不同类型的链接

require 'assets/components/header.php';

require './assets/components/header.php';

require '../assets/components/header.php';


2. 创建一个变量 $BASE_URL 来存储网站的 url 并要求如下:require "$BASE_URL/assets/components/header.php";。我将设置“allow_url_include”设置为 true,但出现此错误:

[04-Nov-2021 12:28:40 UTC] PHP Warning:  require(http://#####/assets/components/header.php): Failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

'#####' -> 我的网站

我读到一个主题,将 'allow_url_include' 设置为 true 并不安全,所以如果我们可以做不同的话。

Header.php

<?php
require_once "$BASE_URL"."assets/function.php";
if(!isset($title)){
    $title='Error 404 - Catif';
}
if(!isset($page)){
    $page='error';
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href='/assets/css/style.css'>
    <title><?= $title ?></title>
</head>
<body>
    <nav>
        <div class="nav-left"><p class="nav-name">Catif</p></div>
        <div class="nav-right">
            <a class="nav-item <?php if($page === 'home'): ?>active<?php endif ?>" href="/index.php">Projets</a>
            <a class="nav-item ml-80 <?php if($page === 'me'): ?>active<?php endif ?>" href="views/me.php">Moi</a>
            <a class="nav-item ml-80 <?php if($page === 'contact'): ?>active<?php endif ?>" href="/views/contact.php">Contact</a>
        </div>
        <button class="nav-button">==</button>
    </nav>

    <div class="container">

【问题讨论】:

  • 分享header.php文件内容
  • require 适用于路径,而不是 url
  • 不要将 BASE_URL 与必需/包含的文件一起使用。请改用__DIR__$_SERVER['DOCUMENT_ROOT'] 之类的东西。
  • 将 URL 传递给 require 是没有意义的。传递您的文件在服务器磁盘上的路径。
  • A 500 错误是一种通用错误消息,几乎涵盖了 PHP 脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。

标签: php require


【解决方案1】:

在 PHP 中包含组件文件时,您应该使用文件路径,而不是 URL。比如我们可以使用相对文件路径来包含pageOther1.php中的header,应该是这样的。

require "../../../assets/components/header.php";
// OR
require __DIR__ . "/../../assets/components/header.php";

为了提高效率,特别是如果您要包含多个组件并具有不同级别的嵌套视图,您可以在 PHP 文件中定义页眉和页脚路径(并包括任何其他全局脚本,如 functions.php) (例如initialize.phpconfig.php)在您应用的根目录。这样你只需要要求 initialize.php 并使用 PHP 常量包含你想要的组件。

这是一种更有效的方法

将组件路径定义为根目录下 PHP 文件中的常量(例如initialize.php

define("APP_PATH", dirname(__FILE__)); // The main project directory
define("HEADER", "APP_PATH" . "/assets/components/header.php"); // Path to header.php
define("FOOTER", "APP_PATH" . "/assets/components/footer.php"); // Path to footer.php

// You could also include your global scripts here and don't have to include it on each view page
require "APP_PATH" . "/path_to_global_script.php";

然后在视图中包含组件和脚本。 pageOther1.php 的示例是:

require "../../../initialize.php"; // Path to the file which defines the constants
require HEADER;
// Page content
require FOOTER;

您可以轻松地将其扩展为多个组件和功能脚本。

【讨论】:

    猜你喜欢
    • 2014-10-13
    • 2013-12-01
    • 1970-01-01
    • 2017-09-25
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多