Page 1 of 1

$base_url works in sub-directory, not in domain root

Posted: Sun May 13, 2007 12:55 pm
by tuttle
How do I specify the domain root (the main public_html directory, not a sub-directory)?

Someone helped me with a PHP script. In part of the script, he wrote:

Code: Select all

$base_url = '/my_sub-directory/';

$replace_me = str_replace($base_url, '', $_SERVER['PHP_SELF']);
In $base_url I am supposed to enter the path after the domain root (the part after domain.com) where the site is installed. As long as it is installed in a sub-directory and I specify it, it works properly. e.g., if I install the site in domain.com/my_sub-directory/ then it works.

But, ultimately I want to put the site in the domain root, so the home page will be in the public_html directory, not a sub-directory. I don't know what to specify for $base_url in that case, because nothing I tried works.

Code: Select all

$base_url = '/';

$replace_me = str_replace($base_url, '', $_SERVER['PHP_SELF']);
For $base_url when in the domain root, I tried:

$base_url = '/';
$base_url = '';
$base_url = ' ';
$base_url = '~';

none of those worked.
What do I enter in $base_url when the site is installed at the domain root immediately in the public_html directory?

.

Posted: Sun May 13, 2007 3:03 pm
by rmouali
If your php script (test.php) is under the public_html directory:
$_SERVER['PHP_INFO'] will have "/test.php" as value
your $base_url, in this context, is "/"
$replace_me will have "test.php" as value
If you are just intersted by extracting php file name then use : __FILE__

Posted: Sun May 13, 2007 3:25 pm
by tuttle
As I wrote, the code fragment is:

Code: Select all

$base_url = '/'; 

$replace_me = str_replace($base_url, '', $_SERVER['PHP_SELF']);

so it's 'PHP_SELF' not 'PHP_INFO'.
And I tried entering $base_url = '/'; which does not work.

No, it doesn't just check the filename. It must match the filename and path/url.

Re: $base_url works in sub-directory, not in domain root

Posted: Sun May 13, 2007 3:39 pm
by volka
tuttle wrote:How do I specify the domain root (the main public_html directory, not a sub-directory)?
You might be interested in
http://www.php.net/manual/en/reserved.variables.php wrote:'DOCUMENT_ROOT'
The document root directory under which the current script is executing, as defined in the server's configuration file.

Posted: Sun May 13, 2007 3:54 pm
by tuttle
Thanks for the reply. Sorry, I'm a complete beginner so I'm not sure what you're suggesting that I change.

The code I currently have, $base_url = 'x'; works perfectly when x is a sub-directory path. e.g. If the site is installed in domain.com/2007/mysite/ then I specify

Code: Select all

$base_url = '/2007/mysite/';
and everything works. I just don't know what to specify when the site is installed directly under domain.com and not in a sub-directory. Since everything else works, I hoped I could just change what I set for $base_url when installed in the domain root. I just don't know what to enter, as '/'; ''; and ' '; don't work.