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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tuttle
Forum Newbie
Posts: 3
Joined: Sun May 13, 2007 12:30 pm
Location: Canada

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

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

.
rmouali
Forum Newbie
Posts: 3
Joined: Sat May 12, 2007 5:51 pm

Post 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__
tuttle
Forum Newbie
Posts: 3
Joined: Sun May 13, 2007 12:30 pm
Location: Canada

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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

Post 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.
tuttle
Forum Newbie
Posts: 3
Joined: Sun May 13, 2007 12:30 pm
Location: Canada

Post 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.
Post Reply