【发布时间】:2010-02-08 12:57:52
【问题描述】:
如果我有这样的菜单
<a href="index.php">home</a>
<a href="aboutus.php">about us</a>
<a href="contact.php">contact us</a>
在一个名为 menu.php 的文件中
并且这个 menu.php 文件被包含在整个应用程序的页面中。但是如果我创建一个新文件夹并将其命名为投资组合,并且在此文件夹中有一个名为 example1.php 的文件,并且 menu.php 包含在此文件中,该怎么办。主页、关于我们和联系我们的链接将不再有效,因为它们指向根。所以我创建了这个函数
function getRoot() {
$self = $_SERVER['PHP_SELF'];
$protocol = $_SERVER['https'] == 'on' ? 'https:/' : 'http:/';
$docroot_before = explode("/", $self);
array_pop($docroot_before);
$docroot_after = implode("/", $docroot_before);
$host = $_SERVER['HTTP_HOST'];
return $protocol.$host.$docroot_after."/";
}
菜单现在看起来像这样
<a href="<?=getRoot();?>index.php">home</a>
<a href="<?=getRoot();?>aboutus.php">about us</a>
<a href="<?=getRoot();?>contact.php">contact us</a>
the link should come out as http://localhost/domain/aboutus.php (for example)
but it comes out as http://localhost:/domain/portfolio/aboutus.php (and this is wrong).
无论 menu.php 包含在何处,始终获得正确文档根目录的最佳方法是什么?
【问题讨论】:
标签: php