marcbraulio Posted March 1, 2012 Share Posted March 1, 2012 Hello, I am attempting to get the base URL of the directory that the CMS was installed, not the main directory. Here's what I mean: For example, let's say my domain is http://www.example.com but I install my CMS under http://www.example.com/test/cms and on another occasion I install it under http://www.example.com/cms, and on another occasion http://www.example.com/my/new/cms/ I would like my base url for the http://www.example.com/cms installation, to be "http://www.example.com/cms" and my base url for the http://www.example.com/test/cms installation to be "http://www.example.com/test/cms" and so on, without me having to manually change anything. How do I go about obtaining the directory that corresponds to that particular installation? I know it is possible because Joomla does it automatically. I have already tried numerous combinations using $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'], and so on, no luck. Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/ Share on other sites More sharing options...
PaulRyan Posted March 1, 2012 Share Posted March 1, 2012 Try this out. <?PHP $http = (!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'; $url = $http.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; $url = pathinfo($url); $url = $url['dirname']; echo $url; ?> Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/#findComment-1322558 Share on other sites More sharing options...
marcbraulio Posted March 1, 2012 Author Share Posted March 1, 2012 Try this out. <?PHP $http = (!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'; $url = $http.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; $url = pathinfo($url); $url = $url['dirname']; echo $url; ?> Thank you for the quick reply. It is very close, but not quite there yet. Here are the results: http://www.example.com/CMS/ will output: http://www.example.com http://www.example.com/CMS/CMS-2 will output: http://www.example.com/CMS http://www.example.com/CMS/CMS-2/CMS-3 will output: http://www.example.com/CMS-2 Seems like it is a directory behind. Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/#findComment-1322562 Share on other sites More sharing options...
creata.physics Posted March 1, 2012 Share Posted March 1, 2012 Where is the code in each directory located? I've made several folders: /home/test /home/test/test2 /home/test/test3 I've put the code provided by PaulRyan in an index file in each directory and I get all correct readings. Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/#findComment-1322660 Share on other sites More sharing options...
marcbraulio Posted March 1, 2012 Author Share Posted March 1, 2012 Where is the code in each directory located? I've made several folders: /home/test /home/test/test2 /home/test/test3 I've put the code provided by PaulRyan in an index file in each directory and I get all correct readings. The code for each directory is located in its main directory, instance: /home/test is a complete CMS by it self. /home/test/test2 is another complete CMS by it self and /home/test/test3 is another complete CMS by it self. The problem I am having is that the CMS installed in "/home/test/test2" and "/home/test/test3" will output the base url of "/home/test/" instead of their real base directory which are "/home/test/test2" and "/home/test/test3". Same for the CMS installed in "/home/test" which will output "/home/" for its base url. When you tested the code, did you get /home/test -> /home/test /home/test/test2 -> /home/test/test2 /home/test/test3 -> /home/test/test3 for each one? Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/#findComment-1322805 Share on other sites More sharing options...
kicken Posted March 1, 2012 Share Posted March 1, 2012 This code should get you a fairly accurate guess: <?php $basedir = substr($_SERVER['REQUEST_URI'], -1)=='/'?$_SERVER['REQUEST_URI']:dirname($_SERVER['REQUEST_URI']); $http = (!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'; $url = $http.$_SERVER['SERVER_NAME'].$basedir; echo $url; Though you should have some way, perhaps during the installation process, for the user to manually enter this information. You can use the guessing code to pre-fill the box to try and make things easier for them. Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/#findComment-1322836 Share on other sites More sharing options...
marcbraulio Posted March 1, 2012 Author Share Posted March 1, 2012 This code should get you a fairly accurate guess: <?php $basedir = substr($_SERVER['REQUEST_URI'], -1)=='/'?$_SERVER['REQUEST_URI']:dirname($_SERVER['REQUEST_URI']); $http = (!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'; $url = $http.$_SERVER['SERVER_NAME'].$basedir; echo $url; Though you should have some way, perhaps during the installation process, for the user to manually enter this information. You can use the guessing code to pre-fill the box to try and make things easier for them. Worked perfectly, thank you! Now that I think of it, another alternative would be to just get the path of the main index.php file, which will always reside in the main directory of the installation. Out of curiosity what is the best method of getting the path of a particular file? Also, if it is not too much of a hassle, could you explain what exatly is going on in this line: $basedir = substr($_SERVER['REQUEST_URI'], -1)=='/'?$_SERVER['REQUEST_URI']:dirname($_SERVER['REQUEST_URI']); Quote Link to comment https://forums.phpfreaks.com/topic/258016-getting-the-base-url-of-the-installation/#findComment-1322900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.