PHP包含问题


PHP Include Trouble

我已经尝试将includes作为我网站的一部分大约两周了。通过多个问题和长期的研究,我发现php是最好的方法。我开始尝试以html注释的方式在服务器端进行include,然后我去了php,研究了include工作所需的所有不同扩展,等等。我已经为mac下载了MAMP,知道它可以使用include。我用php反复尝试类似的代码,但无论出于什么原因,它都不起作用。无论是youtube上的newposton还是php的手册,我都没有成功。

最后,我发现http://www.w3schools.com/php/php_includes.asp。我把他们的代码保存到一个文件夹中,并在MAMP上运行,它神奇地起了作用。我想如果我在我的网站上实现这一点,它就会起作用。同样,没有成功。所以我请求你的帮助。

代码:

页脚:(Footer.php)

<?php echo'<div class ="footer">
<p>Contact Us! <br> @allwaterpolo</p>
<a href="#"><img src ="img/Facebook_Png_01.png" class="facebook" id="FaceBook Link" /> </a>
<a href="#"><img src ="img/Uiconstock-Socialmedia-Instagram.ico" class="instagram" id="Instagram Link" /> </a>
 <a href="#"><img src ="img/Twitter_Logo_Hd_Png_03.png" class="twitter" id="Twitter Link" /> </a>
<a href="#"><img src ="img/Youtube_icon.png" class="youtube" id="YouTube Link" /> </a>
<div class="aboutus">
<a href ="htmlfiles/aboutUs.php">About <br>Us</a>
</div>
<div class= copyright>
Copyright &copy; 2016<script>new Date().getFullYear()>2016&&document.write("-"+new Date().getFullYear());</script>, <br> All Water Polo. All rights reserved
</div>
</div> ';?>

标题:(Header.php)

<?php echo'<div class ="header">
<h1>The Best Sport</h1>
<h1 class="sitetitle">AllWaterPolo</h1>
<img src ="img/51wmckj8p1l__sx300__1.png" class="wpball" alt="Water Polo Ball" />
<h2 class="homeScreenLink"> <a href ="index.html">Water Polo!</a></h2> </div>';?>

导航栏:(navbar.php)

<?php echo'<div class="navBar">
    <ul>
    <li><a href ="htmlfiles/history.php">History
    </a></li>
    <li><a href ="htmlfiles/started.php"> Get Started</a></li>
    <li><a href ="htmlfiles/positions.php">Positions</a></li>
    <li><a href ="htmlfiles/rules.php">Rules</a></li>
    <li><a href ="htmlfiles/nationalleague.php">National League</a></li>
    <li> <a href ="htmlfiles/extras.php">Extras</a>
        <ul>
            <li><a href="#">Videos</a> </li>
            <li><a href="#">Plays</a> </li>
            <li><a href="#">TBA</a> </li>
        </ul>
    </li>
    </ul>
</div>';?>

我的网站中的一个页面:(aboutUs.php)

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css" />
<script type="text/javascript" src="main.js"></script>
<title> Water Polo, The Best Sport</title>
<link rel="icon" type="image/png" href="img/favicon.ico" />
</head>
<body>
<div class="wrapper">
<?php
include 'header.php' ;
?>
<?php
include 'navbar.php' ;
?>
<div class= "content">
</div>
<?php
include 'footer.php' ;
?>
</div>
</body>
</html>

(我还不允许发布直接图片)目录结构

这次是…的版本/includes/header.php我的页面显示了一些希望。唯一的问题是includes没有访问css和图像文件,最终出现了这样的情况:对不起,不能发布两张图片,但新页面没有css影响,看起来像纯文本。

如果您正在使用MAMP,那么我相信您将网站文件放入一个名为www的文件夹中,对吗?假设这是正确的。您有一个名为waterpolo-site的网站文件夹,因此您应该能够通过在Safari的地址栏中键入以下内容来显示网站:

localhost/waterpolo-site

Safari的地址栏应该显示:http://waterpolo-site/index.html。如果这不正确,请纠正我。

(1) 通常,我们会这样构建网站:

example.com
    - css
        styles.css
        bootstrap.min.css
    - js
        index.js
        jquery.min.js
        bootstrap.min.js
    - img
        logo.jpg
        pic1.jpg
        pic2.jpg
        another_pic.jpg
    - inc
        connect.inc.php
        head.inc.php
        header.inc.php
        footer.inc.php
        nav.inc.php
    index.php
    about.php
    history.php
    positions.php

如果你按照上面的结构构建网站文件夹结构,那么所有文件的路径都是直接的:include "folder/file.ext"<link href="folder/file.ext"

(2) 文件head.inc.php可能看起来像这样:

<?php
    session_start();
    include 'inc/connect.inc.php';
    //Any more PHP instructions here
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>My Soccer Website</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="A description of my website goes here" />
    <meta name="keywords" content="soccer, football, hooligans, beer, beckham, stadium, scalpers" />
    <meta name="robots" content="index,follow" />
    <link rel="icon" type="image/png" href="img/favicon.png?v=2" />
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <link href="http://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
</head>

(3) 注意,PHP代码只用于做PHP的事情——调用函数、数据库查找、session_start等。一旦关闭PHP标记?>,HTML就会像.html文件一样输出。

(4) 以.php结尾的文件与以.html结尾的文件之间的主要区别在于,PHP文件还可以处理PHP命令。两者可以完全相同地处理HTML。确实没有必要使用文件扩展名.html

(5) 注意favicon上的小技巧。如果您更新了一个文件(css、js等),并且希望访问者使用新文件,而不是存储在浏览器缓存中的以前提取的版本,那么您可以添加一个?xxx=yyy伪GET参数并presto!他们将从您的服务器下载更新版本。不过,下次他们访问时,他们可能会使用缓存中的版本,除非您再次更改GET参数。

PHP include语句是相对于通过它访问的文件。。。

例如:

如果你使用index.php访问网站,那么包含的内容需要是…

include("includes/footer.php");

如果您直接访问页面,例如:http://domain.com/htmlfiles/aboutUs.php那么include需要是…

include("../includes/footer.php");

使用../路径前缀相当于说在目录结构中向上一级。

使用相对文件路径执行include时,PHP首先使用当前目录查找文件,然后在include路径中查找(include路径可以通过phpinfo()找到)。

在您的情况下,footer.phpnavbar.phpheader.php都位于includes目录中,但aboutUs.php位于htmlfiles目录中。

要修复它,你可以这样做:

aboutUs.php

include dirname(__DIR__) . '/includes/header.php';

经验对话:我们通常不做include '../includes/header.php',因为这是邪恶的。虽然乍一看很好,但如果在另一个目录中有另一个文件,请尝试包含aboutUs.php,相对路径将发生更改,并且再次找不到您的头。执行dirname(__DIR__)是为了确保"我希望PHP相对于当前文件的目录搜索该文件,而不是其他文件"


include语句非常强大,因为它也可以用于其他奇特的东西,例如,包括另一个php文件中的数组(用于配置),甚至包括URL中的内容。请仔细阅读:)