PHP include path/document root help

I’ve spent the last hour searching the forums for something that will help me, and I couldn’t find the exact thing I was looking for…

I’m a complete PHP beginner, with no coding background to speak of, although I know HTML/XHTML/CSS thoroughly. I managed to use the require () statement(? - it IS a statement, right?) on my personal website to include my menu, using this code:

<?php require ("sitenav.txt"); ?>

It took some trial and error for me to figure it out, but I finally got it working.

Now, I’m trying to do the same thing on another website I work on that’s hosted with a different hosting company…the menu should go in one of the table cells. It didn’t work to use the same code (that cell is just blank), so I started searching around. The company’s knowledge base was less than helpful, but I did manage to figure out that one of their users was using this code successfully (I switched to the include statement, since there seemed to be more info on it):

<?php include($DOCUMENT_ROOT."/php/fileHere.txt"); ?>

They did note that the host was running PHP 4.2.1, so that this would be better:

<?php include($_SERVER['document_root']."/php/fileHere.txt"); ?>

So, I’ve tried both of those. I created a “php” folder in my root directory (um, I think…that’s just the main directory under my domain name, correct? The place where my index.html file is kept?) and put my file (named menu.txt) in there. I’ve also tried putting “menu.txt” in my root directory and removing the “/php” from the path in the include statement.

I think part of the problem might be my total ignorance of the “$_SERVER” and “$DOCUMENT_ROOT” variables…I’ve looked in about 50 million places on the net, including php.net, phpb.com, multiple message boards, and I can’t find out if I need to literally put something like this:

<?php include($_SERVER['document_root']."/php/menu.txt"); ?>

or if " ‘document_root’ " needs to be replaced with the path to my document root (I installed a phpinfo.php file and know what the path is)? I’m so freakin’ confused, and it’s just not clicking in my brain. Am I just being stupid, or is there something I’m not doing that will make my menu show up? I’ve been reluctant to post my problem, because I hate showing how ignorant I am, but I’ve been working on this most of the day with nothing to show for it but a headache. Php.net is overwhelming and pretty much greek to me (even the introductory tutorial starts to make my head hurt after a while), and other sites I’ve been on address the include statement but not the include path issues I’m having (or think I’m having). I feel like there’s something really simple I’m just not seeing, and it’s frustrating the hell out of me. :frowning:

Any help is greatly appreciated… :confused:

Why don’t you nix the docroot stuff.


<?php
include('../php/menu.txt');
?>

Well, I did try that…

I tried:

include('../php/menu.txt'); 

just like you had, and I also tried:

include('/php/menu.txt'); 

and

include('php/menu.txt'); 

all to no avail…I don’t know if it’s the way my host has their servers set up, because quite frankly, I know about as much on monkey reproduction as I do about server stuff. And my knowledge on PHP is not much better than that (which I’m sure is painfully obvious…). Am I doing something wrong?

are you getting any erros using those functions?

Nope, no errors…I’m just getting a big blank space where the menu should be. If I look at view source, it’s still showing my php, too, rather than the .txt file I’m trying to call. Does that mean it’s even parsing the php?

I doubt it will help, but the site is www.dean-armstrong.com, and the page where I’m testing is http://www.dean-armstrong.com/bio.html. I remember when I added the require function to my personal site I had the same problem for a while, but I think it was that initially I just didn’t understand what I was doing. Of course, I can’t remember what I did to arrive at what I have now on that site, so it’s really not any help.

If anyone does look at the site, I’ll include my usual disclaimer…I didn’t design it or do most of the coding on it, so I doubt it’s remotely close to standards compliant. I’m trying to turn my partner onto that, though, and expect to use XHMTL transitional when we do a complete redesign later this year.

Well this is the thing. If the path was wrong, you’d get an error. Try it. Change a letter in the path/to/include just to see what I mean.

So the fact that it is being included means that something is wrong with your file…

At least that’s my diagnosis…

FWIW, I get a 404 on the link…

Aaron

God, I feel like I’m being so difficult, and I appreciate your patience a lot. :slight_smile:

I just tried to change the path a little (I put “menu2.txt” instead of “menu.txt”), and still no error. So I KNOW I’m doing something wrong. I changed it back to what I had before, and I fixed the link in the post above if you or anyone else wants to look. I think I’m going to give up on it for tonight and mess with it tomorrow when I’m not so frustrated. I put in a post on the webhost’s board about the issue, so I might get somewhere with that, too.

Soooo, if you see anything obviously stupid I’m doing, great, and if not, I’ll just work on it tomorrow. Thanks again for your help!

You have to use a .php extension.

No you don’t.

http://www.emmense.com/test.php

No, she has to use a PHP extension on the file that’s including, not being included. Right now it’s .html. So unless she uses .htaccess to force type it, she’ll need to use .php.

then the include would display in plain text, mike. as in, her file would show

<?php include(whatever); ?> in plaintext on the browser. Currently, there is nothing.

Hi,
you should use:

$_SERVER[‘DOCUMENT_ROOT’]

the array key is case senstive.

:slight_smile: Andrea

Ok, I see what you mean now. Yeah it is .html (I DID miss that) and the php is actually displayed in the source code.

Laura, these pages have to have a .php file in order for PHP to use them. There are ways to use .html, but it’s probably more trouble than it’s worth…

Hey thanks for looking at this! I had a friend with some PHP experience take mercy on me as well, and she told me what to put into my .htaccess file to get the PHP to parse in the HTML file.

I KNEW this would be something disgustingly easy, but hey, I ENJOY looking foolish. :wink:

That leads me to a more general question, however…is there any real value of keeping the site as HTML over PHP? I kept my personal site HTML because I wanted it to validate, and I didn’t know if php pages could be validated, but I’m just wondering at what level of PHP use it makes sense to just have PHP pages…is the little code snippet worth it? Any advice?

Thanks again…you guys always rock!

ETA:

One more thing…what’s the better code to use:

<?php include $_SERVER[‘DOCUMENT_ROOT’].‘/php/header.php’; ?>

or

<?php include (“php/menu.txt” ); ?>

I’m just trying to take this opportunity to learn a little something, so any insight is appreciated.

php files will validate too. :slight_smile:

The second one is beter. Less for php to parse.

For the record, it wouldn’t display it as plain text because it consideres it to be an unknown tag, therefor ignoring it. At least in IE. :wink: