kreut — 2012-04-12T08:32:18-04:00 — #1
On my first PHP project, I coded all of my include/require files as:
It's kind of a pain and not super clear coding in my opinion. Diving into my second PHP project, I was hoping to use the $SERVER global variable and DOCUMENTROOT as below:
Is this better coding practice or might there be another solution that I'm not aware of?
ralphm — 2012-04-12T09:02:35-04:00 — #2
Personally, I much prefer that solution, as you can then use the same link anywhere without having to worry about relative paths.
kreut — 2012-04-12T09:51:04-04:00 — #3
Thanks so much for the quick reply!
kreut — 2012-04-12T14:25:57-04:00 — #4
I've now given the whole $SERVER["DOCUMENTROOT"] some thought and now think that there may be one small issue. Let's say I develop the site for www.thebestsiteever.com. However, it turns out that it needs to be all moved to a subdirectory (this is actually a possibility for the project), so that all of my files now reside in www.thebestsiteever.com/thebestfolderever/. If this does happen, wouldn't I have to change all of my $SERVER["DOCUMENTROOT"] to reflect this with the additional layer? And, if this is the case, might there be a more "flexible" solution?
parkint — 2012-04-12T14:40:14-04:00 — #5
How about this?
require_once($_SERVER["DOCUMENT_ROOT"] . $SITE_DIR . '/main/restrict_access_index.php');
Just set a global $SITE_DIR (or whatever name you like) to an empty string. If/when the site moves, simple modify that global variable!
kreut — 2012-04-12T14:48:10-04:00 — #6
rvand85 — 2012-04-12T18:49:08-04:00 — #7
I recently started using constants for this. In my configuration file i declare constants with the right paths. If site moves I change soms constants.
But I don't know If this is good practise