$ _SERVER ['document_root'] returns / htdocs not / public_html

I want to switch to a new host, and they provide this little "temporary url" to check your files before you switch. All beautiful and dandy. Therefore, I copy all my files. At the top of each page, I require another file from the server, which is stored in public_html/includes/head.php . Now, for some reason, the $ _SERVER ['document_root'] var parameter returns / public _html / htdocs / includes / head.php (which does not exist on the server), rather than / public _html / includes / head.php (which exists) The error received is as follows:

 Warning: require_once(/home/secure31/public_html/htdocs/includes/head.php) [function.require-once]: failed to open stream: Permission denied in /home/stephe80/public_html/index.php on line 6 

Wine Code:

 require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/head.php'); 

I tend to think that this is an error related to their temporary url setting, but I do not want to pass it through my DNS and find out that all my files are corrupted. I could change them to absolute paths, but I am curious if I am missing something. Any insight would be appreciated. Thanks!

+4
source share
2 answers

For future reference (if anyone has similar problems), it turns out that the problem was related to their test server. I ran it with my code as it was (risky, I know), and everything worked out well.

0
source

I suggest using the __FILE__ constant and from there work out your htdocs folder. This is much more reliable than relying on the $ _SERVER supergalbal, since it actually differs from server to server.

Example (which you call in your index.php in the root folder):

 $htdocs = str_replace(basename(__FILE__), __FILE__, ''); define('ROOT_FOLDER', $htdocs); require_once(ROOT_FOLDER . '/includes/head.php'); 
+4
source

Source: https://habr.com/ru/post/1438278/


All Articles