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.

what are you using as your server (e.g wamp,easyphp ect.)

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’;
?>
Member Avatar for diafol

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.

haha. no prob. with all of that you typed/copied, i can tell that took time!

Member Avatar for diafol

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.

You typed all that?! :-O

damn, yer crazy! hahah. or i'm lazy

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

commented: 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. +3
Member Avatar for diafol

Thanks AS, I'll give 'virtual hosts' a look - thought it might be something to do with fiddling Apache.

//EDIT

Just googled the thing:

http://apptools.com/phptools/virtualhost.php

I have just sorted out the biggest blasted pain to afflict me over the last 2 years of web dev! AS - you beauty!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.