Connecting Tech Pros Worldwide Help | Site Map

Test Server to Live server - best way of handling different file paths in scripts?

Dave Smithz
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi There,

I have taken over someone else's PHP code and am quite new to PHP. I made
some changes and have implemented them to a live environment fine so far.

However, I now want to setup a test environment. All the PHP scripts start
with a few lines of:

require_once "library file at specific location on server"

and when I move them from the test location to live location I have to
manually apply the changes to each script to reflect the different paths for
each server.

I was thinking of writing a small script that is contained within the same
folder as any folder that contains scripts that actually calls another
require for that server .

E.g.
Have script do_something.php which I am developing regularly.
On live server it starts with
require_once "/home/live/phplib/library.php";
On test server
require_once "/home/test/phplib/library.php";

Currently I apply a manual change this (via global change or whatever) when
moving from Test to Live
However, I might just change all requires to:
require_once "./require.php";

And then have a server specific version of require.php that contains the
necessary requires for the particular server on which it resides.

This way I can just change all the scripts to just require the require.php
file in the same folder as itself. The actual PHP scripts where I do most of
my updates need no changes whether on live or test servers.

I just wanted to know if there was a better way of doing it then this. I
know nothing about directives in PHP or if there was a way of determining
the current server name and then calling different require commands
appropriately.

Any more elegant suggestions for handling this are welcome.

Dave


WindAndWaves
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?



"Dave Smithz" <SPAM FREE WORLD> wrote in message news:420818c5$1@news1.homechoice.co.uk...[color=blue]
> Hi There,
>
> I have taken over someone else's PHP code and am quite new to PHP. I made
> some changes and have implemented them to a live environment fine so far.
>
> However, I now want to setup a test environment. All the PHP scripts start
> with a few lines of:
>
> require_once "library file at specific location on server"
>
> and when I move them from the test location to live location I have to
> manually apply the changes to each script to reflect the different paths for
> each server.
>
> I was thinking of writing a small script that is contained within the same
> folder as any folder that contains scripts that actually calls another
> require for that server .
>
> E.g.
> Have script do_something.php which I am developing regularly.
> On live server it starts with
> require_once "/home/live/phplib/library.php";
> On test server
> require_once "/home/test/phplib/library.php";
>
> Currently I apply a manual change this (via global change or whatever) when
> moving from Test to Live
> However, I might just change all requires to:
> require_once "./require.php";
>
> And then have a server specific version of require.php that contains the
> necessary requires for the particular server on which it resides.
>
> This way I can just change all the scripts to just require the require.php
> file in the same folder as itself. The actual PHP scripts where I do most of
> my updates need no changes whether on live or test servers.
>
> I just wanted to know if there was a better way of doing it then this. I
> know nothing about directives in PHP or if there was a way of determining
> the current server name and then calling different require commands
> appropriately.
>
> Any more elegant suggestions for handling this are welcome.
>
> Dave
>
>[/color]

Hi Dave

I did what you are doing recently (i have no comp background though).

I am sorry, I do not have time to read your whole story. However, this may be of use:

$mylocation =
'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/myurl.php';

I just tested everything straight on the server (the site is not life yet) - I do not even have php installed locally at all - but
it worked great for me.

Later

- Nicolaas


Kelvin Mackay
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?


On Tue, 8 Feb 2005 01:41:27 -0000, Dave Smithz <SPAM FREE WORLD> wrote:
[color=blue]
> Hi There,
>
> I have taken over someone else's PHP code and am quite new to PHP. I made
> some changes and have implemented them to a live environment fine so far.
>
> However, I now want to setup a test environment. All the PHP scripts
> start
> with a few lines of:
>
> require_once "library file at specific location on server"
>
> and when I move them from the test location to live location I have to
> manually apply the changes to each script to reflect the different paths
> for
> each server.
>
> I was thinking of writing a small script that is contained within the
> same
> folder as any folder that contains scripts that actually calls another
> require for that server .
>
> E.g.
> Have script do_something.php which I am developing regularly.
> On live server it starts with
> require_once "/home/live/phplib/library.php";
> On test server
> require_once "/home/test/phplib/library.php";
>
> Currently I apply a manual change this (via global change or whatever)
> when
> moving from Test to Live
> However, I might just change all requires to:
> require_once "./require.php";
>
> And then have a server specific version of require.php that contains the
> necessary requires for the particular server on which it resides.
>
> This way I can just change all the scripts to just require the
> require.php
> file in the same folder as itself. The actual PHP scripts where I do
> most of
> my updates need no changes whether on live or test servers.
>
> I just wanted to know if there was a better way of doing it then this. I
> know nothing about directives in PHP or if there was a way of determining
> the current server name and then calling different require commands
> appropriately.
>
> Any more elegant suggestions for handling this are welcome.
>
> Dave
>
>[/color]


If your testing environment is linux, have you considered using symbolic
links to replicate the remote server's directory structure?
I recently did this for a client's site and found it very effective. No
more worrying about remote files using the local server's paths for one
thing. In the long term, though, this probably isn't the best option.

A more common solution, which is usually more practical, is to include a
common configuration file in all scripts that contains a 'site_root'
variable, set to an appropriate value for the server it's on, then
changing all require_once to use it instead of a hard coded path.
Example:
[config.inc.php]
$site_root = '/home/live/phplib/';

[some_file.php]
require_once $site_root . 'library.php';

