![](/img/trans.png)
[英]Can a CSS code work in all php files if I set the style.css in the header.php but I include header.php in all other php files?
[英]How to use same header.php file for all contentc php files in all directory
我有如下創建頭文件。
<style type="text/css" >
.menu{
width: 100%;
/*background-color: #333; */}
.menu ul{
margin: 0; padding: 0;
float: left;}
.menu ul li{
display: inline;}
.menu ul li a{
float: left; text-decoration: none;
color: #000;
padding: 10.5px 11px;
/* background-color: #333; */}
/*
.menu ul li a:visited{
color: white;}*/
.menu ul li a:hover, .menu ul li .current{
color: #fff;
background-color:#0b75b2;}
}
</style>
<div class="menu" style="width:430px; height:43px; margin-left:420px; margin-right:auto;">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About us</a></li>
<li><a href="contact.php">Contact us</a></li>
<li><a href="image-gallery/index.php">Image Gallery</a></li>
<li><a href="articles/index.php">Articles </a></li>
</ul>
</div>
所以我有參考目錄,例如圖片庫,文章
文件路徑 :
www/websitename/index.php
我已經將header.php包含到主index.php中,它正確鏈接了,但是當我包含image-gallery的索引文件未正確鏈接時,
www/websitename/image-gallery/index.php
www/websitename/image-gallery/index.php
有各種各樣的解決方案,但是最簡單的實現方法是在config.php文件中有一個定義。 除其他事項外,define將指定根文件和根路徑
config.php
define('ROOT_URL', 'http://www.example.com/');
define('ROOT_PATH', '/var/www/html');
然后,在index.php中,您將擁有
include('config.php');
include('header.php');
要么
include('../config.php');
include('../header.php');
然后在header.php中
<li><a href="<?php echo ROOT_URL; ?>index.php">Home</a></li>
此主題有很多變體,上面是最簡單的,但這里還有其他變體。
為什么要用這種方法? 因為在config.php中,您可以使用
if($ _SERVER ['HTTP_HOST'] =='staging.example.com'){//一組定義} else {//假設直播-用戶另一組定義}
並從dev / staging / live復制文件而不會覆蓋。
與包含在其中的“ ../”相反,在每個文件中將變量“ root_file”創建為“ ./”或“ ../”。 然后包括($ root_file。'header.php'); 在每個腳本中執行此操作,您也可以將其用於鏈接。
如果指定完整的URL,則如果您在https和http之間滑動,則可能要排除“ http://”部分並添加您所使用的方案。 在代碼中(或在構建鏈接的函數中)添加URL
您可能希望將詳細信息存儲在數據庫中(在這種情況下,您可能會將數據庫連接憑據存儲在config instread中
最后,您可以使用$ _SERVER [''](http://php.net/manual/en/reserved.variables.server.php)變量,或__FILE__
或__ DIR__
(http://php.net/manual/ zh / language.constants.predefined.php)確定您的位置。 請小心,因為REQUEST_URI可能無法准確反映腳本的位置(如果使用.htaccess進行重寫規則),並且如果您處於符號鏈接上,則SCRIPT_FILENAME / __FILE__
/ __DIR__
也可能會說謊。 但是,如果您知道服務器配置,這是處理“我在哪里?”的一種非常可移植的方式。
因此,有很多選擇。 從簡單開始,並根據您的需求逐步建立。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.