PHP Includes and root variable

ah ok, my bad

@hash
When I was referring to the href, the following are relative URLs:
index.html -> Relative to the base URL and current directory
/index.html -> Relative to the base URL
./index.html -> Relative to the base URL and parent directory
Anything that is coded without the base URL of the site is relative.

When you are using relative URLs in your links, the base URL is assumed. Thus, the following have the identical behavior:
<a href=“index.php”> <-This is relative
<a href=“http://www.somedomain.com/index.php”> <– This is absolute

<a href=“/some_dir/index.php”> <-This is relative
<a href=“http://www.somedomain.com/some_dir/index.php”> <– This is absolute

Depending on the scope of the project, relative URL references may be fine. In some instances (all in my case), however, that simply is not the case. To simplify management for smaller sites, relative URLs can work. If you are developing an application that others will install in their own environments, plan on moving files/directories around, use a Content Data Network (again, this has it s own use cases that can differ), then you should definitely use absolute URLs whenever possible. Another use case, using mod_rewrite for content. By using absolute paths, content can live in what appears to be separate URLs without breaking image / css references. I won’t get into that as this will totally veer this thread into the wrong direction since it is about PHP file paths.

With regard to paths in your PHP code, I am a strong advocate of using absolute references. From being able to internally audit locations, to enforcing security policies, using absolute references just makes sense. I recommend to all new PHP coders to get in the habit of using absolute references whenever possible. Like the HTML use cases above, this also depends on the scope of the project. This can not only makes things clear as to where things go, but also allows for configuration changes on a per deployment basis. This is particularly true when a bulk of the PHP code could live outside of the public HTML space, including configuration files (think security). If your script uses cron jobs, the use of relative path’s will lead to a lot of frustrated users and headaches for you.

Oh no. Path question again. With same rumors and misunderstandings. Sitepoint desperately need an article on what is a path and what is a file.

conradical
The first thing you had to do, is to determine the real path to your module-header.php file. As you have access to whole disk, it is not too hard, right?
So, your goal then would be limited just to producing a string, equal to that standard. Much better than guessing.
And string comparing is not too hard too.

Now you ended up with hardcoded path instead of automated one. Not the best choice.
Why don’t you just var_dump() both working and not working paths and just visual compare it? And then correct the automated one? To use on both servers unchanged?

@inclick, you’ve smooth words, but I prefer a code.

. If you are developing an application that others will install in their own environments,

…they’ll end up with your domain in their links.

plan on moving files/directories around,

so what? how do your base url thing will help with it?

use a Content Data Network

your base url constant won’t help with it

i would love to see a tutorial about this issue particular issue… including absolute (rather than relative to accomodate includes inside includes) paths in a relative way (ie transportable from test server/localhost to live deployment). it seems to be asked a million places online all over the place and i don’t see an clear step by step solution anywhere.

This is just not the case. Instead of trying to explain, here is evidence:
I checked twitter, flickr, and sitepoint. All use the “relative” url /page.

/ is the apsolute path to the root of the web server. There is no need to use the full domain url. If you really still disagree, perhaps you can give a specific example to help us understand.

@cutcopypaste
The reason you won’t get a clear answer is that there is more than one way to skin a cat.

What have you been searching for? I can find articles saying both sides…Each saying the other side is wrong. You are essentially wanting an article that says and ends all debate that says “PHP is better than ASP” OR “Windows is better than Linux/Max/etc.” The only way I can think of "absolute (rather than relative to accommodate includes inside includes) paths in a relative way " is a test and live box. I have had experience with this at work, and have zero idea how it works. I only know after my boss says ok, i do an install terminal command that copies from dev to live. We strictly use relative links.

Off Topic:

the article titles suggested do not necessarily reflect my opinions

well perhaps i am getting confused because i think some answers are about doing it the same way when really they are completely different. I just wnat to get includes within includes referenced from different directories set up so that i can migrate from test server to hosting server with minimum fuss, preferably not having to edit php.ini because that’s a whole nother can of worms. if i saw two examples of how to do it, or even three or four, and pros and cons etc… i just don’t know enough to piece the fragments i’ve been finding out there together.

Step 1. Use this to include files:


include $_SERVER['DOCUMENT_ROOT'].'path/to/files/file.php';

Step 2. Make sure your test site and your live site have the same directory structures with regards to the doc root:
dev: C:/mamp/site/path/to/files/file.php
live: /home/user/public_html/path/to/files/file.php

Then just use the standard document root as your base reference.


require_once $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';