By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,358 Members | 2,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,358 IT Pros & Developers. It's quick & easy.

use virtual hosts to develop locally but upload throws error in source file references

P: n/a
On my development computer, I have virtual named host set up, like
www.site1.lab.

When I upload those to my web site for customer review under
mywebsite.com/clients/site1/
it throws some of the source file references off and it does not work
properly.

My references are like:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");

Obviously those will not work since $_SERVER['DOCUMENT_ROOT'] references
mywebsite.com.

Is there anything I can do in apache.conf for a virtual directory that would
make it so when I upload a site for customer review, I don't have to change
the source references?
Jan 19 '07 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Rik
Paul wrote:
On my development computer, I have virtual named host set up, like
www.site1.lab.

When I upload those to my web site for customer review under
mywebsite.com/clients/site1/
it throws some of the source file references off and it does not work
properly.

My references are like:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");

Obviously those will not work since $_SERVER['DOCUMENT_ROOT']
references mywebsite.com.

Is there anything I can do in apache.conf for a virtual directory
that would make it so when I upload a site for customer review, I
don't have to change the source references?
As long as you've really got a virtualhost set, and set the DocmentRoot for
that domain explicitly, is should have the right $_SERVER['DOCUMENT_ROOT'].

Relevant settings:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/path/to/test/docroot"
ServerName test.localhost
</VirtualHost>

server.php:
<?php
echo $_SERVER['DOCUMENT_ROOT'];
?>
Which correctly results in : /path/to/test/docroot
--
Rik Wasmus
Jan 19 '07 #2

P: n/a
Paul wrote:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");
That's not a great way of referencing included files. It makes it
difficult to move your site around.

Try instead creating a directory called "includes", and putting all your
included files into there, like:

includes/search_functions.php
includes/database_functions.php
includes/utility/top.php
includes/utility/bottom.php
includes/utility/left.php
includes/utility/right.php

Now, when you need to refer to them, use:

require_once 'utility/top.php';

Note here that we've not specified the full path for the file, not even a
normal relative path -- we've specified where it is RELATIVE TO THE
"includes" DIRECTORY, not relative to where we are now.

Now in your .htaccess file, you can set:

php_value includes_path /full/path/to/includes/

So when PHP sees a 'include' or 'require' statement, it will search
through the path(s) in the includes_path setting and look for
/full/path/to/includes/utility/top.php.

Yay

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 19 '07 #3

P: n/a
Toby Inkster wrote:
Paul wrote:
>require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");

That's not a great way of referencing included files. It makes it
difficult to move your site around.

Try instead creating a directory called "includes", and putting all your
included files into there, like:

includes/search_functions.php
includes/database_functions.php
includes/utility/top.php
includes/utility/bottom.php
includes/utility/left.php
includes/utility/right.php

Now, when you need to refer to them, use:

require_once 'utility/top.php';

Note here that we've not specified the full path for the file, not even a
normal relative path -- we've specified where it is RELATIVE TO THE
"includes" DIRECTORY, not relative to where we are now.

Now in your .htaccess file, you can set:

php_value includes_path /full/path/to/includes/

So when PHP sees a 'include' or 'require' statement, it will search
through the path(s) in the includes_path setting and look for
/full/path/to/includes/utility/top.php.

Yay

Actually, that's the BEST WAY to reference your files.
$_SERVER['DOCUMENT_ROOT'] will ALWAYS contain the absolute path to the
root directory of your site, no matter where it is. You need to make no
changes to any files when referencing this way.

Then you don't need any entry in your .htaccess file (which even Apache
recommends against using), you don't have to worry about setting it up
or anything else. Everything works automatically.

I use this all the time in my sites. I can move them between different
test machines (both local and on the internet), the final site - whatever.

I've even moved complete sites from one provider to another. All that
was necessary was to upload and go.

And BTW - it works in IIS also, which doesn't have a .htaccess file.
Works great, in fact.

Use the server-provided values where you can. That's what they're there
for.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 20 '07 #4

P: n/a
Jerry Stuckle wrote:
$_SERVER['DOCUMENT_ROOT'] will ALWAYS contain the absolute path to the
root directory of your site, no matter where it is. You need to make no
changes to any files when referencing this way.
Makes it tricky to move your PHP around *within* your document root
though. I try to make my code run equally well rooted in "/somedir/" as it
does in "/".

Putting everything in an 'includes' directory and then specifying the
location of the files in include_path allows you to easily move the
includes anywhere (including outside the document root altogether, which
is often desirable from a security POV) without making any changes to your
scripts at all.
Then you don't need any entry in your .htaccess file (which even Apache
recommends against using), you don't have to worry about setting it up
or anything else. Everything works automatically.
Better to use "httpd.conf" or "php.ini" for such settings, but not
everyone is able to. I tend to use ".htaccess" for testing and then
migrate to "httpd.conf" once I'm sure.
And BTW - it works in IIS also, which doesn't have a .htaccess file.
Works great, in fact.
As does "include_path" -- you just need to set it in php.ini.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 20 '07 #5

