创建 PHP 页眉/页脚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8054638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a PHP header/footer
提问by rjwctm21
I'm designing a relatively simple site for a friend. I would like to impliment php so that he can change his header/footer without having to go back through every file. Problem is, I'm pretty much totally unfamiliar with how php works. Is there a simplistic way to do this? I've seen a few answers for how to make a php header but they all seem different and I haven't had much success. I'm not saying they don't work (I probably did it wrong) but the simpler in this case, the better.
我正在为朋友设计一个相对简单的网站。我想 impliment php 以便他可以更改页眉/页脚而无需返回每个文件。问题是,我几乎完全不熟悉 php 的工作原理。有没有一种简单的方法可以做到这一点?我已经看到了一些关于如何制作 php 标头的答案,但它们看起来都不一样,而且我没有取得太大的成功。我并不是说它们不起作用(我可能做错了)但在这种情况下越简单越好。
Thanks!
谢谢!
回答by William
Besides just using include()
or include_once()
to include the header and footer, one thing I have found useful is being able to have a custom page title or custom head tags to be included for each page, yet still have the header in a partial include. I usually accomplish this as follows:
除了仅使用include()
或include_once()
包含页眉和页脚之外,我发现有用的一件事是能够为每个页面包含自定义页面标题或自定义标题标签,但仍然在部分包含中包含页眉。我通常按以下方式完成此操作:
In the site pages:
在网站页面中:
<?php
$PageTitle="New Page Title";
function customPageHeader(){?>
<!--Arbitrary HTML Tags-->
<?php }
include_once('header.php');
//body contents go here
include_once('footer.php');
?>
And, in the header.php file:
并且,在 header.php 文件中:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title><?= isset($PageTitle) ? $PageTitle : "Default Title"?></title>
<!-- Additional tags here -->
<?php if (function_exists('customPageHeader')){
customPageHeader();
}?>
</head>
<body>
Maybe a bit beyond the scope of your original question, but it is useful to allow a bit more flexibility with the include.
也许有点超出您原来问题的范围,但允许包含更多的灵活性是有用的。
回答by Ben
Just create the header.php file, and where you want to use it do:
只需创建 header.php 文件,并在您想要使用它的地方执行以下操作:
<?php
include('header.php');
?>
Same with the footer. You don't need php tags in these files if you just have html.
与页脚相同。如果您只有 html,则这些文件中不需要 php 标签。
See more about include here:
在此处查看有关包含的更多信息:
回答by Mohan Shanmugam
You can do it by using include_once()
function in php. Construct a header part in the name of header.php
and construct the footer part by footer.php
. Finally include all the content in one file.
您可以通过include_once()
在 php 中使用函数来完成。以 的名称构造页眉header.php
部分并由 构造页脚部分footer.php
。最后将所有内容包含在一个文件中。
For example:
例如:
header.php
头文件
<html>
<title>
<link href="sample.css">
footer.php
页脚.php
</html>
So the final files look like
所以最终文件看起来像
include_once("header.php")
body content(The body content changes based on the file dynamically)
include_once("footer.php")
回答by Mohan Shanmugam
You can use this for header: Important:Put the following on your PHPpages that you want to include the content.
您可以将其用于标题: 重要提示:将以下内容放在您想要包含内容的PHP页面上。
<?php
//at top:
require('header.php');
?>
<?php
// at bottom:
require('footer.php');
?>
You can also include a navbar globaly just use this instead:
您还可以包含一个全局导航栏,只需使用它:
<?php
// At top:
require('header.php');
?>
<?php
// At bottom:
require('footer.php');
?>
<?php
//Wherever navbar goes:
require('navbar.php');
?>
In header.php:
在 header.php 中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
Do Not close Body or Html tags!
Include html here:
不要关闭 Body 或 Html 标签!
在此处包含 html:
<?php
//Or more global php here:
?>
Footer.php:
页脚.php:
Code here:
代码在这里:
<?php
//code
?>
Navbar.php:
导航栏.php:
<p> Include html code here</p>
<?php
//Include Navbar PHP code here
?>
Benifits:
好处:
- Cleaner main php file (index.php) script.
- Change the header or footer. etc to change it on all pages with the include— Good for alerts on all pages etc...
- Time Saving!
- Faster page loads!
- you can have as many files to include as needed!
- server sided!
- 更清洁的主 php 文件 (index.php) 脚本。
- 更改页眉或页脚。等在所有页面上更改它,包括——适用于所有页面上的警报等......
- 节省时间!
- 更快的页面加载!
- 您可以根据需要包含尽可能多的文件!
- 服务器端!
回答by Your Common Sense
the simpler, the better.
越简单越好。
index.php
索引.php
<?
if (empty($_SERVER['QUERY_STRING'])) {
$name="index";
} else {
$name=basename($_SERVER['QUERY_STRING']);
}
$file="txt/".$name.".htm";
if (is_readable($file)) {
include 'header.php';
readfile($file);
} else {
header("HTTP/1.0 404 Not Found");
exit;
}
?>
header.php
头文件
<a href="index.php">Main page</a><br>
<a href=?about>About</a><br>
<a href=?links>Links</a><br>
<br><br>
the actual static html pages stored in the txt
folder in the page
.htm format
以.htm 格式存储在txt
文件夹中的实际静态 html 页面page