包含网站目录的文件 - 制作导航始终包含connect.php,无论子目录是什么[重复]

This question already has an answer here:

I've been working on a small site, to work on my PHP Skills. I've got the navigation inside of a sub-folder called Templates. Now basically, the navigation is going to be included on every page, and along with it connect.php. Now the problem is, if it goes into another sub-folder then it isn't going to be able to find the connect.php file. I've got the APP Directory setup, and that is working on the Stylesheet and all.

However I'm not sure how I would do the include, I got the idea/code for the link href from StackOverflow on another question and on that it said that include (APP_DIR .'connect.php') however that doesn't seem to work for me, it brings up an error "Warning: include(APP_ROOT/connect.php): failed to open stream: No such file or directory in C:\xampp\htdocs\ytsite\templates avigation.php on line 2 "

Any help on how to correctly include a file so no matter where it is, it includes the connect would be much appreciated.

Thanks, Ben

</div>

Your constant should be a directory path, not a url. Try this with your constant definition..

define('APP_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/ytsite' );

echo APP_ROOT; //to test

Not tested. Beware you don't get double slashes and make sure the path goes to the correct directory. Also make sure you add a slash when you include 'connect.php'. Include it in the constant definition, or add it to your include line.

include (APP_ROOT .'/connect.php');

edit

So include your connect file like this:

Make a master file to include into your pages and add these lines.

<?php
    define('APP_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/ytsite' );
    include (APP_ROOT .'/connect.php');
?>

Every time you launch a new page, the constant needs to be recreated.