PHP Get siteurl dynamically
<p>I am trying to get the siteurl <strong>without hardcoding</strong> 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.</p>

<p><strong>Example:</strong></p>

<p>Install website on www.mydomain.com</p>

<p>Configuration file located in www.mydomain.com/my_folder/configuration.php</p>

<p>Want to declare a constant in configuration.php file so that it always gets the installed path dynamically without having to hardcode it. So <code>define("MY_SITE_URL", ???? )</code></p>

<p>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?</p>

<p><strong>Examples: All the pages will contain the same configuration.php at the beginning of the file as a require</strong> </p>

<p><em><strong>Install website in Site 1:</em></strong></p>

<p>Root directory of website: <strong>www.mydomain.com/demos/site1/</strong> </p>

<p>Location of configuration: <strong>www.mydomain.com/demos/site1/my_folder/configuration.php</strong> </p>

<p>Sub folder: <strong>www.mydomain.com/demos/site1/1/index.php</strong> </p>

<p>echoing MY_SITE_URL in index.php should give www.mydomain.com/demos/site1/</p>

<p>Sub folder: <strong>www.mydomain.com/demos/site1/1/2/index.php</strong> </p>

<p>Location of configuration: <strong>www.mydomain.com/demos/site1/my_folder/configuration.php</strong> </p>

<p>echoing MY_SITE_URL in index.php should give www.mydomain.com/demos/site1/</p>

<p><em><strong>Install website in Site 2:</em></strong></p>

<p>Root directory of website: <strong>www.mydomain.com/demos/site2/</strong> </p>

<p>Location of configuration: <strong>www.mydomain.com/demos/site2/my_folder/configuration.php</strong> </p>

<p>Sub folder: <strong>www.mydomain.com/demos/site2/1/index.php</strong> </p>

<p>echoing MY_SITE_URL in index.php should give www.mydomain.com/demos/site2/</p>

<p>Sub folder: <strong>www.mydomain.com/demos/site2/1/2/index.php</strong> </p>

<p>Location of configuration: <strong>www.mydomain.com/demos/site2/my_folder/configuration.php</strong> </p>

<p>echoing MY_SITE_URL in index.php should give www.mydomain.com/demos/site2/</p>

<p>Please note that the TLD can change. That means, installing the website on www.mydomain.tk, www.mydomai.org, www.mydomain.net, www.mydomain.co.ca etc. should not render the code useless.</p>

<p><strong>WHAT I HAVE SO FAR:</strong></p>

<p>I have tried to put together a script. I almost got there, but not 100%. Here it is:</p>

<pre><code>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().'/'); 
</code></pre>

<p><strong>Issue with my current code:</strong></p>

<p>Running the code on www.mydomain.com/demos/site1/ and www.mydomain.com/demos/site1/1/ gives MY_SITE_URL as www.mydomain.com/demos/site1/ and this is correct.</p>

<p>But when I run the code on www.mydomain.com/demos/site1/1/2/index.php, echoing MY_SITE_URL gives me <strong>www.mydomain.com/demos/site1/1/</strong> when in fact I wanted: <strong>www.mydomain.com/demos/site1/</strong></p>

<p>How to solve this?</p>
