I have a site that I am working on, and I got tired of having to change the root path every time I uploaded the script from the local to on-line server.

I tested the following code on both local (XAMPP under Windows) and web (shared Linux hosted on GoDaddy) server, and both returned the proper path.

<?php

echo $_SERVER['DOCUMENT_ROOT'];

?

So, I added a way for the script to detect and automatically use the appropriate server path.

This required changing 3 include files:
In myapp.inc:

<?php
require_once 'db.inc';
require_once 'customHandler.inc';

// Switch between local & server
// - local - //define("D_INSTALL_PATH", "c:/xampp/htdocs");
// - server - //define("D_INSTALL_PATH", "/home/content/o/r/c/myapp/html");
define("D_INSTALL_PATH", $_SERVER['DOCUMENT_ROOT']);

In template.inc:

<?php

// ITX template class extensions for the myapp
// -- myappTemplate is a generic page
// -- myappFormTemplate is a <form> page (and extends myappTemplate)

require_once "MDB2.php";
// Switch between local & server
if (D_INSTALL_PATH == "c:/xampp/htdocs")
	require_once "C:/xampp/htdocs/PEAR/HTML/Template/ITX.php";
else
	require_once "HTML/Template/ITX.php";

In db.inc:

<?php
	// Database parameters
	// Switch between local & server
	if (D_INSTALL_PATH == "c:/xampp/htdocs")
	{
		$hostname = "localhost";
		$username = "********";
	}
	else
	{
		$hostname = "myapp.db.5158902.hostedresource.com";
		$username = "********";
	}

****************************************

When I run this on the on-line server, it runs quite well - with little lag time between pages.

But, when I run this on the local server, it spins it's wheels for over a minute before giving me the requested page - or, on the one page where I request an MySQL query, I get the following errors...

Notice: Use of undefined constant D_INSTALL_PATH - assumed 'D_INSTALL_PATH' in C:\xampp\htdocs\myapp\includes\template.inc on line 9

Notice: Use of undefined constant D_INSTALL_PATH - assumed 'D_INSTALL_PATH' in C:\xampp\htdocs\myapp\includes\db.inc on line 4

So what am I missing?

It seems that you are calling the file with your mysql stuff in it before you actually define your install path variable. I would suggest that the very first thing you do in your main script is define the install path variable before you do anything else. This should solve any errors with the mysql portion of your script. Hopefully, it will also fix the delayed responses as well.

Member Avatar for diafol

Have you thought of accessing the hosts and v-hosts files on Windows?

HOSTS file:
C:\Windows\System32\drivers\etc\hosts

You should see something like:

# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
::1             localhost

Just add the following to the file:

# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
::1             localhost

[B]127.0.0.1 		mysite.local[/B]

httpd-vhosts.conf FILE:
C:\xampp\apache\conf\extra\httpd-vhosts.conf

You should see:

##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
##    DocumentRoot /www/docs/dummy-host2.example.com
##    ServerName dummy-host2.example.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
##</VirtualHost>

NameVirtualHost 127.0.0.1

Just add:

<VirtualHost 127.0.0.1>
   DocumentRoot C:\xampp\htdocs\mysite
   ServerName mysite.local
</VirtualHost>

One this is done, stop and restart Apache.

Your local version will now have the following type of address:

http://mysite.local/index.php

This should ensure that your relative and absolute public files are in the same place on local server and remote server.
PEAR should be taken care of by the include path within php.ini. So that this shouldn't have to be messed with as a rule.

The mysql DB is a different matter, you could check the url for a flag like ".local" in the name to decide on the local version / remote version.

commented: helpful! +6

Yes, I see that...and it should be fixable by moving the line in myapp.inc:

require_once 'db.inc';

to below the line:

define("D_INSTALL_PATH", $_SERVER['DOCUMENT_ROOT']);

So that I have:

<?php
require_once 'customHandler.inc';

// Switch between local & server
define("D_INSTALL_PATH", $_SERVER['DOCUMENT_ROOT']);
require_once 'db.inc';

And then adding the line to the db.inc code::

require_once "myapp.inc";

So that I link to D_INSTALL_PATH:

<?php
	require_once "myapp.inc";
	// Database parameters
	// Switch between local & server
	if (D_INSTALL_PATH == "c:/xampp/htdocs")
	{
		$hostname = "localhost";
		$username = "root";
	}
	else
	{
		$hostname = myapp.db.5158902.hostedresource.com";
		$username = "********";
	}

