如何在服务器根文件夹(PHP)中管理和构建页面和链接

时间:2014-09-29 08:46:04

标签: php xampp include-path document-root

我没有使用任何框架,只是在XAMPP环境中工作。

像我所有的一样,我也有一个索引文件index.php,最重要的是我想在我的所有页面上添加相同的标题。

在我的index.php页面中,我添加了我的标题,如

<?php include 'html/headers/header.html'; ?>

并在我的page.php页面中,它位于根文件夹中的内部信息文件夹中,如c:/ xampp / htdocs / website / info

<?php include '../html/headers/header.html'; ?>

所有工作都很好,但是当heeader中的图像路径保持不变时出现问题,即图像路径对index.php文件有效,但对于page.php文件则不行,这是你已经知道的原因。

我试过$_SERVER['DOCUMENT_ROOT']."/website/images/logo.png".它既不起作用也不想要这种技术,因为在页眉和页脚等中可以有很多图像。

我也不想使用任何框架或cdn存储。

这可以轻松解决吗?如果我错过任何事情,请告诉我。感谢名单

2 个答案:

答案 0 :(得分:0)

当我在PHP编码时,我的每个PHP文件的开头往往会有两个变量。

// Use for file on the server, eg includes
$file_path = "../";

// For files relative to my page path, eg images, links
$link_path = "../../";

我根据文件的位置更改其中的每一个。

然后我使用$file_path在页面上添加$link_path和页面上的内容(例如图片,超链接)。

echo '<img src="' . $link_path . 'images/myimage.png" />';
include ($file_path . "html/headers/header.html");

无论如何,我是怎么做的......

将变量设置在您创建的每个新页面的顶部,并将其设置在页面和任何包含内。

// New file
$file_path = "../";
$link_path = "../../";

include($file_path . "html/headers/header.html");

然后在您的header.html和页面内,您就可以了:

echo '<img src="' . $link_path . 'website/images/logo.png" />';

如果你想要一个链接,你可以去:

echo '<a href="' . $link_path . 'website/page.html'>My Link</a>';

答案 1 :(得分:0)

这是你的index.php页面

<?php
    if(isset($_GET['page'])) {
        $dir = 'page/' . $_GET['page']. '.php';
        if(file_exists($dir)) {
            include_once($dir);
        } else {
            include_once('page/404.php');
        }
    } else {
        include_once('page/home.php');
    }
?>

你有像这样的文件夹结构

  • 部分
  • 的index.php

在页面中保存home.php,info.php等。 在节中你保存header.php,footer.php等。

比你使用index.php中的图像src并且你没有任何问题。