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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    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?


  • Rik

    #2
    Re: use virtual hosts to develop locally but upload throws error in source file references

    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


    Comment

    • Toby Inkster

      #3
      Re: use virtual hosts to develop locally but upload throws error in source file references

      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_function s.php
      includes/database_functi ons.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

      Comment

      • Jerry Stuckle

        #4
        Re: use virtual hosts to develop locally but upload throws errorin source file references

        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_function s.php
        includes/database_functi ons.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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Toby Inkster

          #5
          Re: use virtual hosts to develop locally but upload throws error in source file references

          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_pa th" -- you just need to set it in php.ini.

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

          Comment

          • Jerry Stuckle

            #6
            Re: use virtual hosts to develop locally but upload throws errorin source file references

            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_pa th" -- 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.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Paul

              #7
              Re: use virtual hosts to develop locally but upload throws error in source file references

              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.


              Comment

              • Hendri Kurniawan

                #8
                Re: use virtual hosts to develop locally but upload throws errorin source file references

                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_P ATH_UTILITY',
                $_SERVER['DOCUMENT_ROOT'].'/utility');
                On your "DEPLOYMENT " machine ... define('CONST_P ATH_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(CO NST_PATH_UTILIT Y . '/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

                Comment

                • Jerry Stuckle

                  #9
                  Re: use virtual hosts to develop locally but upload throws errorin source file references

                  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.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Michael Fesser

                    #10
                    Re: use virtual hosts to develop locally but upload throws error in source file references

                    ..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_pa th" -- 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

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: use virtual hosts to develop locally but upload throws errorin source file references

                      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_pa th" -- 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.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • Michael Fesser

                        #12
                        Re: use virtual hosts to develop locally but upload throws error in source file references

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

                        Comment

                        • Sanders Kaufman

                          #13
                          Re: use virtual hosts to develop locally but upload throws errorin source file references

                          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.

                          Comment

                          • Jerry Stuckle

                            #14
                            Re: use virtual hosts to develop locally but upload throws errorin source file references

                            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.
                            jstucklex@attgl obal.net
                            =============== ===

                            Comment

                            • Michael Fesser

                              #15
                              Re: use virtual hosts to develop locally but upload throws error in source file references

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

                              Comment

                              Working...