2014-05-07 157 views
2

我一直在使用PHP構建網站。我對使頁面正確顯示的方法或技巧感到困惑。使用.html和.php對頁眉和頁腳進行說明

我的主要入門點是index.html。從這裏,用戶可以點擊瀏覽。我希望所有12個頁面具有相同的標題信息。 我試過在Dreamweaver中使用標籤,然後只是標籤。

??我感到困惑的使用標籤和使用 文件之間的差異被稱爲header.php

跟進:要建議的對策。我編輯代碼建議
現在,當我'在瀏覽器中查看'我得到'服務器未找到'。

注意。我使用DreamweaverCC:

文檔根目錄/庫/ Web服務器/文件

的header.php

<!doctype html> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
</head> 

<body> 
    <h1>General Header Information for Top of Each Page</h1> 

的index.html

<?php include_once ('header2.php'); ?> 
<!doctype html> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title>index.php</title> 
<link href="file:////Library/WebServer/Documents/webDevelopDWsites  /boilerplate.css" rel="stylesheet" type="text/css"> 
<link href="file:///Library/WebServer/Documents/webDevelopDWsites/indexFourSquare.css" rel="stylesheet" type="text/css"> 

<script src="file:///Library/WebServer/Documents/webDevelopDWsites/respond.min.js"> </script> 
</head> 
<body> 

<?php include 'header2.php'; ?> 
    <div class="gridContainer clearfix"> 
    <div id="div1" class="fluid"> 
    <header id="header" class="fluid">Web Development by myName</header> 

    <div id="Square1" class="fluid"><a href="file:///Library/WebServer/Documents /webDevelopDWsitesimages/Square1.jpg"><img src="images/Square1.jpg" alt=""/></a></div> 

    <div id="Square2" class="fluid"><a href="file:///Library/WebServer/Documents/webDevelopDWsites/Square2.html"><img src="images/Square2.jpg" alt=""/></a></div> 

    <div id="Square3" class="fluid"><a href="file:///Library/WebServer/Documents/webDevelopDWsites/Square3.html"><img src="images/Square3.jpg" alt=""/></a></div> 

    <div id="Square4" class="fluid"><a href="file:///Library/WebServer/Documents/webDevelopDWsites/Square4.html"><img src="images/Square4.jpg" alt=""/></a></div> 

    <div id="siteMap" class="fluid">siteMap</div> 
    </div> 
</div> 
</body> 
</html> 

我是否包括header.php在每個其他網頁的代碼中?我做了這個,但沒有奏效。

所以我試過這個:我創建了一個index.php並將所有的index.html代碼複製到index.php中,並且在代碼的頂部添加了這個代碼。

<?php include 'header.php'; ?> 

當我使用 '在瀏覽器中查看' 啓動index.html: '無頭信息'

  • 我得到的網頁正確

當我在圖像上點擊瀏覽到其他頁面,它的工作原理:

  • 但「沒有頭信息」。

當我點擊index.php

  • 我只得到了標題信息。
+0

默認情況下,您必須爲您的擴展名指定「.php」。否則,你的php代碼將不會被解釋。 – OpenStark

+0

@iHaveAQuestion編輯中的禁用術語是什麼?我改變了標題,以便您的問題更容易找到,並且您可能會得到更多答案。 –

+0

@icanhascheezburger進展。 '無法連接'文檔根目錄庫/ Web服務器/文檔。我創建了一個新文件夾:Library/WebServer/Documents/dwwdSite(= DreamWeaverWebDevelopmentSite)。但是當我輸入URL localhost/dwwdSite/error:'無法連接'。 dwwdSite的網站設置:name = localhost。地址= macHD/library/WebServer/Documents。連接=本地/網絡。測試檢查。本地信息網址:Http:// localhost /。網站名稱= dwwdSite。本地站點文件夾=/Library/WebServer/Documents/dwwdSite。我覺得在這些設置中我必須有不正確的東西。仍然'無法連接'。 – iHaveAQuestion

回答

3

通常情況下,一個網站被分爲3個部分:

enter image description here

頁眉和頁腳遵循所有的頁面,這僅僅是那間變化。所以在你的頭上。PHP,你將有:

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>headerForFourSquares 
</title> 
</head> 
<body>  
     <h1>General Header Information for Top of Each Page.</h1> 

就是這樣,沒有在這裏結束的<body>標籤。

現在,在您的index.php ,你會怎麼做:

<?php include_once('header.php'); ?> 

//all your index content here 

<?php include_once('footer.php'); ?> 

最後footer.php

</body> 
</html> 

這是它的工作方式。對於其他每個頁面,您都應該包含上面的頁眉和頁腳。

您的索引文件擴展名爲.php。如果他們沒有PHP代碼,則頁眉和頁腳文件可以是.html擴展名。

You say index.html is not going to execute any php code but you do not explain why.

當你在你的.html文件的PHP代碼,當瀏覽器打開一個.html文件,其中包含PHP代碼,它不希望任何PHP代碼是存在,因此不解釋它。雖然,你可以強制它解釋下面的here PHP,但這本身是一個不好的做法,因爲其他語言如<?xml之間可能會出現衝突,因爲PHP也以<?開頭。

,以及爲什麼需要通過本地主機,而不是通過瀏覽器打開該文件路徑來運行它的原因是因爲,雖然HTML是一種客戶端腳本,它可以自由地,沒有任何服務器上運行,但PHP是一個服務器 - 並且將被服務器解釋爲只有,這就是爲什麼我們使用web服務器軟件包(如xampp,wamp等)在本地進行測試的原因。

+2

有關'index.html'的注意事項,許多動態網站使用它們各自的語言擴展名作爲引導文件。然而,他們通過重寫URL(例如apache mod重寫)將它們僞裝爲純HTML文件。這就是爲什麼他們儘管出現靜態html文件動態。 – Flosculus

+0

我很欣賞這個反饋。此評論框僅允許500個以上的字符。我想包括我的代碼。我該怎麼做呢 ????當我啓動index.php時,我收到'服務器未找到錯誤'。當我啓動(在瀏覽器中查看)index.html時,我不明白這一點。 – iHaveAQuestion

+0

@iHaveAQuestion index.html不會執行任何PHP代碼,因此您可以忘記它,至於代碼,您可以編輯上面的問題並粘貼。 –