This is much better because if at some stage in the future you have to
move the live site to a new server with a different directory structure,
you only have to change your configuration file.
Jerry Sievers
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?


"Dave Smithz" <SPAM FREE WORLD> writes:
[color=blue]
> Hi There,
>
> I have taken over someone else's PHP code and am quite new to PHP. I made
> some changes and have implemented them to a live environment fine so far.
>
> However, I now want to setup a test environment. All the PHP scripts start
> with a few lines of:[/color]

Better to use

php_value include_path ".:/home/someusername/www/phplib" (example)

Maybe this is of no help for an existing, not very well thought out
system of scripts though since you'd have to edit each one of them to
change the existing hard coded path names.

I pretty much NEVER use absolute paths in require/include statements
but rather pull from one or more defined include dirs.

Doing so and we can move an entire code base by changing only one line
the top level .htaccess file in DOC_ROOT.

HTH

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Dave Smithz
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?


"Jerry Sievers" <jerry@jerrysievers.com> wrote in message
news:m3fz07ks9d.fsf@prod01.jerrysievers.com...[color=blue]
>
> Better to use
>
> php_value include_path ".:/home/someusername/www/phplib" (example)
>
> Maybe this is of no help for an existing, not very well thought out
> system of scripts though since you'd have to edit each one of them to
> change the existing hard coded path names.
>
> I pretty much NEVER use absolute paths in require/include statements
> but rather pull from one or more defined include dirs.
>
> Doing so and we can move an entire code base by changing only one line
> the top level .htaccess file in DOC_ROOT.
>
> HTH[/color]

Thanks for all the responses. Your described benefits of this method appeal
but can you explain a little more. Are php_vale and include_path specific
php commands.
Isn't what you have shown very similar to what I said about having all my
current files do a require_once of a file in the same folder?

Do remember that generally the code is on shared hosting packages so I do
not have full control of the webserver.

Kind regards,

Dave


Jerry Sievers
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?


"Dave Smithz" <SPAM FREE WORLD> writes:
[color=blue]
> "Jerry Sievers" <jerry@jerrysievers.com> wrote in message
> news:m3fz07ks9d.fsf@prod01.jerrysievers.com...[color=green]
> >
> > Better to use
> >
> > php_value include_path ".:/home/someusername/www/phplib" (example)
> >
> > Maybe this is of no help for an existing, not very well thought out
> > system of scripts though since you'd have to edit each one of them to
> > change the existing hard coded path names.
> >
> > I pretty much NEVER use absolute paths in require/include statements
> > but rather pull from one or more defined include dirs.
> >
> > Doing so and we can move an entire code base by changing only one line
> > the top level .htaccess file in DOC_ROOT.
> >
> > HTH[/color]
>
> Thanks for all the responses. Your described benefits of this method appeal
> but can you explain a little more. Are php_vale and include_path specific
> php commands.
> Isn't what you have shown very similar to what I said about having all my
> current files do a require_once of a file in the same folder?[/color]

Pretty much the same result but I think the php config setting in
..htaccess even easier. That's up to you.

A single point administration for this include path value is all we
need to achieve and there is more than one way to do it.
[color=blue]
>
> Do remember that generally the code is on shared hosting packages so I do
> not have full control of the webserver.[/color]

Right. The php_value setting goes in .htaccess and unless the web
server is configured to not even allow a virtual host administrator to
change PHP settings, it should work.

Test it a little is about all I can suggest.
[color=blue]
>
> Kind regards,
>
> Dave
>
>[/color]

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Belmin
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?


The other replied were expired, how did you accomplish this? Curious

Written by "Dave Smithz" on 2/7/05 8:41p:[color=blue]
> Hi There,
>
> I have taken over someone else's PHP code and am quite new to PHP. I made
> some changes and have implemented them to a live environment fine so far.
>
> However, I now want to setup a test environment. All the PHP scripts start
> with a few lines of:
>
> require_once "library file at specific location on server"
>
> and when I move them from the test location to live location I have to
> manually apply the changes to each script to reflect the different paths for
> each server.
>
> I was thinking of writing a small script that is contained within the same
> folder as any folder that contains scripts that actually calls another
> require for that server .
>
> E.g.
> Have script do_something.php which I am developing regularly.
> On live server it starts with
> require_once "/home/live/phplib/library.php";
> On test server
> require_once "/home/test/phplib/library.php";
>
> Currently I apply a manual change this (via global change or whatever) when
> moving from Test to Live
> However, I might just change all requires to:
> require_once "./require.php";
>
> And then have a server specific version of require.php that contains the
> necessary requires for the particular server on which it resides.
>
> This way I can just change all the scripts to just require the require.php
> file in the same folder as itself. The actual PHP scripts where I do most of
> my updates need no changes whether on live or test servers.
>
> I just wanted to know if there was a better way of doing it then this. I
> know nothing about directives in PHP or if there was a way of determining
> the current server name and then calling different require commands
> appropriately.
>
> Any more elegant suggestions for handling this are welcome.
>
> Dave
>
>[/color]

--

Belmin Fernandez

Visit: http://www.belminfernandez.com/homepage
Email: belminf at gmail period com
Dave Smithz
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Test Server to Live server - best way of handling different file paths in scripts?



"Belmin" <anonymous@nospam.com> wrote in message
news:1HCPd.7154$qn2.1266626@twister.nyc.rr.com...[color=blue]
> The other replied were expired, how did you accomplish this? Curious[/color]

What do you mean by this? There were quite a few suggestions.


Closed Thread