473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
17 1670
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
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
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
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
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
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
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
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
..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
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
..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
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
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
..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
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

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
ALl good thoughts - I'll give them a try - many thanks
Jan 30 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andy | last post by:
Hi, I'm trying to get mod_python working on my web server, but I'm running into problems, possibly due to having virtual hosts configured. I'm no apache guru (newbie is more accurate!) so I...
8
by: nick | last post by:
I have a problem and I've been using a cheezy work around and was wondering if anyone else out there has a better solution. The problem: Let's say I have a web application appA. Locally, I set...
16
by: B Letts | last post by:
Hi - I'm currently using the FileUpload control to allow people to upload files to my website. This all works fine, as long as I'm going to a physical path on my server. However, I need to...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
4
by: Marko Vuksanovic | last post by:
I am trying to cause the uplaod button, id="Upload",when clicked, to exectue the onClick event for Button1, id="Button1". <asp:FileUpload id="FileUpload" runat="server"> </asp:FileUpload>...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.