Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:41:14+00:00 2026-05-24T08:41:14+00:00

I am using a 404 error page found at: /error_pages/404.php I have a regular

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:41:15+00:00Added an answer on May 24, 2026 at 8:41 am

    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:

    • Where is the WEB root and where is your engine/framework code located accordingly
    • Where is the PHP files root for your engine/framework and includes/requires accordingly
    • Calculation of proper pathing for CSS/JS/Images/etc…

    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:

    // in index.php
    $frameworkPath = './';
    while((is_dir($frameworkPath))&&(!file_exists($frameworkPath.'framework/framework.php'))){
      $frameworkPath = ($frameworkPath!='./')?'../'.$frameworkPath:'../';
    }
    if(!file_exists($frameworkPath.'framework/framework.php')){
    // some type of error handler here
    }else require_once($frameworkPath.'framework/framework.php');
    
    // in framework.php
    if(!defined('FRAMEWORK_PATH')){
      $frameworkpath = str_replace("\\", '/', dirname(__file__));
      if(substr($frameworkpath, -1)!='/') $frameworkpath .= '/';
      define('FRAMEWORK_PATH', $frameworkpath);
    }
    // now your requires become require_once(FRAMEWORK_PATH.'filename/relative/framework.php');
    

    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:

    $serverPath = $_SERVER['DOCUMENT_ROOT'];
    $thisPath = @realpath(dirname('./'));
    $thisPathLength = strlen($thisPath);
    $SitePath = '/'.str_replace('\\','/', substr($thisPath, strlen($serverPath), $thisPathLength)).'/';
    

    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:

    require_once($thisPath.'user_script.php');
    

    SitePath can now be used to setup includes related to CSS/JS/etc files. So instead of stating

    <img src="img.jpg" />
    

    you say

    <img src="<?php echo $SitePath;?>img.jpg" />.
    

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website which uses the custom 404 error handling in PHP/Apache to
I have a call to a 404 page in a controller: $this->set('title','Page Not Found');
Up till now we've been rewriting URL's using a custon 404 page: the url
I am using php/ajax to submit a form without page refresh. Here are my
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
I have Joomla 1.5.9 running with php 5.2.8, mySQL 5.1.31 on IIS7 on a
I have a Spring MVC/3.0 app using tiles as it's view, this is working
I'm using Ubuntu 10, python 2.6.5 I'm following this tutorial: http://www.djangobook.com/en/2.0/chapter02 I followed all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.