![]() |
Need help with SSI paths in php
I just migrated to a new hosting account. Almost all of my files are in php. In my last hosting account, to create a server side include, I used something like:
PHP Code:
PHP Code:
PHP Code:
|
These aren't actually SSI's. These are inclusions which effectively assemble the parts from smaller parts. SSI's are interpreted during the serve, while the includes are interpreted during the request. The virtual() function can be used to create actual SSI's if for whatever reason you need them.
I do not recommend simply using a flat path unless its taken well into control. Inclusions and requires are always based off of the executing file's working directory, regardless of the depth of the files used for including. For this reason, only your first example is the proper usable one. You can get around this by setting include paths (using set_include_path and get_include_path to append). This lets you search a directory for these paths and then include. It is based on order it is found. If you like the relative, always go relative to this file. So in that second example, it would be require_once __DIR__ . '/../includes/header.php';, or dirname(__FILE__) if you're on an older version of PHP. Unless there is a particular need for it, don't use a regular require or include, always use the _once function to avoid stomping created variables, and avoid fatal errors on already declared functions, constants, classes, etc. Prior to this, we would use a defined() check, but that is no longer necessary. Also, you'll very rarely need an actual include; mostly you are constructing out of other pieces, so these are typically required. |
Fou-Lu,
Thank you for your thorough response. When I use the first example, I receive this error message: Code:
Warning: require_once(/usr/local/apache/htdocs/includes/header.php) [function.require-once]: failed to open stream: No such file or directory in /home/mysitename/public_html/index.php on line 25Any ideas on how to modify this to get it working properly? You mentioned this is the proper method and I prefer it much more than using relative paths. Thank you for any suggestions. |
No it sure isn't working. They've managed to do something to rewrite it. DOCUMENT_ROOT should be virtually mapped to you, but it clearly isn't:
Code:
/usr/local/apache/htdocs/includes/header.phpDo the relative to this directory approach. Overall its far more useful anyways since you can use it in cron jobs while DOCUMENT_ROOT cannot be (unless you manually set it). PHP Code:
|
I talked to the hosting account techs and although they are a great company, they let me know setting the document root functionality is not natively supported in their cPanel at this time.
I have a couple thousand SS includes (or whatever they technically are) that I will have to edit in the site, and I am trying to avoid using relative paths for this reason - this seems as if will be very time consuming Just to make sure I understand, the following method does work when I test it, but you are suggesting that I do not use this method? PHP Code:
|
You should never use a relative path alone since their paths are resolved to the current working directory which is always the executing script. Doing so will mean you cannot change the nesting. If you include this file in a file that's two directories below it, it will attempt to load includes off of the directory two below. Using __DIR__ resolves this as it will always be relative to this script, not the executing script.
|
Upon testing, this example seems to work:
PHP Code:
|
I think I understand..if the file is in the root, it should be:
PHP Code:
PHP Code:
PHP Code:
Please confirm this is correct and thank you. |
By deep, you mean this file right?
Then yep that's exactly what that is. That second one would take this file, and move up two directories to find includes/header.php. The difference in this route is (as mentioned) the use of inclusion nesting: Code:
/require_once '../functions/generic.php';. However, if you include header.php into index.php, then it will throw a fatal error since it cannot find /../functions/generic.php. This is why if you use a relative path you should make it relative from this file, not from an unknown working directory. By putting __DIR__ or dirname(__FILE__) on older PHP versions gets around this. Now when you include header, it will always look a directory up from the header file, not from the index file. |
thank you very much - now it's off to work to change quite a few of these
|
| All times are GMT +1. The time now is 02:04 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.