[英]How can I create a absolute url for my site using php?
嗨,我正在嘗試使用php創建網站,其結構如下所示:
-index.php
-header.php
-footer.php
-style.css
-images\{images are in this folder}
-\web\index.php
-\contact\index.php
問題是我在所有index.php文件上使用php的include函數來使用header.php和footer.php文件,但是問題是只有根文件夾中的index.php顯示樣式,但是當我打開localhost / web / index.php,頁面找不到,因為沒有CSS和圖像。
我該如何解決這個問題。
請幫助
以類似的名稱添加網站名稱
$site_base_url = "http://localhost/web";
之后,當您使用php之類的代碼時,將其添加到url的前面
echo '<link type="text/css" href="'.$site_base_url.'/style.css" rel="stylesheet" charset="utf-8">'
或喜歡
echo '<img src="'.$site_base_url.'/images/file_name.jpg" />'
當您使用函數時,像這樣global
使用
function find_user(){
global $site_base_url;
echo "no users found";
echo '<a href="$site_base_url">return home</a>
}
您可以require_once($_SERVER['DOCUMENT_ROOT'].'\\header.php');
試試這個結構:
的index.php
styles / style.css
templates / header.php
template / footer.php
圖片/ {圖片在這里}
我強烈建議您閱讀本教程。 對於初學者來說,這確實很有幫助且容易。
在您的style.css中,您可以使用
background-image:url(/images/mycoolpic.png);
很容易。
您必須從站點根目錄開始,每個路徑都是/
。
因此,將style.css /style.css
為/style.css
可以從站點的任何頁面調用它。
該網站上的所有本地超鏈接也是如此-只需從/
啟動它們(接着是正確的路徑)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.