I have a basic web page setup that is using includes to pull in the header, navigation, sidebar, and footer. The issue I have is that the server I am uploading the files to, doesn’t appear to support "$_SERVER[‘DOCUMENT_ROOT’].
So I have been doing some research and it is suggested that I create my own variable which holds the server root address, so i was wondering how I can go about finding my servers root address?
I also uploaded a file which contains this code:
$root = getenv("DOCUMENT_ROOT") ;
Echo $root;
This wont return the address either. So being new to PHP I feel a little stuck now, and I wondered if anyone could offer any suggestions?
Thanks for the reply. I did what you suggested with the phpinfo file, and I can’t seem to find much info there either. I added the same file to another site on a different server, and there is info in like “SITE_HTMLROOT”, “_SERVER[“DOCUMENT_ROOT”]” but this info doesn’t appear to be present on the server that I need it on. Is there anything I should look out for in the phpinfo file?
I need the document_root so I can call in the template files. Each page calls in the header, navigation, sidebar, footer. All of these use
$_SERVER['DOCUMENT_ROOT'] . "inc/footer.php";
I have done some more research and where the servers is a windows server, you can’t just use $_SERVER[‘DOCUMENT_ROOT’] apparently. So I was trying to find the server root, so that I could create my own variable, to use: $docRoot . “inc/footer.php”.
I think that I have found the server root address now, but the back slashes are also the other way around “\” so I have been looking at str_replace to switch them to forward slashes.
So I am taking it, that up until \ est_doc.php that is the root address? And as you can also see the back slashes are the other way around, which I understand is because of the windows server.
Feels like I am going around in circles at the moment.
That has sorted the issue out, and I can now use DOCUMENT_ROOT My only issue, is that it sometimes messes with the URL especially with “-” …Very strange!
Thanks for taking the time to find that link. Really appreciate it!
Well I have tried out that code some more and it appears to be doing some strange things to my documents, so i’m not sure if I am going to use it. I have found this code though:
$localpath=getenv("SCRIPT_NAME");
$absolutepath=realpath($localPath);
// a fix for Windows slashes
$absolutepath=str_replace("\\\\","/",$absolutepath);
$docroot=substr($absolutepath,0,strpos($absolutepath,$localpath));
// as an example of use
include($docroot."/includes/config.php");
At the moment it still isn’t working, but if I echo out the $localpath and $absolutepath then they display the correct paths, but when I echo out the $docroot, to see what it returns I get nothing.
So I wondered if anyone could maybe see an issue with the code above?
Ok. I seem to have this script working now, so I thought I would post the solution that I am using, just incase anyone else has this problem:
<?php
$localpath=getenv("SCRIPT_NAME");
$absolutepath=realpath(basename(getenv("SCRIPT_NAME")));
// a fix for Windows slashes
$absolutepath=str_replace("\\\\","/",$absolutepath);
$docroot=substr($absolutepath,0,strpos($absolutepath,$localpath));
?>
I think you may be making this much more difficult then it needs to be. Have you tried:
include ‘inc/header.php’;
There is really no need for an absolute path if you just need to include files from public_html. As long as your include path contains a ‘.’ then you should be good to go.
If it’s not PHP 5.3+, DIR won’t work, but you can use dirname(FILE) to get the same result (which would be the absolute path to the folder that the file you called it from sits in).
^^^this. Haven’t done PHP in a while, but when last I did things, using FILE was about the only consistent way to get a path. And if you’ve got a fixed known path you can always compute a relative path.
I know, I know… too simple an answer. I run PHP on a windows server all the time without problems with ANY of the $_SERVER values, so it’s likely the server is GARBAGE (though that’s a given with “windows” and “server” in the same sentence). I’d either move to a host that doesn’t suck, or demand it be fixed.
Because if that’s screwed up, christmas only knows what else is boned.