php root click link problem

Joined
12 Feb 2006
Posts
17,629
Location
Surrey
i'm trying to make my site link to pages from the root directory so that if the page is on level lower it will still link correctly to other pages.


this is all working fine, though with some difficulty because of using xampp, but the pages are able to include other pages.

the trouble i have is with links in that they link all the way back from the drive, and although the folders are correct Firefox cannot click the link popping up with a message about how it doesn't know what to do with the protocol g.

iirc from times before this will only be whilst developing the site on my pc but once online will be fine, however i don't want to have to re upload the pages every time i change them so would really like a fix for this.

thinking about it i think i'd need to change what the root directory is for xampp, currently htdocs. quick google is showing nothing, and also thinking that as htdocs is root but it still links from g://program files/my docuemtns etc, that changing it wont do anything.
 
I think what you want is for the URL to be absolute, i.e. from the docroot rather than the current directory. You can't just use the local path because request URIs have to go through the HTTP server (Apache) before they're mapped to local resources. Every (local) URI you use on your site must be relative to the site's document root (htdocs in the case of XAMPP).
 
You can't use $_SERVER['DOCUMENT_ROOT'] in link URIs because it gives the local path to the document root, e.g. G:\XAMPP\htdocs. When you upload it to your server, which presumably is running Linux, you'll get URIs that look something like /home/mammalian/www/page.php. Obviously, putting that into your browser isn't going to work.

Basically, there are three types of behaviour that a link URI can have:
  • If the URI is preceded by a protocol identifier (some letters followed by a colon, e.g. http: ), then the browser will treat it as a completely new address.
  • If the URI is preceded by a forward slash, then the browser will treat it as a URI to something on the same site, relative to the document root. In this case, the link target is appended to the site's root address. For example <a href="/foo/bar.php">Bar</a> corresponds to http://domain.com/foo/bar.php
  • Otherwise, it is treated as a URI to something on the same site, relative to the current directory. In this case, the link target is appended to the current address. For example, <a href="foo/bar.php">Bar</a>, on http://domain.com/foo/baz.php will correspond to http://domain.com/foo/foo/bar.php.

So basically, just give the path to your page relative to the docroot and add a slash to the front.
 
Last edited:
Good post inquisitor, i didn't know a link preceded by a forward slash is relative from document root, learn something every day^^
 
also not a good idea to use $_SERVER['DOCUMENT_ROOT'] as it can be different on different servers - ie. one server might have it with the trailing / and one might not, meaning you'll have to go through and change everything that uses it.
 
also not a good idea to use $_SERVER['DOCUMENT_ROOT'] as it can be different on different servers - ie. one server might have it with the trailing / and one might not, meaning you'll have to go through and change everything that uses it.

well what i actually did was have all the files use $root, and then just include a file called start which defines what $root is, this way allowing for if something was different on the server from my pc then all i have to do is change the location of the start.php file to where document root links to, if that makes sense.

So basically, just give the path to your page relative to the docroot and add a slash to the front.

i must be doing something wrong as it still doesn't work.

what i basically have structure wise, names are just generic names for this example

Code:
index
contact
about
terms
etc
- memberFolder
     ---- profile
     ---- editProfile

- includesFolder
      ---- headerList
all the pages include the headerList page which lists the links that go in the header, so index, contact, about etc.

the trouble is then that when the headerList is included to profile.php it links to index in memberFolder which doesn't exist.

i have added the forward slash infront of the links, so they are linked like /index.php but it still doesn't work :confused:
 
Last edited:
Can you post your HTML? A link to /index.php should send the browser to the index.php file in the docroot regardless of where it currently is.
 
edit

silly me it does work, just i was missing a folder that all this site is in, which is going to be a bit of a bugger later but will find a way around this.

thanks everyone
 
Last edited:
ok new problem kind of, using the / at the front when doing a php include doesn't start at the root directory like a link does.

i have a fix for this but it's not as pretty as just putting a forward slash so is there a solution for this?
 
php include (or include_once() ;)) works in such a strange way, and the error messages are so vague, it can be really frustrating to try and figure out where it's going wrong. Try using ./ to look in the current directory and ../ to go up a directory.
 
Back
Top Bottom