$_SERVER['PHP_SELF'] returns the name of the script file it is running,
on the root of the document.
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 above script for the URL http://www.exemplo.com/estoque/produto.php?id=5
will produce ../produto.php
If the menu files are in 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 and so forth.
The variable $relative resolves to index or any other file that is in the root, and its application to any other folder. However, it cannot be applied to other menu links if they are in other folders. In this case use a/ bar that you specify 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>
Thank you so much Anderson for the explanation !
– Gabriel Victor