自己动手做通用页眉/footer.php [英] do-it-yourself universal header/footer.php

查看:19
本文介绍了自己动手做通用页眉/footer.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作通用的页眉/页脚包含文件.
通用在这里意味着适用于任何目录级别的文件,从includes"目录调用header.php"时无需在任何更深的级别添加../".

好的.
我可以使用
<?php include $_SERVER['DOCUMENT_ROOT']."/includes/header.php";?>

I wanna make universal header/footer include files.
Universal here means to be applicable in files on any directory level without need to add "../" at any deeper level when calling "header.php" from "includes" directory.

Ok, fine.
I can use
<?php include $_SERVER[‘DOCUMENT_ROOT’]."/includes/header.php";?>

将它放在任何地方,但 header.php 包含对 .css 文件的相对引用,而 .css 文件包含其相对引用(例如background: url(../images/o.gif);"和所有这些都让我在每个新级别都陷入../../"的泥潭.

当然,我可以在每个级别复制 .css 和 ../images ,但这似乎有点尴尬,而且与伟大的 php 的原则和精神背道而驰(在一个地方控制所有).

to have it anywhere but the header.php contains a relative reference to a .css file and the .css file contains its relative references (for instance "background: url(../images/o.gif);" and all that lands me in a quagmire of "../../" at every new level.

Of course, I could replicate the .css and ../images at every level yet it seems a bit awkward and contrary to the very principle and spirit of the great php (control all at one place).

尊敬的你`谢尔盖·奥戈尔佐夫

Respectfully yours` sehrguey ogoltsoff

推荐答案

你可以将 CSS 中的 url 设为绝对路径(以 / 开头).然后,无论用户浏览了您网站上的哪个位置,它都可以正常工作.

You could have the url in the CSS be an absolute path (one that starts with /). Then it will work regardless of where on your site the user has browsed.

或者,您可以使用 URL 重写 mod_rewrite 使用户访问的 URL 保持在顶级.

Alternatively, you could use URL rewriting mod_rewrite to make the URL the user visits stay in the top level.

这篇关于自己动手做通用页眉/footer.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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