P0oltj3 Posted January 14, 2013 Share Posted January 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/ Share on other sites More sharing options...
MDCode Posted January 14, 2013 Share Posted January 14, 2013 I don't believe there is an alternative for the matter. (Although someone correct me if I'm wrong). If you really are so upset by it, you may have to write it out in some configurations file. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1405577 Share on other sites More sharing options...
P0oltj3 Posted January 14, 2013 Author Share Posted January 14, 2013 Right now I`m trying to convert my structure so that I wouldn't have to use $_SERVER['DOCUMENT_ROOT'] or a alternative. So something that your suggesting. Thanks for fast answer. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1405579 Share on other sites More sharing options...
The Letter E Posted January 15, 2013 Share Posted January 15, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1405746 Share on other sites More sharing options...
P0oltj3 Posted January 15, 2013 Author Share Posted January 15, 2013 I have read that $_SERVER values can be changed by the server settings. So if my webserver would be wrongly set up in that particular part it could be that certain parts of the application would just go white. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1405795 Share on other sites More sharing options...
White_Lily Posted January 15, 2013 Share Posted January 15, 2013 (edited) you could type out the value of $_SERVER["DOCUMENT_ROOT"] into a $GLOBALS[""] variable? eg: <?php $GLOBALS["file_root"] = "/Projects/cms/5113/htdocs2"; ?> this way only you can change and nothing else Edited January 15, 2013 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1405895 Share on other sites More sharing options...
P0oltj3 Posted January 15, 2013 Author Share Posted January 15, 2013 oh yeah I totally forgot about setting own globals thank you for reminding! I need to get my head back in. I`ll search for dos and cons for that. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1405905 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 *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. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406075 Share on other sites More sharing options...
P0oltj3 Posted January 16, 2013 Author Share Posted January 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406078 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406091 Share on other sites More sharing options...
kicken Posted January 16, 2013 Share Posted January 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406143 Share on other sites More sharing options...
premiso Posted January 16, 2013 Share Posted January 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406145 Share on other sites More sharing options...
DavidAM Posted January 16, 2013 Share Posted January 16, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406167 Share on other sites More sharing options...
P0oltj3 Posted January 17, 2013 Author Share Posted January 17, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406451 Share on other sites More sharing options...
Christian F. Posted January 17, 2013 Share Posted January 17, 2013 (edited) 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 January 17, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406454 Share on other sites More sharing options...
P0oltj3 Posted January 18, 2013 Author Share Posted January 18, 2013 Thanks for your snippet I`ll be sure to use it when I`ll have to use PHP_SELF. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406642 Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 You're welcome. Glad I could be of help. Quote Link to comment https://forums.phpfreaks.com/topic/273143-alternative-for-_serverdocument_root/#findComment-1406709 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.