Jump to content

Document root using '\' question


Go to solution Solved by mac_gyver,

Recommended Posts

Hey y'all.

 

I'll gladly admit that I'm not a server guy - I have the utmost respect for people comfortable enough with *nix to handle Apache setup and configuration in a production environment. I can noodle about in *nix typically without killing anything vitally important, but trust me when I say no-one wants me to be a server administrator. That having been said, I do have a development server behind my home network firewall running Ubuntu that I use to test and develop the sites that I create on my separate Windows box. So I try to keep up with the way a shared server would be set up in a real-world situation, to a degree. For instance, I haven't bothered setting up the mail server or FTP, but I do create virtual host files for each of my projects.

 

So, I've got a virtual host set up on the directory /var/www/myAwesomeSite.com/ with the .conf file containing the following configuration:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName myAwesomeSite.com
	DocumentRoot /var/www/myAwesomeSite.com/www
	<Directory /var/www/myAwesomeSite.com/www>
		AllowOverride All
	</Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now, in my php scripts echoing out $_SERVER['DOCUMENT_ROOT'] gives me 'myAwesomeSite/www' as I would expect. And using $_SERVER['DOCUMENT_ROOT'] in any require() statements actually does include the requested file, so that part seems to be working fine. My question is this: when I use a leading slash ('/'), shouldn't that equate to the document root? I use it at work all the time and it works flawlessly, but on the dev server I have set up here, it's a no-go. Is there another config command that I need to issue to make it work, or am I crazy?

 

Basically, am I missing a setting somewhere that will make this:

require_once('/path/to/my/include.php');

work like this?

require_once($_SERVER['DOCUMENT_ROOT'].'/to/my/include.php');

*edit* I just realized that I used the wrong slash in the title of this post, but I don't see a way to change the topic title. crap.

Edited by maxxd
Link to comment
https://forums.phpfreaks.com/topic/292657-document-root-using-question/
Share on other sites

for the require/include statements you are showing (that don't have a protocol and a domain specified, such as http://somedomain or ftp:/somedomain) you are dealing with file system paths. for a file system path, a leading / refers to the root of the current disk.

 

the confusion with this occurs because for a URL, a leading / refers to the domain root of the current page in the browser, and is a domain root relative URL (as apposed to a relative URL that starts with a . or a..) when the browser finds something in the context where a URL gets used in the html markup, if it starts with a /, the browser takes the protocol and domain of the current page and appends the domain root relative URL it found to form the actual URL that will be used.

 

file system paths on the server and paths in a URL in the html of a page in a browser are two different things.

That makes perfect sense - thank you!

 

So if I were to prepend a www. to the 'domain name' it'd work as expected? I'm using quotes because it's not a registered domain name, and I didn't prepend the www. to the server directory to begin with because it's all just on my local network - I'm using the .hosts file on my Windows boxes to trick the browser into connecting to the dev server as though the site was published on the Interwebs.

a www has nothing to do with this, as a www is part of a URL specifier, i.e. the host-name/sub-domain. include/require is using a file system path. nothing to do with a URL.

 

the only way a leading / would work in an include/require statement is if the resultant path is to where the actual file being included/required is at, starting at the root of the current disk. but this would mean that all code written in this environment will break if it is moved to a different directory structure. the whole point of having a variable like $_SERVER['DOCUMENT_ROOT'] is so that the code can be put onto any server and it will work regardless of the underlying directory origination.

Hang on - wait, though. Question about that. At work, our development server is a BlueHost shared server; using '/' can't refer to the disk root in that case, can it? Because I don't have root access, and there are other hosted sites on the same server. The way we set it up is this - with every site, we create a subdomain of our main development domain, and using a slash within that subdomain to create an absolute reference to stylesheets, images, javascript files, etc. works.

 

What's the difference between me setting up a virtual host on my local machine and a national hosting provider setting up a virtual host on their machine (other than the fact that they know what they're doing)? I actually never used to do the whole virtual server set-up at home, but I was running into issues where simply using http://myDevMachine/myNewSite/www/ was causing behavior that differed from my production server.

  • Solution
reference to stylesheets, images, javascript files, etc. works.

 

 

you would need to post some example code, but i'm betting you are now talking about links in your html that are loaded by the browser via their URL and NOT things that are being included/required by the php code.

you would need to post some example code, but i'm betting you are now talking about links in your html that are loaded by the browser via their URL and NOT things that are being included/required by the php code.

You are absolutely correct - OK, thank you for clearing that up in my head. It made perfect sense until I started over-thinking it...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.