Direct Path Names for Hosting Using PHP

Care to elaborate

ScallioXTX, it solves the problem that doesn’t exist.

Alex, blame my bad English, I can’t do explanations properly.
Although I answered you already

You must distinguish your filesystem path from webserver path
/home1/username/public_html/template/ is the filesystem path
but this path doesn’t exist in the virtual web-server.
your browser don’t ever know of it’s existence!
the web-server path begins after public_html.
so, for the browser it will be /template/
so, you don’t need any single variable to make your virtual path absolute. it is always the same
Look at the URL of this very page:

It has protocol part, hostname part and request part, which consists of path, filename and query string.
notice the path?
/forums/showthread.php - <–absolute path. would remain the same if you call it from anywhere. that’s the meaning of the word “absolute”

So, if your styles dir lays in the root of the webserver,
/styles/ie7.css is enough.
<?php echo $root ?>/javascripts/jquery.ie6.js is wrong, because browser doesn’t have access to your hard disk!

And well, hash, I can’t agree with you too :slight_smile:
your method is sort of relative path,
it solves the problem with 2 hosts but won’t work if we move 1 folder deeper.
It works only if front controller used.
if not - we have to use something else
$_SERVER[‘DOCUMENT_ROOT’] is my favorite

And well, hash, I can’t agree with you too
your method is sort of relative path,
it solves the problem with 2 hosts but won’t work if we move 1 folder deeper.
It works only if front controller used.
if not - we have to use something else
$_SERVER[‘DOCUMENT_ROOT’] is my favorite

That is no good if you don’t know where your app has been installed.
There are several ways to do things, I mentioned an alternative that doesn’t require knowing where you are, and also doesn’t require knowing where you are in relation to true root - your apps root doesn’t have to be the servers.

I mentioned an alternative that doesn’t require knowing where you are

No, it does.
That’s what I am talking about.
$root = dirname(FILE); won’t give you real root
but just relative path to your script.
run your code in the /fourums/ dir - and it will fail.

While $_SERVER[‘DOCUMENT_ROOT’] is the real root, absolute one, if properly set (usually does)

I think we’re talking about diff things. There is server root, and also an app root that only your app cares about. It could be anywhere … /var/www/some/directory/project/apps/forum/ (or it could be identical to server root)

So for your app you either know where you are and go up x number of directories to get to forum/ or you don’t, and mark forum so you will know when you found it. Then your apps root is set, include $root.‘includes/file.php’ is /var/www/some/directory/project/apps/forum/includes/file.php
Using $_SERVER would fail horribly.

but my app can utilize some folder structure
and once I set it’s “root” in the /var/www/some/directory/project/apps/forum/
it won’t help me in the /var/www/some/directory/project/apps/forum/polls/
if I don’t use front controller.

your method utilizes relative path, not absolute. that’s it
so, you don’t really need your code
include includes/file.php’; would have the same effect

