March 29, 2011

How to detect the path to the application root?

Question by auge

I’m trying to dynamically detect the root directory of my page in order to direct to a specific script.

echo ($_SERVER['DOCUMENT_ROOT']); 

It prints /myName/folder/index.php

I’d like to use in a html-file to enter a certain script like this:

<a href="<?php $_SERVER['DOCUMENT_ROOT'].'lib/logout.php'?>">log out</a>

This seems to be in bad syntax, the path is not successfully resolved.

What’s the proper approach to detect the path to logout.php?

The same question in different words:

How can I reliably achieve the path to the root directory (which contains my index.php) from ANY subdirectory? No matter if the html file is in /lib/subfolder or in /anotherDirectory, I want it to have a link directing to /lib/logout.php

On my machine it’s supposed to be http://localhost/myName/folder (which contains index.php and all subdirectories), on someone else’s it might be http://localhost/project

How can I detect the path to application root?

Answer by Starx

Try this,

<a href="/lib/logout.php'?>">log out</a>

This jumps to the root directly.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!