Boojum Posted May 23, 2004 Posted May 23, 2004 I am trying to use a .php include file to replace the menus for my various subdomains as well as the primary domain (each requires a partially different menu). It works all right as long as I use the code <?php include $_SERVER['DOCUMENT_ROOT']."/inc/menu-ho.php"; ?> for example. But if I replace the above with a version including the full URL, e.g., <?php include $_SERVER['DOCUMENT_ROOT']."http://www.squort.com/inc/menu-ho.php"; ?>, I get an error when I try to open the page. Now, this would be no problem if I were just using the primary domain. But with subdomains, everything needs to be coded with absolute URLs, so I'm concerned that this include technique won't work for my subdomains—which constitute the bulk of my site. Is there any way around this problem? Quote
TCH-Rob Posted May 23, 2004 Posted May 23, 2004 Have you tried >include("http://www.squort.com/inc/menu-ho.php"); Quote
Boojum Posted May 23, 2004 Author Posted May 23, 2004 I am assuming that code is to replace the second line, and am trying it now. Please stand by for update .... Quote
TCH-Rob Posted May 23, 2004 Posted May 23, 2004 I am assuming that code is to replace the second line, and am trying it now. Please stand by for update .... Yes, as I understand it $_SERVER['DOCUMENT_ROOT'] is already calling squort.com. By having it in the second example it is calling squort.com/http://www.squort.com/inc/menu-ho.php. Then again I could be wrong, my PHP is a bit rusty. Quote
TCH-Rob Posted May 23, 2004 Posted May 23, 2004 It does appear to work. Thanks, Rob. Really? woooot Bout time I got a PHP question correct. Quote
borfast Posted May 23, 2004 Posted May 23, 2004 (edited) A couple of things to note: 1 - $_SERVER['DOCUMENT_ROOT'] is evaluated into something like /home/yourusername/public_html/ so the first line you wrote would work, just as you said. The resulting path would be something like /home/yourusername/public_html/inc/menu-ho.php and that's the kind of path you should use when including files in PHP. Why? Point 2... 2 - include() is usually used to include separate PHP scripts into the currently running script, meaning you usually want the *PHP code* to be included. But if you include the PHP file via an HTTP URL, then an HTTP request will be made to get the script, which means that the script will be processed by the server before being delivered, resulting in HTML code or something else (whatever the script outputs). It will work just as if you had requested the file yourself. That's why you should always use filesystem paths and not HTTP URLs. Edited May 23, 2004 by TCH-Raul Quote
borfast Posted May 23, 2004 Posted May 23, 2004 Rob, $_SERVER['DOCUMENT_ROOT'] doesn't give you the site URL, it gives you the document root, which means the base directory *in the filesystem* of your site, which is: /home/username/public_html/ In this case it works probably because menu-ho.php contains only HTML code and no PHP code that needs to be parsed along with the script that's including it. Read the explanation above. Quote
TCH-Rob Posted May 23, 2004 Posted May 23, 2004 (edited) Rob, $_SERVER['DOCUMENT_ROOT']doesn't give you the site URL, it gives you the document root, which means the base directory *in the filesystem* of your site, which is:/home/username/public_html/ Raul, I dont know about your site but home/username/public_html is the exact same destination as **** What is the real difference, his example 1 and the option I provided are just different means to the same end are they not? What am I missing? Edit: I do understand the meaning of include, I have files that end in .php that will not run by themselves but when included in another page will parse correctly. Ergo, include that file. But what is the difference between $_SERVER['DOCUMENT_ROOT'] and ****. In the end it is the same thing,no? Edited May 23, 2004 by TCH-Rob Quote
borfast Posted May 24, 2004 Posted May 24, 2004 Rob, the difference is that if you use this: include("http://domain.com/something.php"); PHP will generate an HTTP request for 'http://domain.com/something.php', as if the file is on another server (PHP doesn't know and doesn't care if it's on the same server or not). What this means is that the webserver where the file is, will receive the request and will process it just as if it was a web browser requesting the file (the web server also doesn't care about who's requesting the file), and so it will pass it through PHP and send back the output of the PHP parser. This means that if you have some variables being declared in the included file and you want to use that variable in the main script, you won't be able to do it, because the script has already been parsed. In other words, if you use this method, what you'll be including is not the PHP source code but the result of it being parsed. On the other hand, if you use filesystem paths to include the file, you'll be including the source code and both the main script and the included script will be parsed together. It's like if the include("/home/username/public_html/scrip.php"); is replaced by the source code inside "script.php". Here's test: create three PHP files, bla.php, bla1.php and bla2.php >/* bla.php */ <?php echo "Using filesystem paths:<br/>"; include("bla1.php"); echo $text1; echo "<br />"; echo "Using HTTP URLs:<br/>"; include("http://192.168.0.1/bla2.php"); echo $text2; ?> >/* bla1.php */ <?php $text1 = "Some text inside PHP code, included via a filesystem path...<br />"; ?> >/* bla2.php */ <?php $text2 = "Some text inside PHP code, included via an HTTP URL...<br />"; ?> Place them in your website root and point your browser to bla.php. You will see that the first variable is accessible by the main script but the second one is not. Hope that makes it clear Quote
Boojum Posted May 24, 2004 Author Posted May 24, 2004 Let's see if I've got this. The original code snippet ><?php include $_SERVER['DOCUMENT_ROOT']."/inc/menu-ho.php"; ?> actually resolves to my homepage URL, and by substituting "/menu-sa.php", "/menu-na.php", etc., to conform to the various subdomain menu filenames, I can refer to the correct files even from the respective subdomain pages. The suggestion from Rob, meanwhile, ><?php include("http://www.squort.com/inc/menu-ho.php"); ?> will do the same, as long as there are no variables to parse within the included file. And of course, as you've said, ><?php include $_SERVER['DOCUMENT_ROOT']."http://www.squort.com/inc/menu-ho.php"; ?> will not work because the absolute URL is effectively redundant. So, as a general rule, I am therefore best advised to use the first code snippet, with the respective include file names substituted for "menu-ho.php" as needed. Does this sound correct? Quote
Boojum Posted May 24, 2004 Author Posted May 24, 2004 Arrgh! I just tried this code, ><?php include $_SERVER['DOCUMENT_ROOT']."/inc/menu-sa.php"; ?> in which the menu is an include file under public_html/inc/menu-sa.php, on my Santa Cruz subdomain index page. Result: Warning: main(/home/squortc/public_html/santa-cruz/inc/menu-sa.php): failed to open stream: No such file or directory in /home/squortc/public_html/santa-cruz/index.php on line 118 Warning: main(): Failed opening '/home/squortc/public_html/santa-cruz/inc/menu-sa.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/squortc/public_html/santa-cruz/index.php on line 118 So, now what? Do I try modifying Rob's suggestion, inasmuch as the file includes no variables, or is there some better procedure? Quote
TCH-Bruce Posted May 24, 2004 Posted May 24, 2004 (edited) It's not working because it's not looking at your root folder, when parsing this directive. $_SERVER['DOCUMENT_ROOT'] This will give the path of the page being loaded. In your case from your subdomain. You include file is in the "inc" folder of your "public_html" folder but the path to your actual document calling it is: /home/squortc/public_html/santa-cruz Rob's solution will work. I don't know if there is a way to get just the root parsed. Someone with more PHP knowledge will have to help. Edited May 24, 2004 by TCH-Bruce Quote
Boojum Posted May 24, 2004 Author Posted May 24, 2004 As a practical matter, Rob's solution appears to work, and I haven't heard back from Raul yet, so I'm implementing it. I just can't afford to get bogged down in yet another technical issue. Perhaps I'll revisit this should I ever include variables in my include files. Quote
TCH-Don Posted May 24, 2004 Posted May 24, 2004 (edited) Have you tried >include($DOCUMENT_ROOT . '/inc/menu-sa.php' ); this works for me. Edited May 24, 2004 by TCH-Don Quote
MikeJ Posted May 24, 2004 Posted May 24, 2004 (edited) As a practical matter, Rob's solution appears to work, and I haven't heard back from Raul yet, so I'm implementing it. FYI, Bruce's statement is correct. $_SERVER['DOCUMENT_ROOT'] returns the document root based on the site you are requesting, which in your case because you are using a subdomain, is "/home/squortc/public_html/santa-cruz", therefore the include fails because that directory doesn't have the inc subdirectory. Since your included files are not in your subdomain, you can use the include like you have done so (an external call using absolute domain name as Rob suggested), or if you want an abosolute path, but still flexible by using a variable, the following should effectively work: ><?php include $_SERVER['DOCUMENT_ROOT']."/../inc/menu-sa.php"; ?> The .. tells the path to go up one directory from your subdomain's document root to find the inc directory. Therefore that include should work for any subdomain you make off that primary domain. Edited May 24, 2004 by TCH-MikeJ Quote
borfast Posted May 24, 2004 Posted May 24, 2004 (edited) Brian, $_SERVER['DOCUMENT_ROOT'] does not return an URL, it returns the document root based on the site you are requesting, just as Mike said above. Try this PHP snippet to see what we mean: ><?php echo $_SERVER['DOCUMENT_ROOT'] ?> That will show you what $_SERVER['DOCUMENT_ROOT'] returns and will help you understand why your code isn't working (notice the paths you're using and compare them with this). Aside from that, Mike and Bruce have already explained everything. By the way, Don, $DOCUMENT_ROOT will only work on servers that have the register_globals configuration directive set to 1. If register_globals is off, $DOCUMENT_ROOT will not work. Since the PHP default is to have register_globals off, it's safer to use the super-global variables, like $_SERVER['DOCUMENT_ROOT'] instead of just $DOCUMENT_ROOT. Edited May 24, 2004 by TCH-Raul Quote
TCH-Don Posted May 24, 2004 Posted May 24, 2004 Thanks Raul, I have modified my template to ><?php // add header and additional title $page_title="- blank template"; include $_SERVER['DOCUMENT_ROOT']."/inc/header.php"; // begin main content ?> <P ALIGN="left"> normal content </P> <?php // end main content // add footer file include $_SERVER['DOCUMENT_ROOT']."/inc/footer.php"; ?> and it works just fine, even from a sub-folder. Thanks again Thumbs Up Quote
borfast Posted May 24, 2004 Posted May 24, 2004 No problem I prefer to do it that way because I'm sure that if for some reason the server where my script is had register_globals set to 1 but suddenly the admins set it to 0, my script will still work. Or if I move it to another server that has register_globals set to 0 I know I won't need to change anything to make it work Quote
Z3NiTH Posted July 6, 2004 Posted July 6, 2004 Had a problem regaring the parsing of some variables to a piece of PHP that I included in another one. This problem was also solved using the >include $_SERVER['DOCUMENT_ROOT']."/dir/piece.php"; format. Thanks! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.