I am trying to get the siteurl without hardcoding and store it in a constant inside of a configuration file, so that I can use it within my scripts irrespective of the nesting/nested folders. This configuration file is stored in a folder within the main installation directory of the website.
Example:
Install website on http://www.mydomain.com
Configuration file located in http://www.mydomain.com/my_folder/configuration.php
Want to declare a constant in configuration.php file so that it always gets the installed path dynamically without having to hardcode it. So define("MY_SITE_URL", ???? )
What should be in the place of the ???? in the code above so that it works even if my main website is installed within nested folders?
Examples: All the pages will contain the same configuration.php at the beginning of the file as a require
Install website in Site 1:
Root directory of website: http://www.mydomain.com/demos/site1/
Location of configuration: http://www.mydomain.com/demos/site1/my_folder/configuration.php
Sub folder: http://www.mydomain.com/demos/site1/1/index.php
echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site1/
Sub folder: http://www.mydomain.com/demos/site1/1/2/index.php
Location of configuration: http://www.mydomain.com/demos/site1/my_folder/configuration.php
echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site1/
Install website in Site 2:
Root directory of website: http://www.mydomain.com/demos/site2/
Location of configuration: http://www.mydomain.com/demos/site2/my_folder/configuration.php
Sub folder: http://www.mydomain.com/demos/site2/1/index.php
echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site2/
Sub folder: http://www.mydomain.com/demos/site2/1/2/index.php
Location of configuration: http://www.mydomain.com/demos/site2/my_folder/configuration.php
echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site2/
Please note that the TLD can change. That means, installing the website on http://www.mydomain.tk, http://www.mydomai.org, http://www.mydomain.net, http://www.mydomain.co.ca etc. should not render the code useless.
WHAT I HAVE SO FAR:
I have tried to put together a script. I almost got there, but not 100%. Here it is:
function domain_url()
{
$pageURL = 'http';
if(isset($_SERVER["HTTPS"]))
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
//Remove any GET params from url
$remmove_get = strtok($pageURL, '?');
//Removes trailing slash only at the end of string. This slash is received only when accessing a page within sub folders.
$pageURL = preg_replace('{/$}', '', $remmove_get);
//Remove page name
$pageURL = dirname($pageURL);
return $pageURL;
}
define("MY_SITE_URL", domain_url().'/');
Issue with my current code:
Running the code on http://www.mydomain.com/demos/site1/ and http://www.mydomain.com/demos/site1/1/ gives MY_SITE_URL as http://www.mydomain.com/demos/site1/ and this is correct.
But when I run the code on http://www.mydomain.com/demos/site1/1/2/index.php, echoing MY_SITE_URL gives me http://www.mydomain.com/demos/site1/1/ when in fact I wanted: http://www.mydomain.com/demos/site1/
How to solve this?
If you always want the 2nd directory then you can use
If the site is ever not in the 2nd directory you can use: (this code should be a bit more concise). Just replace site1 with the name of the site
Update
This will get the url for everything before /my_folder
On a side-note, you should try the Code-Igniter framework. The framework makes this sort of thing extremely easy.