Like I said, two methods:

  1. you know where you are (eg with front controller)
  • set root based on where you are
  • $root = realpath(dirname(FILE).‘…/’
  1. You don’t know where you are
  • go up till you find root.txt
  • set this as root

End of the day you have your apps root as the full path to the directory you want as your apps root, so no matter where you include $root.includes/file.php it works, instead of only working from /forum

End of the day you have your apps root as the full path to the directory you want as your apps root, so no matter where you include $root.includes/file.php it works, instead of only working from /forum

Your code won’t help with it.
it works from the forum only.
if you call it from the forum/index.php it will point to the ‘forum/includes/file.php’
and if you call if from the /forum/polls/index.php it will point to the ‘forum/polls/includes/file.php’ and fail.

your method is relative, not absolute
sigh

How does setting the $root to be /var/www/forums/ and using include $root.‘includes/file.php’ EVER include anything other that /var/www/forums/includes/file.php no matter where it is used?

sigh


$root = dirname(__FILE__);
while(!file_exists($root.'/root.txt')) {
    $root = realpath($root.'/..')
}

If you know the app root is always one up from the front controller, then just skip the while. Either way, what you end up with is the full absolute path to the root you want as your apps root (which may or may not be the same as the doc root).

The problem:
Need a root folder to reference other folders by (cache, log, include, lib whatever)

Solution 1:
Use $_SERVER[‘DOCUMENT_ROOT’]
This won’t always work. If your app gets moved one folder down, then all is broken.

Solution 2:
You know where the root is in relation to the boot script/front controller.
Set $root accordingly, as an absolute path. Now you don’t care where you are moved.

Solution 3:
You don’t know where you are in relation to the app root. Put a marker in the root, and move upwards looking for it, set $root to this absolute path.

Solution 4:
Use a config directive and define a root. As with #1, has to be updated if your app moves directories.

Solutions 5-79???

ahaha that’s funny code.
never thought of that. this one is near to usable one
but I’d hate to write such a construct to get my config.
I would prefer $_SERVER[‘DOCUMENT_ROOT’]./…/config/ini.php

As of your previous approach, it’s

  1. useless
  2. wrong.

Case one:
Assume we call it from the /users.php
and our includes folder’s path is /includes/
So, we can replace your code

$root = dirname(__FILE__);
include $root.'/includes/file.php'; //always works 

with just


'includes/file.php'; 

with same result

verdict: useless

Case two:
Assume we call it from the /news/archive.php
and our includes folder’s path is /includes/
in this archive.php we have your code

$root = dirname(__FILE__);
include $root.'/includes/file.php'; //always works 

and “File not found” error
because your $root variable named wrong. it has nothing to do with root as it merely relative path to the calling file and nothing more

verdict: wrong

And if your config is in $_SERVER[‘DOCUMENT_ROOT’]/path/to/moved/app
Then what do you do? Hard code it? Nice one.

this is nowhere near the same result.
$root can be used from anywhere in the app, include ‘includes/’ cannot.

Again you fail a bit to grasp what is happening.
You would never set root to be where you are unless you know exactly where you are. and you would never use inculdes/ to include system/

try to think this one through there shraps. particularly before all the lol fail haha wrong you like to throw around.

$root can be used from anywhere in the app, include ‘includes/’ cannot.

words, words…
prove it with the code, please

You would never set root to be where you are unless blah blah blah

I am plain and simple guy.
I just want to include /includes/file.php in my /news/archive.php
and keep it if I move it to the /news/tech/archive.php
Not to talk of such theoretical matters

prove what with code? that the app root dir might not be the same as the doc root? that the app could be installed in any sub dir and if you don’t want to change a hard coded path then you can determine where you are?

Oh, another page hit! What an achievement! :slight_smile:

I don’t even know what you’re on about now.

You can have $root = $_SERVER[‘DOCUMENT_ROOT’]
Or you can have $root = ‘some/other/path/that/you/want/to/be/root/for/your/app’

If you always use the first one, then you can’t move your app.
If you set your own root to what you want, then you can.

How you do it is up to you. I’m sure you’re aware that FILE is the actual file not the working directory.

I am more on practical matters, not theory.

In practice, we usually need some config file, being included into every our script.
In this config file we can set up anything: paths to the modules, passwords to the database, etc etc. We can move certain app and then change only one line in config file - so, all your moving problems solved.
But we need our config first.

We have 4 ways to do it.

  1. hardcoded path. is not an option at all
  2. front controller. but usually we don’t have one front controller for the whole site. More likely each app has it’s own controller. So, the problem persists.
  3. $_SERVER[‘DOCUMENT_ROOT’], the monument of the absoluteness. I’d recommend it for the general use.
  4. auto_prepend_file. VERY useful, as it make manual inclusion unnecessary. But a bit complicated to use, basic .htaccess knowledge and some PHP configuration settings required. recommended for the experianced users, who can also choose a server by their own will

Well that makes a little more sense from your side. But as usual you don’t explain yourself. Sorry to say, it’s not your english that is the problem, it’s your attitude.

You want to have a global init file in the doc root … go for it :slight_smile: access it via doc root and sleep well.

If you want to allow someone to move your app, then you fail. I provided two ways to get around this. I would say that each app with it’s own front controller would be a fantastic reason to determine the apps absolute path via a relative path. Surely any shared libs common to apps will be just above this layer?

There are many ways to skin a cat. My point is simply that I prefer to use require …/config.php from an entry point and $root = dirname(FILE); in that config than alter the config file.

Well, back to your point.

The OP has 2 problems

  1. absolute addressing from the web root, for the CSS files, no matter from which catalog it being called
  2. absolute addressing from the filesystem root. I assume, with same purpose. To call /includes/file.php, no matter from which catalog it being called
    And your code fail to help him.

That’s plain and simple. We just want a way to use absolute path. But you give him the same old relative one!

  1. was taken care of, webroot is /
  2. I gave a way to make an absolute path based on a relative one. ie …/something is a relative path, dirname(…/something) is an absolute path (based on a relative path, but it will give you ABSOLUTE).
    2,a I gave an alternative way to do this if you were going to use arbitrary files like www.site.com/some/silly/page/burried/and/accessed.php

gave a way to make an absolute path based on a relative one

oxymoron (it’s not a curse)
you cannot get absolute from the relative, because of nature of these matters

er dirname will return the path, is that a fake path?
getting late, pls explain what you mean a bit.