【问题标题】:PHP include a file with an included file -> scripts and variables get included but seem echo'dPHP包含一个包含文件的文件->脚本和变量被包含但似乎回显
【发布时间】:2016-04-12 23:00:29
【问题描述】:

我在包含一个包含包含一些 js 脚本库的文件的文件时遇到问题。

index.php

<?php
include 'header.php';
?>
<section>
  <div>
     <p>misc. html</p>
  </div>
</section>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>

页脚.php

<?php
include ($_SERVER['DOCUMENT_ROOT'].'/includes/footerscripts.php');
?>
</body>
</html>

/includes/footerscripts.php

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php
$testVar = 'test';
?>

在那种情况下,索引文件在运行时在源代码中确实有这个:

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
test =

脚本正在运行,但没有被执行,变量也没有通过。

当我将 footer.php 更改为直接包含这些脚本和变量时,该变量会将其变为 index.php,脚本也是如此,但它们没有执行(或没有按时执行?)。

各位有什么想法吗?

我有一个奇怪的 php5.ini 文件,因为我不知道要在里面放什么,如果有什么不同的话。包含文件路径似乎没问题,因为它在大多数情况下仍然有效。

谢谢, 马特

编辑:添加了包含('header.php'),它工作正常并拉入我的标题。

那个文件看起来像:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="--">
<meta name="keywords" content="--">
<meta name="author" content="--">
<title>--</title>
<link rel="stylesheet" href="/css/mainstylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="icon" href="/images/favicon.ico">
</head>
<body>
    <header class="nav-top" id="top" role="navigation">
        <div class="container">
            <div class="home-link">
                <a href="../"><img src="/images/logo/logo-p.png" alt="plc" class="logo" /></a>
            </div>
            <nav class="nav-main">
                <ul>
                    <li>
                        <a href="../">Home</a>
                    </li>
                    <li>
                        <a href="/portfolio">Portfolio</a>
                    </li>
                    <li>
                        <a href="/contact">Contact</a>
                    </li>
                </ul>
            </nav>
        </div>
    </header>

编辑:我想我只需要从文档的标题加载我的 jquery,因为它是这样工作的。我仍然不能将变量从孙子传递给父母,但是哦,好吧

【问题讨论】:

  • 将你的 include 更改为 require 并观察失败的地方。
  • 我做到了,它没有失败,jqery 和 bootstrap.js 文件只是不执行
  • $_SERVER['ROOT'] 你的意思是:$_SERVER['DOCUMENT_ROOT']

标签: php include


【解决方案1】:

根据您的示例,index.php 文件缺少某些内容:

<?php
include ($_SERVER['ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>

我假设您没有发布整个文件,因为此处的输出 hmtl 无效 - 缺少 html 开始标记等。您能否提供整个和/或简化的索引文件?

【讨论】:

    【解决方案2】:

    将所有$_SERVER['ROOT'] 实例替换为$_SERVER['DOCUMENT_ROOT']

    【讨论】:

    • 是的,我明白了。谢谢。但是,它仍然无法正常工作。我不应该能够在 /body 标记之前以这种方式加载一些 js 库吗?
    猜你喜欢
    • 1970-01-01
    • 2012-02-13
    • 2017-11-22
    • 1970-01-01
    • 2011-08-02
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多