如何在不同的页面上显示相同的页眉和页脚? [英] How to display same header and footer on different pages?

查看:170
本文介绍了如何在不同的页面上显示相同的页眉和页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的所有页面中包含的header.php和footer.php文件。示例页面如下所示 -

 <?php include($ _ SERVER ['DOCUMENT_ROOT']。'/ header.php );?> 

< div id =content>
...
< / div> <! - 内容 - >

<?php include($ _ SERVER ['DOCUMENT_ROOT']。'/ footer.php')?>

虽然这在服务器上运行良好,但是当我在本地测试页面时[Windows 7上的xampp],

 警告:include(C:/ xampp / htdocs)我收到以下错误消息而不是页眉,类似于页脚/header.php)[function.include]:无法打开流:第1行没有C:\xampp\htdocs\f\index.php中的文件或目录

警告:include()[function.include]:打开'C:/xampp/htdocs/header.php'失败(C:\xampp\php\PEAR')第1行上的xampp \htdocs\f\index.php

这使得测试非常繁琐,我必须上传到服务器进行每一个小改动。



另外,我挖掘WP代码,并使用get-header()显示标题。 PHP文件。我无法完全理解这个功能。

包含页眉和页脚文件的正确方法是什么?

解决方案似乎 $ _ SERVER ['DOCUMENT_ROOT'] 指向 C:\ xampp \htdocs ,而您的脚本位于 C:\xampp\htdocs\f\ ,请检查 $的值_SERVER ['DOCUMENT_ROOT'] 您的本地环境。



编辑:

 <?php 
$ rootDir =;
if(strpos($ _ SERVER ['HTTP_HOST'],'localhost')=== FALSE)
{
// On Production
$ rootDir = $ _SERVER ['DOCUMENT_ROOT ];
}
else
{
//在开发服务器
$ rootDir = $ _SERVER ['DOCUMENT_ROOT']。'/ f';
}

<?php include($ rootDir。'/ header.php');?>

< div id =content>
...
< / div> <! - 内容 - >

<?php include($ rootDir。'/ footer.php')?>>


?>


I have header.php and footer.php files which I include in all of my pages. A sample page looks like this -

<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php');?>

<div id="content">
...
</div> <!-- content -->

<?php include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>

Although this works well on the server, but when I test pages locally [ xampp on Windows 7 ], I get the following error message instead of the header, similarly for the footer -

Warning: include(C:/xampp/htdocs/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\f\index.php on line 1

Warning: include() [function.include]: Failed opening 'C:/xampp/htdocs/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\f\index.php on line 1

This makes testing very tedious as I have to upload to the server for every minor change.

Also, I dug around the WP code, and it uses a get-header() to display the header.php file. I could not completely understand the function. My site does not use WP.

What is the correct way for including header and footer files?

解决方案

It seems that $_SERVER['DOCUMENT_ROOT'] is pointing to C:\xampp\htdocs, while your scripts are at C:\xampp\htdocs\f\, check the value of $_SERVER['DOCUMENT_ROOT'] on your local environment.

edit:

<?php
$rootDir = "";
if(strpos($_SERVER['HTTP_HOST'],'localhost')===FALSE)
{
  //On Production
  $rootDir = $_SERVER['DOCUMENT_ROOT'];
}
else
{
  //On Dev server
  $rootDir = $_SERVER['DOCUMENT_ROOT'].'/f';
}

<?php include($rootDir.'/header.php');?>

<div id="content">
...
</div> <!-- content -->

<?php include($rootDir.'/footer.php') ?>


?>

这篇关于如何在不同的页面上显示相同的页眉和页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