i’m building a php cms style app for tracking of members characters, the projects coming along nicely, but I am wondering if there is a more efficient way of coding my navigation bar.
the bar is included in many different pages so relative links as standard will not work for obvious reasons, the way I have been getting around this is by
if (strstr($_SERVER['SCRIPT_FILENAME'], "admin/char/")) {
$home = "../../index.php";
$logout = "../user/logout.php";
} else if (strstr($_SERVER['SCRIPT_FILENAME'], "admin/user/")) {
etc.
so I have each combination of path that could be typed, and this solution works, and quite well, but since I am copying pasting code and making changes the chances of me missing something is increased, and with more links this chance is increased.
I am wondering if there a better way of achieving the same result with less code?
Any advice would be greatly appreciated.
You might want to create a common configuration file (include in all your scripts) where you define a constant (e.g. BASE_URL = ‘http://localhost/your_site/’).
Then, change all your links with ABSOLUTE values instead, using the BASE_URL constant defined in the configuration file.
In the end you will have something like:
which will always be a valid link.
At deployment, you will need to change the configuration file and set
BASE_URLas your new domain (e.g.www.mysite.com).