$ _SERVER ['PHP_SELF'] returns the file name of the script that is running,
relative to the document root .
Example: Suppose you are accessing the URL http://www.exemplo.com/estoque/produto.php?id=5
then $ _SERVER ['PHP_SELF'] returns "/estoque/produto.php"
in your script.
Script to use relative path with notation ../
$caminho = ($_SERVER['PHP_SELF']);
$quant = substr_count($caminho, '/');
$quant=$quant-1;
for ($k = 0; $k < $quant; $k++) {
$relativo.="../";
}
echo "<a href=\"".$relativo."index.php\">index</a>";
The script above for the URL http://www.exemplo.com/estoque/produto.php?id=5
will produce ../produto.php
If the menu files are the same:
<a href=\"".$relativo."index.php\">Início</a>
<a href=\"".$relativo."demo.php">Demo</a>
<a href=\"".$relativo."chat.php">Chat</a>
Directory 1 level above, will produce
<a href="../index.php\">Início</a>
<a href="../demo.php">Demo</a>
<a href="../chat.php">Chat</a>
Directory 2 levels above, will produce
<a href="../../index.php\">Início</a>
<a href="../../demo.php">Demo</a>
<a href="../../chat.php">Chat</a>
And so on.
The variable $ relative resolves to the index or any other file that is in the root, and its application in any other folder. However, it can not be applied to other menu links if they are in other folders. In this case use a / slash that indicates from the root.
<a href="../index.php">Início</a>
<a href="/pastaA/pastaB/demo.php">demo</a>
<a href="/pastaB/chat.php">chat</a>