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

  • Home
  • SEARCH
  • 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 8190807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:45:36+00:00 2026-06-07T03:45:36+00:00

Based on some exceptionally helpful tips, I am using the following code to include

  • 0

Based on some exceptionally helpful tips, I am using the following code to include PHP files outside my root directory which looks similar to this:

define('WEB_ROOT', __DIR__);
define('APP_ROOT', dirname(__DIR__));
define('PHP_ROOT', APP_ROOT . DIRECTORY_SEPARATOR . 'application');


include(PHP_ROOT . DIRECTORY_SEPARATOR . 'bootstrap.php');

My question is this, lets say for example you include the code bootstrap.php as per what you have above.

What if that PHP file bootstrap then had its own line of code the included a file BACK in the public_html root folder…. how would one code that? I am having some difficulty doing this, my objective here is that I dont want to put actual literal directories in full in the code and I want to avoid file traversal attacks

  • 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-06-07T03:45:37+00:00Added an answer on June 7, 2026 at 3:45 am

    Consider this project structure:

    /path/to/projectroot/index.php
                         header.php
                         include/inc.php
    

    If index.php had

    include('include/inc.php');
    

    and inc.php had

    include('header.php');
    

    You’d get that error since the line in inc.php would be looking for

    /path/to/projectroot/include/header.php  (doesn't exist)
    

    not

    /path/to/projectroot/header.php (does exist)
    

    There are a few ways people resolve this.

    1: Absolute paths

    The first, and most straightforward is to use absolute paths.

    If index.php had

    include('include/inc.php');
    

    and inc.php had

    include('/path/to/projectroot/header.php');
    

    This would work.

    2: Absolute paths with defines

    Similar to #1, if index.php had

    define('PROJECT_ROOT', '/path/to/projectroot/');
    include(PROJECT_ROOT.'include/inc.php');
    

    and inc.php had

    include(PROJECT_ROOT.'header.php');
    

    This would work.

    Update: As noted in the comments by pichan, you could use one of the “magic” constants here in index.php, so:

    index.php

    define('PROJECT_ROOT', __DIR__.'/');
    include(PROJECT_ROOT.'include/inc.php');
    

    and inc.php

    include(PROJECT_ROOT.'header.php');
    

    Note we add a trailing slash to __DIR__ here since:

    This directory name does not have a trailing slash unless it is the root directory.

    3: Include both and hide errors

    If inc.php had

    @include('header.php');    # Try this directory
    @include('../header.php'); # Try parent directory
    

    This would work.[1]

    4: Assume current directory unless otherwise specified

    If index.php had

    $rel_prefix_to_root = '../';
    include('include/inc.php');
    

    and inc.php had

    if(!isset($rel_path_to_root)){ $rel_path_to_root = ''; }
    include($rel_path_to_root . 'header.php');
    

    This would work.

    My take on these methods

    1 and 2 are basically the same, but 2 is a little bit easier and more common for big projects since it allows you to make one constant definition and use it site-wide. It also allows you to deploy the project on multiple servers (placed in multiple paths) and only requires changing one line project-wide, as opposed to one line in each file for option 1.

    3 is terrible, don’t do it. Sometimes you’ll see it, you might even see it in tutorials online. Don’t do it.

    4 should probably be avoided in favor of 1 or 2. But this approach might be necessary if you have some complex set of includes.

    Some Notes:

    [1] This is a terrible idea. It works, but don’t do it.

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

Sidebar

Related Questions

I have the following requirement: Based on some user input, I need to generate
Hi I write some code to hide and show some based on the selected
I want to toggle a class based on some condition which I have to
This question is based off some really odd code I recently found in a
Imagine an HTML based report which shows some data (for example) of process runs.
I need to disable delete button GLOBALLY based on some condition? The following solutions
Based on some of my previous questions I have put together the following: var
Based on some code in a lecture by Doug Crockford, I've created this. var
I now have this code based on some of the answers below. Is this
I have some conditionals in my InitializeComponent which affect the layout based on some

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.