P: n/a
Toby Inkster wrote:
Jerry Stuckle wrote:
>$_SERVER['DOCUMENT_ROOT'] will ALWAYS contain the absolute path to the
root directory of your site, no matter where it is. You need to make no
changes to any files when referencing this way.

Makes it tricky to move your PHP around *within* your document root
though. I try to make my code run equally well rooted in "/somedir/" as it
does in "/".
Sure. But I almost never do that. And if I do, I can quickly do a
search/replace for the file path. On a 3K+ file site it takes less than
a minute to replace all of the references.
Putting everything in an 'includes' directory and then specifying the
location of the files in include_path allows you to easily move the
includes anywhere (including outside the document root altogether, which
is often desirable from a security POV) without making any changes to your
scripts at all.
Sure - if you're on Apache, if you have .htaccess, if your host allows
PHP changes to .htaccess...

And files can be outside of the document root with this means, also - i.e.

$_SERVER['DOCUMENT_ROOT'] . '/../passwords.txt'

would be the file 'passwords.txt' one level below the document root.

And again - search/replace changes the scripts just fine. Use the right
tools and you don't need gimmicks.
>Then you don't need any entry in your .htaccess file (which even Apache
recommends against using), you don't have to worry about setting it up
or anything else. Everything works automatically.

Better to use "httpd.conf" or "php.ini" for such settings, but not
everyone is able to. I tend to use ".htaccess" for testing and then
migrate to "httpd.conf" once I'm sure.
Again - what if you're on an IIS system? No .htaccess. What if your
hosting company has restricted you so that PHP cannot load values from
..htaccess? I've found several who do.
>And BTW - it works in IIS also, which doesn't have a .htaccess file.
Works great, in fact.

As does "include_path" -- you just need to set it in php.ini.
Which is impossible on a shared host. Not everyone has access to or
needs a dedicated server or a vps.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 20 '07 #6

P: n/a
That works but here's the issue. I develop locally using virtual host like
www.client1site.com. When I want my client to see the progress, I upload
to:www.mysite.com/clients/client1/

So you can see that by referencing:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");
take the reference to mysite.com/utility/, where instead I want it to go to
mysite.com/clients/client1/utility

Does that make better sense?

I am just looking for a way where I don't have to change all my references
when I upload it for client review, BEFORE uploading it to it's final host.
Jan 20 '07 #7

P: n/a
Paul wrote:
That works but here's the issue. I develop locally using virtual host like
www.client1site.com. When I want my client to see the progress, I upload
to:www.mysite.com/clients/client1/

So you can see that by referencing:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");
take the reference to mysite.com/utility/, where instead I want it to go to
mysite.com/clients/client1/utility

Does that make better sense?

I am just looking for a way where I don't have to change all my references
when I upload it for client review, BEFORE uploading it to it's final host.


On your "DEV" machine create a file called const_path.php (or something
like that) on a fixed location.
Similar to your "DEPLOYMENT" machine,
create another file (with same name, with same location relative to
$_SERVER['DOCUMENT_ROOT'].

On that file, you define your path constants.
On your "DEV" machine.... it will be: define('CONST_PATH_UTILITY',
$_SERVER['DOCUMENT_ROOT'].'/utility');
On your "DEPLOYMENT" machine ... define('CONST_PATH_UTILITY',
$_SERVER['DOCUMENT_ROOT'].'/clients/client1/utility');

On your page, you need to include the file:
require_once($_SERVER['DOCUMENT_ROOT'].'/const_path.php');

Then you include your utility file:
require_once(CONST_PATH_UTILITY . '/top.php');

Make sure you don't overwrite the two different const_path.php between
"DEV" and "DEPLOYMENT" server
--- OR ---

you can always use relative path :)
Hendri Kurniawan
Jan 21 '07 #8

P: n/a
Paul wrote:
That works but here's the issue. I develop locally using virtual host like
www.client1site.com. When I want my client to see the progress, I upload
to:www.mysite.com/clients/client1/

So you can see that by referencing:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");
take the reference to mysite.com/utility/, where instead I want it to go to
mysite.com/clients/client1/utility

Does that make better sense?

I am just looking for a way where I don't have to change all my references
when I upload it for client review, BEFORE uploading it to it's final host.

I don't understand your problem.

Let's say your local machine path the the file is
/var/http/clients/client1/utility/top.php

So, just have a virtual host on your local machine which points to
/var/http/clients/client1. Another virtual host will point at a
different directory.

I have about a dozen different virtual hosts on my development machine
right now, each pointing to a different directory. And the files on
every one have the same path relative to their document root that the
specific client has.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 21 '07 #9

P: n/a
..oO(Jerry Stuckle)
>Again - what if you're on an IIS system? No .htaccess. What if your
hosting company has restricted you so that PHP cannot load values from
.htaccess? I've found several who do.
Change the host? Why should I stay with a company that doesn't provide
the tools I need?
>As does "include_path" -- you just need to set it in php.ini.

Which is impossible on a shared host.
Depends. I'm on a shared host and can use my own php.ini.

Micha
Jan 22 '07 #10

