Jump to content

Alternative for '$_SERVER['DOCUMENT_ROOT']'


P0oltj3

Recommended Posts

Hallo phpfreaks,

 

I`m new to this specific forum so I would like to contribute to some topics in the future.

My question here is about absolute php paths.

 

Right now I`m busy with making my own cms and I want to make it as flexible as possible.

I`m not a fan of frameworks or library's so I`m making everything by myself (for this project).

 

I already found a specific script that does exactly what I want to accomplish but I`m not so happy

with that it uses

$_SERVER['DOCUMENT_ROOT']

and this is the script that I found:

substr(
str_replace(
'\\', '/', realpath(dirname(__FILE__))),
strlen(
str_replace(
'\\', '/', realpath($_SERVER['DOCUMENT_ROOT']))));

At this moment I`m using Windows so this is the output that I get with it:

/Projects/cms/5113/htdocs2

 

I was hoping that someone would know a alternative to the $_SERVER part.

 

If it`s unclear what I`m trying to accomplish I`ll just write the whole story for you.

Link to comment
Share on other sites

Hallo phpfreaks,

 

I`m new to this specific forum so I would like to contribute to some topics in the future.

My question here is about absolute php paths.

 

Right now I`m busy with making my own cms and I want to make it as flexible as possible.

I`m not a fan of frameworks or library's so I`m making everything by myself (for this project).

 

I already found a specific script that does exactly what I want to accomplish but I`m not so happy

with that it uses

$_SERVER['DOCUMENT_ROOT']

and this is the script that I found:

substr(
str_replace(
'\\', '/', realpath(dirname(__FILE__))),
strlen(
str_replace(
'\\', '/', realpath($_SERVER['DOCUMENT_ROOT']))));

At this moment I`m using Windows so this is the output that I get with it:

/Projects/cms/5113/htdocs2

 

I was hoping that someone would know a alternative to the $_SERVER part.

 

If it`s unclear what I`m trying to accomplish I`ll just write the whole story for you.

 

Why don't you want to use $_SERVER vars?

Link to comment
Share on other sites

*Christian just shakes his head.

 

Sorry to say this, but this is wrong on so many levels I almost don't know where to start.

 

First off, I recommend that you read this post of mine. It should help explain why you really should reconsider your opinion on existing systems.

 

Secondly: There is absolutely nothing wrong with depending upon the $_SERVER variables, especially not DOCUMENT_ROOT. If a server is configured in such a way that that index is not available/reliable, the server has a LOT more issues than your scripts not working. Also, the owner of said server should be all too aware over this limitation already.

As for setting your own global variables, and hardcoding the document path. That's just asking for a maintenance issue. With having to update said variable every time something on the server changes, or the server itself changes. It's not a reliable alternative for $_SERVER in any way, form, or shape.

 

It is great that you're writing your own CMS, but only if you're doing this to learn how to make one. Doing it for a project that's going to be put into production, is counter-productive (see article about square wheel). Especially if this is only because you don't like code you haven't written yourself.

There are a few cases when creating a custom-made CMS is the proper option to go, but those are really few and far between. With the rule of thumb being that you can't find anything that remotely suits your needs, before you start to contemplate on such an action.

Link to comment
Share on other sites

What I actually meant is that I want to make al my file links flexible because I had enough trouble with hardcoded systems.

So what White_lily said was just a good reminder of setting own globals but it wasn`t the answer.

 

I completely understand what you mean Christian it`s just that I`m trying to hard to make my system work exactly like I want it to work.

 

Right now I`m just using the document_root and everything is working fine. So thanks for helping everyone.

 

Ps: Christian I can completely rely myself to the post you linked to but in my case it`s just that I want to understand what every single

line of code does and I`m not jQuery pro so I need to take time for that I don`t really have at this moment.

Link to comment
Share on other sites

A better way to get your document root, assuming that your entrance file is located in that folder, is by using the basedir () and realpath () functions. Then you can just add the following line into your index.php (and any other entrance file in the root folder):

define ('ROOT_DIR', basedir (realpath (__FILE__)).'/');

 

However, this will not give you the document root if the entrance file is stored in another folder. If it is imperative that you get the document root, no matter where the entrance files are located, then you need to use $_SERVER['DOCUMENT_ROOT'].

 

I've yet to come across any servers which didn't have DOCUMENT_ROOT defined, or defined incorrectly. And I've been working with PHP for over 12 years by now.

 

In any case: Good luck on your learning! I'm glad to see that you are indeed doing this for the learning experience. :)

Link to comment
Share on other sites

I've yet to come across any servers which didn't have DOCUMENT_ROOT defined, or defined incorrectly. And I've been working with PHP for over 12 years by now.

 

I used to work on some IIS setups that did not define DOCUMENT_ROOT for some reason, though if I remember correctly, there was a similar variable, just went by a different name.  Since then I've gotten into the habit of always define'ing my own DOCUMENT_ROOT constant based on __FILE__.  I can't remember which IIS version it was that did that.  The newer version we are using now does define it properly.

 

Link to comment
Share on other sites

 

 

I used to work on some IIS setups that did not define DOCUMENT_ROOT for some reason, though if I remember correctly, there was a similar variable, just went by a different name.  Since then I've gotten into the habit of always define'ing my own DOCUMENT_ROOT constant based on __FILE__.  I can't remember which IIS version it was that did that.  The newer version we are using now does define it properly.

 

Not to mention that DOCUMENT_ROOT is defined by the webserver, so it can be a variation of different items, depending on who setup the web server or even be left off at times. So yea, the __FILE__ method is definitely my preference as well. 

Link to comment
Share on other sites

Secondly: There is absolutely nothing wrong with depending upon the $_SERVER variables, ...

 

Actually, some of the elements of the $_SERVER array contain data provided by the browser. Those elements cannot be trusted; i.e. they must be validated and/or escaped, depending on how they are being used. Personally, I wish PHP would split this array out; perhaps: $_SERVER and $_CLIENT or something, so we know which elements come from where. [ PHP Manual - $_SERVER ]

 

 

And, for the record, I ran into one situation where DOCUMENT_ROOT was not defined correctly. While moving a working website from HOST-A to HOST-B, before changing the DNS records, the scripts were loaded into HOST-B so I could test them (using an IP Address since the DNS was not changed). At this time, DOCUMENT_ROOT created all kinds of problems. When I called support, I was told this was because the directory was in a "temporary" location. I never fully understood, or cared, why. At any rate, once we finalized the setup and transferred the DNS, everything was fine. (Yes, HOST-B was a shared hosting server).

Link to comment
Share on other sites

Yeah its mostly for learning experience and not a production environment.

Actually what DavidAM just wrote is what I meant by not trusting $_SERVER but thats the PHP_SELF value that I actually don`t trust.

 

Thanks for some experience story's!

Link to comment
Share on other sites

I'd like to thank too, I was a bit overly broad in my statement about trusting the $_SERVER variable. Against better judgement. As long as you know what you're doing, it's normally not an issue with trusting it. However, as pointed out, unfortunately there are some of the values that's being set/sent by the client. Not too fan of that myself, but as long as you're aware over it they don't pose any real issues.

 

When it comes to not trusting the PHP_SELF value, that's why I wrote this snippet. ;)

Edited by Christian F.
Link to comment
Share on other sites

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.