【问题标题】:PHP Includes and Require not workingPHP 包含和要求不起作用
【发布时间】:2014-03-14 20:06:54
【问题描述】:

我知道以前有人问过这个问题,但是即使在阅读了其他帖子之后,我也无法弄清楚发生了什么......

请看看我的代码结构,看看我做错了什么..

文件夹结构:

index.php

(文件夹 - 包括)header.php

(文件夹 - 浏览)blog.php、ecommerce.php、other.php

(文件夹 - 核心)init.php

我正在尝试在浏览文件夹中包含 blog.php 中的包含文件夹中的 Header.php。下面是我的 header.php 代码。

头文件位于 Includes 文件夹中

    <!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>

    <!-- Basic Page Needs
  ================================================== -->
    <meta charset="utf-8">
    <title>Web Awwards | Web Gallery | Submit your site</title>
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Mobile Specific Metas
  ================================================== -->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <!-- CSS
  ================================================== -->
    <link rel="stylesheet" type="text/css" href="../css/base.css">
    <link rel="stylesheet" type="text/css" href="../css/skeleton.css">
    <link rel="stylesheet" type="text/css" href="../css/layout.css">
    <link rel="stylesheet" type="text/css" href="../css/styles.css">
    <link rel="stylesheet" type="text/css" href="../css/menu.css">
    <link rel="stylesheet" type="text/css" href="../css/submission.css">

    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Favicons
    ================================================== -->
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">

</head>
<body>
<?php
    require_once('core/init.php');
?>
<!-- Menu Horizontal -->
<div class="horizontal">
    <div class="webName">

并浏览文件夹blog.php文件代码:

<?php
    include './includes/header.php';
?>
<!-- TODO PHP Scripts -->
<div class="category nine columns">
    <div class="category-img">
        <img src="css/img/webImg/screen1.png" alt="Site Description"/>
    </div>
    <h3> Web Name </h3>
    <h5> Designed By: Tautvydas Slegaitis </h5>
    <h7> Designers url </h7>
    <div class="vote">
        <h4> Vote for my site </h4>
        </br>
        <a href="#"> Love it </a>
        <a href="#"> Nope, Not for me </a>
    </div>
</div>

</div><!-- Close Container Div -->
<div class="footer">


</div>



</body>
</html>

但它不起作用。我不知道为什么,...请帮我解决这在过去 2 天里杀死我的问题。 Coda 2 显示错误

" 警告:include(./includes/header.php):无法打开流:第 2 行中没有此类文件或目录 警告:include():打开 './includes/header.php' 失败包含 (include_path='.:') 在 - 第 2 行“

init.php 代码:

<?php
session_start();

$GLOBALS['config'] = array(
    'mysql' => array(
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'db' => 'webAwwards'
    ),
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expire' => 604800
    ),
    'session' => array(
        'session_name' => 'user'

    )
);
// TODO Remove parenthesis in require once if doesnt work
spl_autoload_register(function($class) {
    require_once('classes/' . $class . '.php');
});

require_once('/functions/sanitise.php');

?>

【问题讨论】:

  • ./ 应该是../
  • 如果我添加 ../ 它包含标题但随后停止执行 blog.php 的其余部分,如果我在浏览器中查看源代码仅显示 header.php 代码
  • 尝试使用require 而不是include
  • 如果我使用 require 它不会改变任何东西。我真的没有想法不知道发生了什么

标签: php include-path


【解决方案1】:

试试:

include('../test/header.php');

它在我身上很好用。

Edit: 如果不是您的情况,请尝试更正您的init.php 的路径:

require_once('../core/init.php');

【讨论】:

  • 如果我按照您的建议更正了我的 init.php 的路径,然后没有打开任何文件。我知道这是有道理的,但我不知道为什么会这样
  • 您是否尝试过先将其删除,以检查您是否已经可以正确检查 get include('../test/header.php');
  • 对,所以问题出在头文件中包含 init.php :)
  • 但是我怎样才能让它包含在各种不同的目录中>?是否在 main index.php 和 browse/blog.php
  • 不使用require_once,你也可以尝试在init.php中包含,看看会发生什么?
【解决方案2】:

你可以试试这样的:

include $_SERVER['DOCUMENT_ROOT'] .'*/project_name_if_needed*/test/header/';

不知道是否推荐,请谨慎使用。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,最后发现 OVH 默认禁用其共享 Web 服务器上的选项 allow_url_include。这是无法更改的常规设置。

    解决方案是使用类似的东西

    require ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
    

    正如卢卡斯所说。

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 2013-07-15
      • 2017-09-14
      • 2016-02-17
      • 2017-06-29
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多