TaserPain TaserPain - 5 months ago 17
PHP Question

Nested include with PHP

I'm having trouble with nested includes. While I have seen there are a number if similar issues, they don't seem to help.

Generally I don't have problems with includes, but recently I have been trying something new and I cannot get the nested includes to work.

One solution: php nested include behavior

Basic setup:


  • index.php includes '/include/header.php'

  • header.php includes '/resources/login/index_alt.php'

  • index_alt.php includes '/resources/login/index_auth.php'

  • index_auth.php include '/class/Login.class.php' and
    '/class/Connection.class/php'



I do not actually write the paths like this (its to understand the depth).
This is how it will look on the pages.

index.php:


  • include('include/header.php');



header.php: (header is included at every depth level except for /resources/...)


  • include('../resources/login/index_alt.php');



index_alt.php:


  • include('index_auth.php');



index_auth.php:


  • include('../../class/Login.class.php');

  • include('../../class/Connection.class.php');



At some depth levels the header file is accepted, but includes nested in will not...

Answer

assuming that the filesystem looks like this..

/www
   include/header.php
   class/Login.class.php
   class/Connection.class.php
   resources/login/index_alt.php
   resources/login/index_auth.php
   index.php

this means that

index.php: include(__DIR__ . '/include/header.php');
header.php: include(__DIR__ . '/../resources/login/index_alt.php');
index_alt.php:  include(__DIR__ . '/index_auth.php');

etc; see http://php.net/manual/en/language.constants.predefined.php

Comments