PHP & include_path

software development

#1

Hi! New to DH, and have transferred a site from another provider, but obviously some of the PHP and Apache settings are different from what I originally had.

I used to be able to put all my PHP scripts in one location ("/php", such that http://www.mysite.com/php), and get them in my pages via <?php include '/php/header.php'; ?> (for example). It appears that on DH this requires some manipulation of the include_path variable? Any suggestions on how to setup shared headers, footers, etc., so they are available from any directory within my site?


#2

First off here’s a link to DreamHost’s current version, which is 4.2.1 (just updated)

Here is an example of how I have it working:

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

Keep in mind I’m just inserting text files, not sure if php files would work the same. $DOCUMENT_ROOT is the main web directory (/home/user/yourdomain.com/ or http://yourdomain.com/). But, according to the latest version, you should replace $DOCUMENT_ROOT with $_SERVER[‘DOCUMENT_ROOT’] So you should make that simple change and it should work.


Todd Eddy
vrillusions.com


#3

Todd,

Thanks for responding. Your example is almost exactly the solution I came up with – which is probably better programming practice in the first place… I’m not sure how my original ISP had their PHP setup (apparently, not the most secure way, since they no longer support it, which was the reason for my move to DH in the first place!).

$DOCUMENT_ROOT is still supported apparently, as that’s what I’m using with no problems. Have you seen anything that explicitly says one should use $_SERVER[‘DOCUMENT_ROOT’] instead?


#4

yeah, if you click on the 4.2.1 on my first post, it’s a link to all the changes. You’re right, $DOCUMENT_ROOT will work, but it’s officially depreciated. Also DH did some reconfig because I guess that stuff is actually disabled by default now. So althought $DOCUMENT_ROOT works now, who knows how much longer it will. So I would probably try to get in the habit of using $_SERVER[‘DOCUMENT_ROOT’] in any new pages and change the old pages when you get the chance. I guess it all depends on how many users still use the $DOCUMENT_ROOT durring each upgrade that will determine if the DH people will keep enabling it…


Todd Eddy
vrillusions.com


#5

I’m interested if you all have tried changing the include_path in a .htaccess file. I usually put all my include files outside of the web root (for instance ~me/inc instead of ~me/mydomain.com/inc). I can’t try it yet, as my DNS hasn’t propagated for my domains (I’m very new to DH); have any of you tried it?


#6

Using include_path will work!

The syntax in an .htaccess file is:

php_value include_path /home/youruser/some/place:/home/youruser/other/thing

Remember, .htaccess files work recursively!

nate.


#7

I’m resurrecting this thread because it deals directly with my question, and I’ve spent most of the day searching 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 a website I work on that’s hosted by DreamHost. I found this thread, but when I put

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

(changing, of course, “fileHere.txt” to my file name, in this case “menu.txt”), nothing happens…it’s just blank where my menu is supposed to show up.

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

<?php include($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.

Any help is greatly appreciated…


#8

I’m not the biggest PHP person either but both of these:

[code]<?php include $DOCUMENT_ROOT.'/php/header.php'; ?>

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

[/code]work on the site where I use them…

You might try looking in the output from phpinfo() again, and see what the register_globals directive is set to; it’s set to On on my site, and as I understand it, that directive is what determines whether variables such as $_SERVER[‘DOCUMENT_ROOT’] can be accessed through their shorter counterpart (e.g., $DOCUMENT_ROOT)…

For what it’s worth, it’s better programming practice to use the $_SERVER array anyway – see http://www.php.net/register_globals and http://www.php.net/manual/en/language.variables.predefined.php for the reasons why…


#9

A few suggestions, just for debugging purposes:

  1. Try hardcoding the path to your include file, instead of using the $_SERVER array.
    Like,

include ("/home/yourusername/includes/myfile.txt");

  1. Try hardcoding your path with http://
    Like,

include (“http://mydomainname.com/includes/myfile.txt”);

  1. Rename your file with a .pcgi extenstion instead of a php extension. (Other can explain, but this helps when you need access to files outside of your web directory.)

  2. Change your .htaccess file to include your include path. I think the method for doing this is spelled out in a previous post.

One of these things should work, so at least you can get your site working and then figure out what the problem with $_SERVER[DOCUMENT_ROOT] is later.

  1. Also, try using http:// to access your file directly via your browser. Does anything show up? Maybe there’s something in your file (like <? tags) that are messing with its output.

Good luck!