Jump to content
Larry Ullman's Book Forums

Base_Uri Setting For Production Site


Recommended Posts

Hi,

 

On page 49 instructions are given for the setting up of the BASE_URI constant for both a development environment and a production environment.

 

In my production site, I have access to a folder named httpdocs within which is the opening index.php script.  To get to this httpdocs folder I use the site IP address or to start the website I use the www.... site URL.

 

Can you please advise exactly what I need to put in the production BASE_URI constant?

 

Many thanks in anticipation.

Link to comment
Share on other sites

The base URI constantly has caused a lot of people confusion on these boards, but it basically equates to the directory path on the server your site is hosted on that leads to the root of where your files reside.

More than likely, the server your hosting company is using is a UNIX box, which means that the base URI will be something like the following:

/home/users/your-user-name/your-domain-name/

 

Of course, the base URI can greatly differ from hosting company to hosting company, but that information should be available somewhere in your control panel. If you can't find it, you can always just contact your hosting company as well.

 

Also, what Larry calls the "base URI", I would usually refer to as the "site root", "document root", "server base directory" or "home directory", etc. Your hosting company may use any one of those terms (or another one altogether).

 

More important than the term that's used though is the fact that the base URI is used to refer to any resource on your web server via a file path local to your server. This is very advantageous, as it gives you the ability to refer to resources absolutely (as opposed to relatively), as well as refer to files that live outside your web root (e.g., the public_html directory) and cannot be referenced via a web URL.

 

That make sense?

Link to comment
Share on other sites

Hi Hartley-san, Thanks for this.

 

I checked and my hosting company uses IIS. I was able to get the full path info via $_SERVER['DOCUMENT_ROOT']. I first echo'd out the whole $_SERVER array and from that I saw that the DOCUMENT_ROOT array element had the full path.

 

Which begs the question, when setting the constant, should it/could it just be set from this $_SERVER array value? Thus if the hosting company changed this for any reason, the value of the constant would automatically be updated?

 

Cheers from Oz.

  • Upvote 1
Link to comment
Share on other sites

$_SERVER['DOCUMENT_ROOT'] certainly is a way to figure out your "base URI" value, but keep in mind that that value will contain the whole path to the file you're executing. For example, if you have a script in the public_html directory, then that will be listed as part of the document root.

 

All the same, $_SERVER['DOCUMENT_ROOT'] is a valid way to figure out the base URI value on any server.

Good find!

Link to comment
Share on other sites

As an experiment, I tried to see if I could generate the values for the constants and somewhat to my surprise I found that I could. Here's an example:

 

$base_URI = $_SERVER['DOCUMENT_ROOT'];
$script_filename = $_SERVER['SCRIPT_NAME'];
// find the last slash and then truncate the string at that point
$slash_location = strrpos($script_filename, '/');	// finds the last slash
$script_filename = substr($script_filename, 0, $slash_location);	// chop off the script name
$base_URI .= $script_filename . '/';
define('BASE_URI', $base_URI);
As I was not able to find any examples of setting a constant in this way on the Internet, I was quite wary, but it seems to work OK.

 

Any traps to be aware of?

 

Note1: I tested the value of the constant with

echo '<p>URI is ' . constant("BASE_URI") . '</p>';
Note2: the process would be a little different in the production environment as some of the $_SERVER elements are different in the hosted production environment, though there are array values from which the constant values could be generated and a number of them are the same array key names.

 

Thanks for your interest and Cheers from Oz.

Link to comment
Share on other sites

Well, two things come to mind:

 

1) The DOCUMENT_ROOT value will change based on where the script being executed is located. Unless the script being executed is truly in your base directory for your server account, you're not going to get an accurate value.

 

2) I don't see the benefit of calculating this value dynamically. To me, this is why Larry uses a config file in the first place. Specifically, the concept of a config file is to store all environment-specific info in one location so that when you change environments (e.g., go from a dev environment to a production environment), you simply change the strings in the config file, and boom!, everything suddenly works. In fact, my config file usually looks like the following during development:

define('SITE_ROOT', 'site-root-in-dev-environment');
//define('SITE_ROOT', 'site-root-in-production-environment');

// Other constants here.

As you can see, I enter static values for the same constant for both environments, and when I change environments, all I have to do is port over the DB, and then comment out the dev settings and uncomment the production settings.

In other words, editing the config files takes all of about 15 seconds.

 

I'm not saying you have to use static values, and you can't calculate everything dynamically, but I just don't see the benefit, really.

 

Also, you might want to look into the basename, dirname and pathinfo functions in PHP. They'll make your code a lot simpler.

Link to comment
Share on other sites

Hi Hartley-san,  Thanks for your feedback.

 

In Larry's example (page 49) he tests which environment the script is executing in and sets the constants accordingly.  That is what I was fiddling with - to generate the correct constant values in either case.

 

Yes, maybe my 'fiddling' is a bit unnecessary but I was just experimenting to see if I could get the constants set dynamically:-)

 

I will also check out the PHP functions you recommended - thanks.

 

Cheers from Oz.

Link to comment
Share on other sites

More 'fiddling' yielded the following for the URI:

 

 

$base_URI = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME']) . '/';
define('BASE_URI', $base_URI);

 

I think that I have 'fiddled' enough now :-)

 

Note that this is only for my XAMPP localhost environment.

 

Cheers

Link to comment
Share on other sites

Even though you can set the constants from $_SERVER data, Hartley-san and Larry are right in that it is better to hard-code the values because if one of your scripts is surreptitiously invoked, and that script is not in the site's highest-level directory, the automatic setting of the constant values will give you incorrect results. 

 

So do it as the book says!

 

Cheers from Oz.

Link to comment
Share on other sites

The other benefit of hardcoding the values is performance: not having to dynamically calculate all this saves time. On the other hand, dynamically calculating all this (correctly) means it'll always work, without thinking, even if you move the site. Always a trade off...

Link to comment
Share on other sites

 Share

×
×
  • Create New...