Finding my server root address?

Hi Everyone

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 any help.

Start by making a one line php program called info.php containing

<?php phpinfo(); ?>

That will list all the variables that your server does support. Hopefully, one of them will contain what you need.

PHP: phpinfo - Manual

Hi ahundiak

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?

Thanks again

Also as some extra info, it appears that the site is hosted on a Windows server. Does that change the way that you can use $_SERVER[‘DOCUMENT_ROOT’]?

Thanks

Well, then plan B is to determine what you think you need document root for. I know I have never needed it. What exactly are you trying to do?

Is there a value in the phpinfo output for “Server Root”?

Hi ahundiak

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.

Any ideas where I can go from here? :frowning:

Thanks

Hi SpacePhoenix

No there is no mention of “Server Root” in the phpinfo. I used this piece of PHP code

<?php
 print realpath(basename(getenv("SCRIPT_NAME")));
?>

Which returns (I have changed this address for obvious reasons):

\\172.18.201.201\webvol\vhc59g9\mydomain\public_html\ est_doc.php

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.

Thanks for any help you can offer.

Here’s another workaround that google turned up

Fyneworks: PHP DOCUMENT_ROOT in IIS (Windows servers)

Hi Doug

That has sorted the issue out, and I can now use DOCUMENT_ROOT :slight_smile: 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! :slight_smile:

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?

Thanks again for everyone’s help.

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));
?>

Thanks again for the suggestions.

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.

You might also see if FILE or DIR work.

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.

following can be helpful

  1. Create a page i.e. root-test.php

  2. Add the following line of code to the page:

Remove the spaces appropriately from around the question marks:

< ? = $_SERVER[‘DOCUMENT_ROOT’]; ? >

  1. Upload the file.

  2. Then go to the file online i.e. http://www.yourdomain.com/root-test.php

You missed the original problem, which is that on an IIS server the DOCUMENT_ROOT server variable doesn’t exist :slight_smile:

May be the below code, might give your root address

<?php
$s = empty($_SERVER[“HTTPS”]) ? ‘’ : ($_SERVER[“HTTPS”] == “on”) ? “s” : “”;
$protocol = substr(strtolower($_SERVER[“SERVER_PROTOCOL”]), 0, strpos($_SERVER[“SERVER_PROTOCOL”], ‘/’)).$s;
$port = ($_SERVER[“SERVER_PORT”] == “80”) ? “” : (“:”.$_SERVER[“SERVER_PORT”]);
echo $protocol.“://”.$_SERVER[‘SERVER_NAME’].$port . ‘/’;
?>

My advice? FIX THE SERVER!

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.

Personally, I use PuTTY for access the server root. You can download from here. Small size of tool which work without installation in your computer.