Working through problems is always fun, but sometimes you get a bit stuck as to the best way to go about things, and today that person is me. You see the problem is I'm not sure how to link everything together on my site. I'd really like to be able to be able to chuck it on any domain and it will work after a quick database connection with no problems. At the moment it's not doing that in two respects. Firstly when I move the ' <?php require_once 'requires.php';?>' to a header.php file, it has to work whatever location it is in the domain. Also the links too. By any location on the domain I mean it will work the same here: www.url.com as it would here: www.url.com/fish/dish/ So here are the two problems I've got at the moment Index.php Location: www.url.com/testing/index.php File links: <?php require_once 'requires.php';?> Domain links: <p><a href="<?=$_DOCUMENT_ROOT?>/test/admin/">admin page</a></p> If you notice the highlighted /test/. I had to put that in manually because the script only accomodates being installed in the root of the domain (www.url.com). editnews.php Location: www.url.com/testing/admin/editnews.php File Links: <?php require_once '../requires.php';?> ../ - I've been using the web for ages & I'm still not 100% sure what the '..' does. It seems to perform the same task as '$_document_root'. I have thought of a way to make the site work for the links in any directory by using the $_server['php_self'] for links. Then when that has executed use a trim function to remove everything to the right of the last forward slash so www.url.com/test/index.php would become www.url.com/test/ and it would work... But it feels a bit hacky and there must be better functions out there. thanks
the "../" backs it up a directory. So if your requires.php was in your web root directory and you called <?php require_once '../requires.php';?> from a file within you /admin directory, it would go back one directory (to the web root) and look for the file there.
Can you not just use <?php require_once 'http://www.domain.com/requires.php';?> and use absolute urls for all the links and includes?
Using absolute urls wouldn't make it very compatible if for whatever reason the site wasn't accessed by it's main domain or it was installed on each server. I realised me linking in the admin side with php code was silly, as it's always the same. So this does a much better job: <p><a href="admin/">Admin area</a></p> However, making a universal site wide require function whereby it can find requires.php is still out of my reach.
If you are just trying to be able to include a file in your root directory from anywhere on your site, just use this: $filetoinclude = $_SERVER['DOCUMENT_ROOT'] . "/filename.php" ; include($filetoinclude); should work fine... -Erik
Another solution is to use the absolute path to the file. For example: <?php require_once( "/home/MY_DIRECTORY/_MY_FOLDER_/include_this.php" ); ?> Code (markup): I like the idea of putting such files in a directory outside your website -- so that they cannot be accessed from the internet, but so that PHP can access them. This offers improved security. This method also means the location of the file in the website directories has no impact on its ability to include another file. You can take it a step further and put absolute poath name parts in a configuration file so that changing that one entry in that file will propogate across all files. It also means you only have one file to edit if you move to a new computer and forget to and/or cannot duplicate the directory structure.
My quick comments on this... You need to use absolute paths, but you can use relative 'bits' to get to that. Instead of hard coding the whole path, you should use things like: dirname( __FILE__ ) to work out where your current file is (with an absolute path) then from there, add any relative paths to find what you need. If you don't use absolute paths, you're asking for trouble. There was a discussion on the Joomla forums recently as to why you should use absolute paths as opposed to relative paths or URLs for your files. So, instead of arguing it all out again, here's a link: http://forum.joomla.org/index.php/topic,49344.0.html .
Divide your application up into a front end directory and a back end directory. Put the back end somewhere outside of the web root. Put the front end inside the web root. Keep the front end absolutely as small as possible. Something like: <?php set_include_path("/path/to/backend/"); require "start.php"; ?> PHP: Now, all of your require and includes can be relative to your backend location, where ever that may be. You can also put some configuration information in there if you want. These days, In almost every application I write, I have only one .php file in the web root. That file, a front controller, then does all of the dispatching to rest of the php code in the back end. Of course, you also have to dynamically generate all of your URLs to have their locations be relative to whereever you put your front controller. I also tend to have a single media directory which contains all of the javascript, css, and images that the program will reference. All references to these assets have to be generated, too. I also tend to leave the media directory with the back end and alias it into the web root via .htaccess. One advantage of this is that you can install the same application into different domains on the same server by just copying the front controller file, changing any configuration in it, and then aliasing the media directory.
Thanks for your input fellers. The set include path was very useful and the one that I used. It keeps the rest of the site quite relative but the absolute link to the secure directories is a fine compromise