PHP 包括麻烦
PHP Include Trouble
大约 2 周以来,我一直在尝试将包含内容作为我网站的一部分。通过多个问题和长期研究,我发现 php 是最好的方法。我开始尝试服务器端包含 html 评论方式,然后我去了 php 并研究了包含工作所需的所有不同扩展等等。我已经为 mac 下载了 MAMP,知道它可以与包含一起使用。我一次又一次地尝试使用 php 的类似代码,但无论出于何种原因,它都不起作用。无论是 youtube 上的 thenewboston 还是 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 © 2016<script>new Date().getFullYear()>2016&&document.write("-"+new Date().getFullYear());</script>, <br> All Water Polo. All rights reserved
</div>
</div> ';?>
Header:(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>
(我还不能 post 直接图片)
Directory Structure
这次是../includes/header.php 我的页面显示了一些希望。唯一的问题是包含没有访问 css 和图像文件,结果是这样的:抱歉不能发布两张图片,但新页面没有 css 影响,看起来像纯文本。
PHP include 语句是相对于它通过...访问的文件的...
EG:
如果您使用 index.php 访问该站点,则包含需要...
include("includes/footer.php");
如果您直接访问页面,例如:http://domain.com/htmlfiles/aboutUs.php 那么包含需要...
include("../includes/footer.php");
使用../
路径前缀相当于在目录结构中说上一层。
当你include
使用相对文件路径时,PHP首先使用当前目录查找文件,然后在包含路径中查找(包含路径可以通过phpinfo()
).
在您的例子中,您的 footer.php
、navbar.php
和 header.php
都位于 includes 目录中,但是您的 aboutUs.php
位于 htmlfiles
目录。
要修复它,您可以这样做:
aboutUs.php
include dirname(__DIR__) . '/includes/header.php';
经验谈:我们一般不做include '../includes/header.php'
,因为这是邪恶的。虽然乍一看看起来不错,但如果在另一个目录中还有另一个文件,请尝试包含 aboutUs.php
,相对路径将改变,并且将再次找不到您的 header。做dirname(__DIR__)
是为了保证"I want the PHP to search this file relative to the directory of this current file, and not other files"
include
语句非常强大,因为它也可以用于其他奇特的东西,例如,包含来自另一个 php 文件的数组(用于配置),甚至包含来自 URL 的内容。请仔细阅读:)
如果您使用 MAMP,那么我相信您将网站文件放入了一个名为 www
的文件夹中,对吗?假设这是正确的。您有一个名为 waterpolo-site
的网站文件夹,因此您应该可以通过将其键入 Safari's address bar:
来显示该网站
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 参数很快!他们将从您的服务器下载更新版本。不过,下次他们访问时,他们可能会使用缓存中的版本,除非您再次更改 GET 参数。
大约 2 周以来,我一直在尝试将包含内容作为我网站的一部分。通过多个问题和长期研究,我发现 php 是最好的方法。我开始尝试服务器端包含 html 评论方式,然后我去了 php 并研究了包含工作所需的所有不同扩展等等。我已经为 mac 下载了 MAMP,知道它可以与包含一起使用。我一次又一次地尝试使用 php 的类似代码,但无论出于何种原因,它都不起作用。无论是 youtube 上的 thenewboston 还是 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 © 2016<script>new Date().getFullYear()>2016&&document.write("-"+new Date().getFullYear());</script>, <br> All Water Polo. All rights reserved
</div>
</div> ';?>
Header:(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>
(我还不能 post 直接图片) Directory Structure
这次是../includes/header.php 我的页面显示了一些希望。唯一的问题是包含没有访问 css 和图像文件,结果是这样的:抱歉不能发布两张图片,但新页面没有 css 影响,看起来像纯文本。
PHP include 语句是相对于它通过...访问的文件的...
EG:
如果您使用 index.php 访问该站点,则包含需要...
include("includes/footer.php");
如果您直接访问页面,例如:http://domain.com/htmlfiles/aboutUs.php 那么包含需要...
include("../includes/footer.php");
使用../
路径前缀相当于在目录结构中说上一层。
当你include
使用相对文件路径时,PHP首先使用当前目录查找文件,然后在包含路径中查找(包含路径可以通过phpinfo()
).
在您的例子中,您的 footer.php
、navbar.php
和 header.php
都位于 includes 目录中,但是您的 aboutUs.php
位于 htmlfiles
目录。
要修复它,您可以这样做:
aboutUs.php
include dirname(__DIR__) . '/includes/header.php';
经验谈:我们一般不做include '../includes/header.php'
,因为这是邪恶的。虽然乍一看看起来不错,但如果在另一个目录中还有另一个文件,请尝试包含 aboutUs.php
,相对路径将改变,并且将再次找不到您的 header。做dirname(__DIR__)
是为了保证"I want the PHP to search this file relative to the directory of this current file, and not other files"
include
语句非常强大,因为它也可以用于其他奇特的东西,例如,包含来自另一个 php 文件的数组(用于配置),甚至包含来自 URL 的内容。请仔细阅读:)
如果您使用 MAMP,那么我相信您将网站文件放入了一个名为 www
的文件夹中,对吗?假设这是正确的。您有一个名为 waterpolo-site
的网站文件夹,因此您应该可以通过将其键入 Safari's address bar:
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 参数很快!他们将从您的服务器下载更新版本。不过,下次他们访问时,他们可能会使用缓存中的版本,除非您再次更改 GET 参数。