I need to add my "web app" to an existing site. It will be placed in a separate folder.
What is the best way to define the root for my app? It will have no interaction with the rest of the site.
I thought of just creating a .php
script in my root and do something like:
define('ROOT_DIR', dirname(__FILE__));
And then include this file in all other scripts.
Is this the right way to do it?
Your method is also correct but it is matter of personal choice. I prefer using absolute URL over relatives as I personally feel absolute URL are less error prone.
For any web application that we make in core PHP (without using any framework), we define a config.php file with two following entries (Obviously with other too)
First two lines on any other php file are:
After that, all the paths/URL use $WEBPATH or $OSPATH.
FROM PHP DOC on
dirname(__FILE__)
or__DIR__
This can easily change depending on our be structure and one single config file
eg.
My Advice .. HARD CODE it since it is always fixed
if you have a file in
/public_html/class/HelloWord.class.php
You can easy access it has the following no matter your current directory