P: n/a
Michael Fesser wrote:
.oO(Jerry Stuckle)
>Again - what if you're on an IIS system? No .htaccess. What if your
hosting company has restricted you so that PHP cannot load values from
.htaccess? I've found several who do.

Change the host? Why should I stay with a company that doesn't provide
the tools I need?
>>As does "include_path" -- you just need to set it in php.ini.
Which is impossible on a shared host.

Depends. I'm on a shared host and can use my own php.ini.

Micha
Not really,

If they're using the CGI version of PHP (instead of the Apache
extension), you can use a subset of the php.ini in your directory. But
unless they have no concerns about security (or don't understand
security), you won't be able to put all the possible options in it. And
what they allow or disallow varies from one host to another.

And if you do have all options available I'd switch hosts - FAST.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 22 '07 #11

P: n/a
..oO(Jerry Stuckle)
>Michael Fesser wrote:
>>
Depends. I'm on a shared host and can use my own php.ini.
Not really,

If they're using the CGI version of PHP (instead of the Apache
extension), you can use a subset of the php.ini in your directory. But
unless they have no concerns about security (or don't understand
security), you won't be able to put all the possible options in it.
True. But it's more than enough. Additionally many of the most-used
directives are PHP_INI_ALL, so they can be set everywhere.

Micha
Jan 22 '07 #12

P: n/a
Jerry Stuckle wrote:
If they're using the CGI version of PHP (instead of the Apache
extension), you can use a subset of the php.ini in your directory. But
unless they have no concerns about security (or don't understand
security), you won't be able to put all the possible options in it. And
what they allow or disallow varies from one host to another.

And if you do have all options available I'd switch hosts - FAST.
So that's what's going on!
I have a PHP.ini file, too.
I was wondering how they can do that, and still keep their
servers from blowing up.
Jan 22 '07 #13

P: n/a
Michael Fesser wrote:
.oO(Jerry Stuckle)
>Michael Fesser wrote:
>>Depends. I'm on a shared host and can use my own php.ini.
Not really,

If they're using the CGI version of PHP (instead of the Apache
extension), you can use a subset of the php.ini in your directory. But
unless they have no concerns about security (or don't understand
security), you won't be able to put all the possible options in it.

True. But it's more than enough. Additionally many of the most-used
directives are PHP_INI_ALL, so they can be set everywhere.

Micha
Maybe it's more than enough. Maybe not. It all depends on what they
allow you to set.

And if you ever change hosts to someone using the Apache extension,
you'll be SOL. It's even worse if you change to a Windows host.

The bottom line is - this doesn't work on every system. The
$_SERVER['DOCUMENT_ROOT'] does.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 23 '07 #14

P: n/a
..oO(Jerry Stuckle)
>And if you ever change hosts to someone using the Apache extension,
you'll be SOL. It's even worse if you change to a Windows host.
Why should I? My own sites and the sites I maintain are hosted on
servers carefully chosen by me. There's no reason to change.

Micha
Jan 23 '07 #15

P: n/a
Michael Fesser wrote:
.oO(Jerry Stuckle)
>And if you ever change hosts to someone using the Apache extension,
you'll be SOL. It's even worse if you change to a Windows host.

Why should I? My own sites and the sites I maintain are hosted on
servers carefully chosen by me. There's no reason to change.

Micha
And I've heard that one so many times before. The last time just a few
months ago with a company who had been an excellent provider for years.

Companies change. Management changes. Companies get new owners. And
companies go out of business.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 23 '07 #16

P: n/a

As I am not having much experience, I don't know if the following will
result any security vulnerability, or may be effect at other part of
the code causing unexpected results internally.

/* concat path */

$_SERVER['DOCUMENT_ROOT'] .= "/clients/site1";
But for temporary purpose, can this be used at the top of every page/or
any common page that is being included by all other files.

What I generally do is,

(in config file)

define('APP_DIR', '/any/path/if/present/');
define('APP_FULL_URL', 'http://'.$_SERVER["HTTP_HOST"].APP_DIR);

and use "APP_FULL_URL" to include files, etc.
Other is I define a variable $basepath at the top of every page, that
is relative to the directory that has main index file for the site.

/
all files have $basepath="./";

/dir1/
/dir2/
all files have $basepath="./../";
/dir1/subdir1/
/dir2/subdir1/
all files have $basepath="./../../";

defined at top of page.

And $basepath is used for including all other files.
May not be good way to do, but has been working fine for all projects
till now.

Paul wrote:
On my development computer, I have virtual named host set up, like
www.site1.lab.

When I upload those to my web site for customer review under
mywebsite.com/clients/site1/
it throws some of the source file references off and it does not work
properly.

My references are like:
require_once($_SERVER['DOCUMENT_ROOT']."/utility/top.php");

Obviously those will not work since $_SERVER['DOCUMENT_ROOT'] references
mywebsite.com.

Is there anything I can do in apache.conf for a virtual directory that would
make it so when I upload a site for customer review, I don't have to change
the source references?
Jan 23 '07 #17

P: n/a
ALl good thoughts - I'll give them a try - many thanks
Jan 30 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.