skip to Main Content

i know my question seems a bit vague but an example will do wonders.
For example :
When you go into a folder you use : <?php include_once 'includes/header.inc.php' ?>
when you want to go back one you use ../../ to go back you get the idea. However is there by chance a better way of doing this cuz i’ve found it’s easy to break?

vague but i hope someone can help, thanks in advance.

I’ve obviously tried the ../ to go back to parent directory or folder. When i google i tend to find how to go back using header etc but i dont think that’ll help me much.

2

Answers


  1. Use your DOCUMENT_ROOT as the starting point for all your includes and requires… That way it will be portable between dev and production as well.

    Here you can read about $_SERVER

    IE

    include_once $_SERVER["DOCUMENT_ROOT"] . '/public/foo/bar/includes/header.inc.php';
    
    Login or Signup to reply.
  2. Rather than basing everything off of the document root, which is not necessarily the root of your actual project, nor is it populated outside of an HTTP context, I generally do the following.

    In a config file for which you definitely know the location relative to your application’s root:

    define('APP_ROOT', realpath(__DIR__ . '/../path/to/dir'));
    // will result in something like: /var/www/somesite/path/to/dir
    

    and, for good measure:

    define('WEB_ROOT', 'https://example.com/my/app/');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search