PHP Dynamically get the full Absolute URL for a specific file to be included in other files

I have a configuration file. This is not a separate file. I will include this file at the top of the pages that I want to use using require (). I want to dynamically get the full absolute URL of this configuration file , regardless of its location, and save it as a constant inside myself. For instance:

Physical location: (root directory) /my_folder/configuration.php

Required URL like: www.mydomain.com/my_folder/configuration.php

Physical location: (root directory) /demos/my_folder/configuration.php

Required URL like: www.mydomain.com/demos/my_folder/configuration.php

Physical location: (root directory) /demos/site1/my_folder/configuration.php

Required URL like: www.mydomain.com/demos/site1/my_folder/configuration.php

Physical location: (root directory) /demos/site2/my_folder/configuration.php

Required URL like: www.mydomain.com/demos/site2/my_folder/configuration.php

Simple so far? This is what is really necessary and complicated (IMO). Consider this:

Configuration file located at: www.mydomain.com/demos/site2/my_folder/configuration.php

They have subfolders: www.mydomain.com/demos/site2/1/2/3/index.php

When I access the index.php file in a subfolder of "3", following the above URL, I need the path to the configuration file as www.mydomain.com/demos/site2/my_folder/configuration. php , not like www.mydomain.com/demos/site2/1/2/3/my_folder/configuration.php

How can I achieve the above?

+4
source share
2 answers

If you can rely on the value of $_SERVER[ 'DOCUMENT_ROOT' ] , then inside configuration.php :

 $path = substr( __FILE__, strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) ); $url = "www.mydomain.com{$path}"; 

If this matches your use case, you can make it more dynamic with $_SERVER[ 'HTTP_HOST' ] ;

DOCUMENT_ROOT

I used DOCUMENT_ROOT liberally in my development, as it is often the only dynamic variable available for building certain paths with self-reflection. It missed the Apache run ( # 26052 ) about how DOCUMENT_ROOT handled poorly, in particular that Apache would not allow you to set a value with a RewriteRule and did not set it to a reasonable value when using mod_vhost_alias. The discussion continues for 7-8 years, as people, presumably from the Apache project, resist behavior change until they finally come and change this year in 2.4.1. (I studied this earlier, but now I forgot what the exact changes were and how satisfying they were.)

If you look at the comments on the ticket, you will see that people resist behavior changes with comments like:

Do not trust the DOCUMENT_ROOT variable.

DOCUMENT_ROOT is not, has never been and never will be a reliable way to find the path to the web content of the file system.

Therefore, I suggest reading the comments on this ticket to find out what they say that they use it. I used it with great success and I don’t know how best to achieve the same things in the same situations that DOCUMENT_ROOT is available and provides the necessary data.

+5
source

What I used, which worked perfectly for what I wanted, was as follows:

 define('URL', dirname(substr($_SERVER['SCRIPT_FILENAME'], strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) )).'/'); 
+2
source

All Articles