Jump to content

Getting the base url of the installation


marcbraulio

Recommended Posts

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.

Link to comment
Share on other sites

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;
  
?>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.