Jump to content

single nav file referenced from various subfolders - links failing


Recommended Posts



I am designing a new web page with a navigation bar (nav.php) that is used by pages throughout the site. The "nav" is in the main directory, some other pages are in the sub-directories.


The (relative) links in "nav" do not work when referenced from a "file" in a subdirectory. I suspect this is because they are being read relative from the "file" directory, rather than the directory that "nav" is in.


Please confirm that this would be the case!?!

Please advise is this a suitable practice of using a single "nav" across all subdirectories (I was planning on nesting to a second level so that when the about button for instance is clicked it would show something like:

      HOME      ABOUT    SHOP      [level 1]

    RED     BLUE     YELLOW    BLACK      [level 2 about red ones] so that a second nav bar in the about folder nested under the main "nav") or is it typical to just put a different one in each directory so they all have to be updated, or do I just put all the files in one directory or is there some other way that this might better be done?


I've nearly got this working [file attached]:

<?php $path = $server['DOCUMENT_ROOT']; $path .= "/subpath/public"?>

<html> <a href=<?php print "$path";?>/pathext/about1.php>ABOUT1</a></html>

When I view the source on my browser it is right, I can even cut and paste the path/file back into the browser and it works, but it doesn't work in the code. What am I am doing wrong here?





Link to comment
Share on other sites

$_SERVER['DOCUMENT_ROOT'] is a file system path. it only has meaning on the server where it is at. it is NOT usable in the html on your page. the links in your html must be URLs that are either relative paths to the domain root or the page they are on or an absolute url.


links that are built using $_SERVER['DOCUMENT_ROOT'] will work on your localhost development system since the browser is also running on that system and can request the file through the file system.


for an absolute URL, i.e. http://somedomain.com/some_path/some_file.php or http://localhost/some_path/some_file.php, the browser uses that url as is in the http request to the server.


for relative URLs, a domain relative url looks like - /some_path/some_file.php. the leading slash refers to the document root. the browser takes the current page's http://somedomain.com or http://localhost and appends the domain relative url to it.


for all other relative URLs, i.e. some_path/some_file.php, the browser takes the current page's http://somedomain.com/some_other_path or http://localhost/some_other_path and appends the relative url to it.

Link to comment
Share on other sites

Yeah I would prefer to stay away from server side coding and am just trying to prevent having the same menu in 20 pages that will then need to be updated 20 times every time I add a page, hence the php, hence the question. What I am about to do, for all the common files (headbanner  - navigation - footer), is introduce a constant which is:

<?php $path = "/projdir/public_html/";?>    and then in the html use:

<a href=<?php print "$path";?>index.php"><img src="<?php print "$path;?>images/header.png"></a>

That works, so I expect that I will want to store that in a "config" file rather than on every page, reference the config file on every (required) page so that the constant $path is included, and then just update the config file when I move from my local XAMP test server to my hosted server. Please let me know yay or nay if I am on the right path. Whats going to happen when I do that and realise I no longer need the constant? Can I just make it <php $path = NULL;?>   ?

Edited by tkyoung75
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.