I am using a 404 error page found at:
/error_pages/404.php
I have a regular page template used for displaying all kinds of messages to the user at:
/message.php
My error page looks like this:
require_once("../includes/core.php");
$message = "Sorry but we could no longer find the item that you were looking for";
$messageStart = "Not Found";
require_once("../message.php");
I have an admin page found at:
/admin
Now my problem is the CSS files and JS files at message.php. If the 404 happened at document root, all links are resolved to their proper locations and all files are included in the HTML. If the 404 happened at /admin, all links are resolved as if message.php is found at /admin/message.php!
I already tried using a constant like PATH_TO_ROOT which is defined per page, or it is defined by core.php depending on the value of $_SERVER[“PHP_SELF”].
Now the problem in the above approach is that $_SERVER[“PHP_SELF”] always contains /error_pages/404.php thus PATH_TO_ROOT will always contain '../' and will mess-up all 404’s happening at document root.
If I define PATH_TO_ROOT within message.php itself, then all 404’s in /admin gets messed up.
How do I solve this?
EDIT:
I tried using $_SERVER[“REQUEST_URI”]. The CSS and JS files are now properly included. The problem is the PHP includes. All includes are resolved from /error_pages where 404.php is located. These PHP includes do use PATH_TO_ROOT so a PATH_TO_ROOT based on REQUEST_URI is really not that good.
Having done this in my own framework the solution can be a bit aggravating to get to and not well documented by many others on the web. There are really 3 problems you have to account for:
Assuming a few minor details these can all be worked around easily enough. First assumption to make is that your framework will be in a known folder (call it framework) and that it will have a known file name (again, framework.php). Then to find the framework path in your index.php handler you can use:
Then we need to calculate the Site’s installed path.We can do this using the DOCUMENT_ROOT and the path to the “currently accessed file”. Something similar to the following:
This also provides us the absolute path for the sites PHP files and we can take advantage of the fact that the PHP include system lets us give absolute file paths and not just relative ones for inclusion of user scripts. thisPath is what is used for user script includes and requires. If for example you want to include a file in /path/to/site/user_script.php you can do a:
SitePath can now be used to setup includes related to CSS/JS/etc files. So instead of stating
you say
If your interested I’ve got my current framework to a point where its shareable, eventually I’m going to release it Open Source but I haven’t done the documentation so its a read at your own risk type of thing :). I extended this to use SIMPLE_HTML_DOM_PARSER so it updates the tags in the source HTML for the designer and they don’t have to worry about src, action, href, etc attributes on tags. Sure, it slows down rendering a bit, but it makes the designers lives so much easier 🙂