Jees K Denny Jees K Denny - 2 months ago 7
HTML Question

A file include a file in another directory which have links, How to set relative path for links?

my project directory as follows.

root
css
img
src
login
login.php
dashboard
basic
header.php
footer.php
profile.php
manage.php
department
add_depart.php
configuration
config.php


Here the
header.php
included
profile.php
,
manage.php
,
add_depart.php
and
config.php
.

In the
header.php
have links point to the another files that are kept in
basic
folder. My problem is when i include the
header.php
in
add_depart.php
and
config.php
the links are not working. I know include only copy the file into
add_depart.php
. the links cant access within department folder.

How can i set relative path for the links inside the
header.php
can access the links provided by header from basic,department,and configuration folders.

I am also tried with
getcwd()
,
dirname(__FILE__)
.

Please Help.

Header.php

$path_r = $_SERVER['DOCUMENT_ROOT'];
$path_l = $path_r . '/root/src/dashboard/basic/';
<li class="dropdown">
<a href="#" data-target="#" class="dropdown-toggle" data-toggle="dropdown"> Settings <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="<?php echo $path_l ?>/profile.php">Profile</a></li>
<li><a href="<?php echo $path_l ?>/config.php">Config</a></li>
<li class="divider"></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</li>

Answer

You're confusing the file system paths with the URL path.

$_SERVER['DOCUMENT_ROOT'] contains the absolute path to the document root on your server (the file system), not the URL. The same goes for getcwd() and dirname().

Just start the URLs with a slash: <a href="/path/to/profile.php" etc, and the URL's will be the same no matter where you include it.

Comments