But, it still jams...however, if I add the following:

$hostname = "localhost";
	$username = "******";

below the if statement - thereby disregarding the result of the if statement, I have

<?php
	require_once "myapp.inc";
	// Database parameters
	// Switch between local & server

	if (D_INSTALL_PATH == "c:/xampp/htdocs")
	{
		$hostname = "localhost";
		$username = "*******";
	}
	else
	{
		$hostname = "myapp.db.5158902.hostedresource.com";
		$username = "*******";
	}
	
	$hostname = "localhost";
	$username = "*******";

it works just fine.

So, apparently the value of D_INSTALL_PATH does not transfer to the db.inc code...

For now, I can deal with that - there is only the one place to remember to swap out the local/server code...two lines, not so bad.

Would be nice to know why it isn't working though...

Thanks for the help.

Have you thought of accessing the hosts and v-hosts files on Windows?

OK, not sure what you mean by that - I should mention that I am new to php, so some of this is a bit confusing at this point.

I am using Windows on the local machine - but the web server is Linux, so not sure how that applies.

HOSTS file:
C:\Windows\System32\drivers\etc\hosts

You should see something like:

# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
::1             localhost

Just add the following to the file:

# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
::1             localhost

[B]127.0.0.1 		mysite.local[/B]

httpd-vhosts.conf FILE:
C:\xampp\apache\conf\extra\httpd-vhosts.conf

You should see:

##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
##    DocumentRoot /www/docs/dummy-host2.example.com
##    ServerName dummy-host2.example.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
##</VirtualHost>

NameVirtualHost 127.0.0.1

Just add:

<VirtualHost 127.0.0.1>
   DocumentRoot C:\xampp\htdocs\mysite
   ServerName mysite.local
</VirtualHost>

One this is done, stop and restart Apache.

Your local version will now have the following type of address:

http://mysite.local/index.php

This should ensure that your relative and absolute public files are in the same place on local server and remote server.
PEAR should be taken care of by the include path within php.ini. So that this shouldn't have to be messed with as a rule.

The mysql DB is a different matter, you could check the url for a flag like ".local" in the name to decide on the local version / remote version.

OK, the folder for the code is the same on both the local and web server (i.e: root/myapp/....) - is that "under" or "above" the root? I've seen both mentioned and am not sure which is correct. :confused:). And I can get the absolute root from running MYphpinfo.php - which seems to work when used with the appropriate root. Finding the root isn't the problem - what I need is a way to have the code reach out and determine which one I am using, and switch to the proper one for the machine I am running on. I figured it would save time as I switch from local to web server, and avoid forgetting to change everything that needed to change. At least it seemed like an easy task when I started. As I said in the earlier response, I can deal with having to change 2 lines in 1 file...I need to concentrate on getting this project up & running.

Thanks for the help and suggestions!

One question occurs to me - are you using "127.0.0.1" as a catch-all URL, that will default to the machine it's run on?

Member Avatar for diafol

127.0.0.1 is the same as 'localhost'

The fact that you're using Windows as Apache local server and Linux as Apache remote server makes absolutely no difference. It's the same as my setup.

My suggestion should ensure that you only have to worry about the mysql DB. Everything else should be automatic - no need to mess with roots in your code.

When trying to access files above public root, well yes that will require some messing, unless you use relative references.

You can use $_SERVER (as you have done) to circumvent problems when trying to access functions such as file_exists() etc, when you need to provide the full path (webserver).

As for you "server-finding" question:

if(strpos($_SERVER['DOCUMENT_ROOT'],"mysite.local") === false)){
   //on remote server - apply your remote mysql details
}else{
  //on local server - apply your local mysql details
}

This should be one of the first bits of code run by any page requiring DB access.
I would suggest that you place this in a file such as config.php and include it at the top of every page. That way, if you decide to change the local name for the site, you don't have to change loads of pages. In addition, if you move host, you only need to change the mysql connection details once.

OK, that makes sense! Thanks for clearing that up for me - as I said, I'm new to php so some times it takes a bigger hammer than normal to get me to look in the right direction...

I'll make the changes later today when I get to work on the project.

Again, thanks for all the responses!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.