9 Replies - 896 Views - Last Post: 19 January 2013 - 04:05 PM Rate Topic: -----

#1 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 14 January 2013 - 06:03 PM

Hello,

I have an issue since I moved a site to a new development server Xammp light.

The site uses $_SERVER["DOCUMENT_ROOT"] in many of the require(); lines and the new server gives an error

<br /> <b>Warning</b>: require(C:/xampplite/htdocs/core/connect.inc.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in <b>C:\xampplite\htdocs\Jinglle\scripts\sec_questions\get_user_security_question.php</b> on line <b>2</b><br /> <br /> <b>Fatal error</b>: require() [<a href='function.require'>function.require</a>]: Failed opening required 'C:/xampplite/htdocs/core/connect.inc.php' (include_path='.;C:\xampplite\php\PEAR') in <b>C:\xampplite\htdocs\Jinglle\scripts\sec_questions\get_user_security_question.php</b> on line <b>2</b><br />



Should I change the $_SERVER["DOCUMENT_ROOT"] value? If so, what is the best way to this? If not, what is the best route to take?

This all worked fine until the copy to the new development server.

Thank you for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,858
  • Joined: 12-December 12

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 14 January 2013 - 06:12 PM

Quote

$_SERVER['DOCUMENT_ROOT'] in different environments may has trailing slash or not, so be careful when including files from $_SERVER['DOCUMENT_ROOT']:
<?php
include(dirname($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . 'file.php')
?>


I'm not sure if this is the direct cause of your issue.

This post has been edited by andrewsw: 14 January 2013 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 14 January 2013 - 07:24 PM

as you can see from the error that my document root is C:/xampplite/htdocs so, I believe, I need to change it to be localhost or 127.0.0.1 for it to read the file as a php file.

Is this correct?
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 14 January 2013 - 07:27 PM

The problem is that the DOCUMENT_ROOT value is always the local path to the web-root, so by using it the code will only work if the files are always at the exact same location under the root, which is presumably directly under it for production sites. - This is failing for you because you seem to have put the files into a sub-directory under the root: "Jinglle".

So, the solution, without changing exchanging the DOCUMENT_ROOT for something else, would be to move the files directly under the root. You can do this without losing the ability to host other sites on your machine by using virtual machines. They each can have their own root, but still be hosted on the same machine. (It's not that complex, but takes a bit to understand properly.)

What I like to do when including files is to use the absolute location of the current file as the root, and the traverse the file system from that point. So, for example, in your situation I might do this:
require __DIR__ . "/../../../core/connect.php";


(Note that __DIR__ only works with PHP 5.3 and up. Use dirname(__FILE__) for older versions.)

With this, you can move the project wherever you want, without any regard for where files are placed, and it will always know where to go. This also removes the need to use any sort of pre-set constant, which is also a popular way to handle these types of situations.


For more advanced designs, though, where everything is OOP, you'll of course want to use autoloaders instead. Much simpler like that.

View Postlaytonsdad, on 15 January 2013 - 02:24 AM, said:

as you can see from the error that my document root is C:/xampplite/htdocs so, I believe, I need to change it to be localhost or 127.0.0.1 for it to read the file as a php file.

Is this correct?

No. Remember that your code is trying to import the actual code so it can be used in the importing script. If you load it via HTTP (through localhost or 127.0.0.1) you'll only ever get the result of the PHP code execution, not the actual PHP code.
Was This Post Helpful? 1
  • +
  • -

#5 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 14 January 2013 - 08:56 PM

View PostAtli, on 14 January 2013 - 07:27 PM, said:

This is failing for you because you seem to have put the files into a sub-directory under the root: "Jinglle".


I knew this too....what a dunce.

View PostAtli, on 14 January 2013 - 07:27 PM, said:

So, the solution, without changing exchanging the DOCUMENT_ROOT for something else, would be to move the files directly under the root. You can do this without losing the ability to host other sites on your machine by using virtual machines. They each can have their own root, but still be hosted on the same machine. (It's not that complex, but takes a bit to understand properly.)


Can you point me in the right direction of what to search for to get a better understanding of the VM capability, is this an adjustment to Apache?
EDIT: I found somthing about virtual hosts on the Apache site.

I understand VM's for windows, I don't know a lot about Apache.

View PostAtli, on 14 January 2013 - 07:27 PM, said:

No. Remember that your code is trying to import the actual code so it can be used in the importing script. If you load it via HTTP (through localhost or 127.0.0.1) you'll only ever get the result of the PHP code execution, not the actual PHP code.

I see what your saying, and I will be looking into the VM situation you posted.
Thank you for the refresher.

This post has been edited by laytonsdad: 14 January 2013 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 15 January 2013 - 05:18 AM

View Postlaytonsdad, on 15 January 2013 - 03:56 AM, said:

Can you point me in the right direction of what to search for to get a better understanding of the VM capability, is this an adjustment to Apache?
EDIT: I found somthing about virtual hosts on the Apache site.

I understand VM's for windows, I don't know a lot about Apache.

They aren't VM's (virtual machines), they are virtual-hosts. (Sometimes "vhosts" for short.) Those are very different terms. Important if you are searching for info on this :)/>

If you go into your Apache installation directory, you'll find a file called "conf/extras/httpd-vhosts.conf". This is typically where virtual-hosts are configured for Apache. In order to use that file, though, you'll have to go into the "conf/httpd.conf" file and include it, by creating (or uncommenting) a line like this:
Include extras/httpd-vhosts.conf



The first thing to do is to add a NameVirtualHost *:80 directive - if it's not already present somewhere. It's sometimes already set in the main config file, even though no virtual hosts were intended to be active. This'll tell Apache to start looking for different domain names in incoming requests, and redirect them to the proper virtual hosts.

Then you can start editing the vhosts file to define your additional hosts. For example, a virtual-host entry may look like this:
<VirtualHost *:80>
        # This directive sets the main domain associated
        # with this vhost
        ServerName dev.localhost

        # And any additional domains can be set using aliases.
        # You can add as many of these as are needed.
        ServerAlias dev.machine-name.lan
 
        # This sets the root dir of the vhost, and sets
        # the permissions and other settings for that dir.
        DocumentRoot /home/www-data/vhosts/dev
        <Directory /home/www-data/vhosts/dev>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # Or, if you're on Apache 2.4, replace the
                # two above with this:
                #   Require all granted
                </Directory>

        # You can also set up different log files only for
        # this vhost.
        ErrorLog ${APACHE_LOG_DIR}/dev.error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/dev.access.log combined

         # An optional directive, yet still not a bad idea to set.
        ServerAdmin you@example.com
</VirtualHost>



Also, remember that the vhost domain set up there will have to be associated with the local host as well. See this post for details about that.

Once all that is set up, you should be able to type in http://dev.localhost/ and it'll serve the files from the virtual host doc root.
Was This Post Helpful? 2
  • +
  • -

#7 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 15 January 2013 - 01:13 PM

This is what I came up with that didn't crash Apache but it doesn't work at jinglle.localhost

NameVirtualHost *:80

<VirtualHost *:80>
        # This directive sets the main domain associated
        # with this vhost
        jinglle.localhost

        # And any additional domains can be set using aliases.
        # You can add as many of these as are needed.
        ServerAlias jinglle.localhost
 
        # This sets the root dir of the vhost, and sets
        # the permissions and other settings for that dir.
        DocumentRoot C:/xampplite/htdocs/Jinglle.localhost
        <Directory C:/xampplite/htdocs/Jinglle.localhost >
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # Or, if you're on Apache 2.4, replace the
                # two above with this:
                #   Require all granted
         </Directory>
         # An optional directive, yet still not a bad idea to set.
        ServerAdmin pagesbydustin@gmail.com
</VirtualHost>


I don't seem to be able to find the issue in this without crashing Apache. I have been reading the docs on the apache site and am not to sure what I am doing with it.

Thank you for your time.
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 15 January 2013 - 01:50 PM

Line 6 is missing the "ServerName" directive. It should look more like:
ServerName jinglle.localhost


Line 10 should also be removed, seeing as you don't need an alias for the main host name. Those are for secondary names.

I can't remember exactly whether the paths on Windows need to be like this, but I always wrote them quoted with the Windows style back-slash delimiters. I know that works, at least.
DocumentRoot "C:\xampplite\htdocs\Jinglle.localhost"
<Directory "C:\xampplite\htdocs\Jinglle.localhost">



One thing also seems odd to me. Is the root directory of the vhost inside the main DocumentRoot? (I assume that's the "C:\xampplite\htdocs" directory.) - I doubt it will crash the server, or anything, but it would probably be better to move the vhost files outside the main host, so they aren't being served by two separate hosts.
Was This Post Helpful? 1
  • +
  • -

#9 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 16 January 2013 - 10:10 AM

So I have this:
<VirtualHost *:80>
        # This directive sets the main domain associated
        # with this vhost
        ServerName jinglle
 
        # This sets the root dir of the vhost, and sets
        # the permissions and other settings for that dir.
        DocumentRoot "C:/xampplite/jinglle"

        <Directory "C:/xampplite/jinglle">
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # Or, if you're on Apache 2.4, replace the
                # two above with this:
                #   Require all granted
         </Directory>
         # An optional directive, yet still not a bad idea to set.
        ServerAdmin pagesbydustin@gmail.com
</VirtualHost>


It works now when I go to localhost. I would like it to work when I go to jinglle.lh so that my xammp will still show when I go to localhost so I can use the phpmyadmin and such.

Thank you for your time.
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Change $_SERVER["DOCUMENT_ROOT"] path for xammp light

Posted 19 January 2013 - 04:05 PM

OK. The ServerName value should be the domain you want to use when opening the server in your browser. So if it's just "jinglle", you'd have to go to: http://jinglle/ to open it. - I tend to like setting up sub-domains for the localhost though, like: "jinglle.localhost", just so I'm not overriding actual URLs.

You must also remember to alter the hosts file so that the system knows to redirect the domain to the localhost. If you use "jinglle", for example, the host file must include a line that reads:
127.0.0.1    jinglle


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1