I'm somewhat new to PHP. I'm not sure how to point to the root of my web site in a function. I was using $_SERVER["DOCUMENT_ROOT"], but when I use it in my function it comes back:
"http://mywebsite.com/var/www/html/mypath"
Note - this is in the HTML - other parts of the function seem to use the path, but when it is used for a link or as the src for an image, it adds the "var/www/html/".

function myFunction($path = "/mypath/") {
	$path_to_be = $_SERVER["DOCUMENT_ROOT"];  
	$first_path = $path_to_be.$path;
}

I tried using $_SERVER["HTTP_HOST"], but that doesn't seem to work either.

What is it that I do not understand? Thanks for any help.

I'm using Apache on my laptop with windows for testing and there is an older version of Apache on the web server that I publish to. Would it make more sense to set a global variable in a library file with the URL root location and then just call it in from there? And then if it changed I'd just change it there?

Thanks!

I'm somewhat new to PHP. I'm not sure how to point to the root of my web site in a function. I was using $_SERVER["DOCUMENT_ROOT"], but when I use it in my function it comes back:
"http://mywebsite.com/var/www/html/mypath"
Note - this is in the HTML - other parts of the function seem to use the path, but when it is used for a link or as the src for an image, it adds the "var/www/html/".

function myFunction($path = "/mypath/") {
	$path_to_be = $_SERVER["DOCUMENT_ROOT"];  
	$first_path = $path_to_be.$path;
}

I tried using $_SERVER["HTTP_HOST"], but that doesn't seem to work either.

What is it that I do not understand? Thanks for any help.

Maybe I'm not understanding the question, but if you are trying to output some HTML, you can always use the relative "/".


example

<?php
echo "<a href = \"/mylinks.php\">Cool links</a>";
echo "<img src = \"/images/smile.gif\" >";
?>

also

<?php
echo $_SERVER["SITE_HTMLROOT"].’/images/smilegif’;
?>

Use $_SERVER with php functions, such as include files, some php file actions (where you can access above the public html directory).

include files when accessing absolutely, that is starting with '/', will look in the document root (the topmost level of your site - not to be confused with the public root), so using $_SERVER can be really useful for these as you can place your page files anywhere and still have the same include statements work. Otherwise, if you use relative paths, you may have to change them if you move the page files up or down the directory hierarchy.

so, this should work no matter where the page files reside (assuming your include files reside in the 'my_includes' folder):

include($_SERVER['DOCUMENT_ROOT'] . '/my_includes/config.php');

The public_html (or 'html' directory in your case) is the directory which should be used as your 'normal root' directory for link, script and image tags. These cannot access (as far as I know) directories above 'html'.

If you make all paths absolute (e.g. '/css/main.css' instead of something like the relative '../../css/main.css'). This should obviate the need for a function. Again you can move your page file anywhere in the folder hierarchy and the links (src and href attributes) should hold true.

I should point out that this is a real pain if you are using 'localhost/mysite/' for local testing. For this, your html directory will be '/mysite/' instead of the usual remote server '/'.

I use Dreamweaver/XAMPP and hit this problem time after time. I still don't know how to work around it. Anyone?


*****SORRY 'K' _ YOU MUST HAVE POSTED WHILE I WAS ON A MISSION! *****

BTW: What's this: $_SERVER["SITE_HTMLROOT"] ?

Haven't seen that before.

hey cheeky - it was typed! Mind you after a few beers, I don't know if it makes much sense. Had to go over it a few times.

THanks for the tips. I'm getting that I can use $_SERVER["DOCUMENT_ROOT"] for includes, requires, and within functions to point to directories, but if I'm in a function within PHP and writing out the URL for say an image or link, I should NOT use $_SERVER["DOCUMENT_ROOT"].

I was trying to make the site so that i could move it from dir to dir without having to change paths in pages like you mentioned. (I'm writing out directories and files and trying to make them links in the HTML.) I'm trying the root of the server option ("/directory/file.php") when writing out HTML src links and then adding a variable called $home_directory (in a separate library file) and adding that to the front of paths.

Anyway, thanks for the clarification.

I should point out that this is a real pain if you are using 'localhost/mysite/' for local testing. For this, your html directory will be '/mysite/' instead of the usual remote server '/'.

I use Dreamweaver/XAMPP and hit this problem time after time. I still don't know how to work around it. Anyone?

If I understand correctly, the solution lies in your Apache configuration. I had the same problem until I learned how to set up virtual hosts (and then how to compose urls in the corresponding format).

Airshow

Comments
Thanks so much - can use same doc_root and html root local and remote - didn't have a clue what to do until your post.
This question has already been answered. Start a new discussion instead.