简体   繁体   中英

How can I access a page that is not on the root and has inclusions

I am working on a web application that uses PHP and JavaScript and I have some troubles accessing some pages that are not on the root if they are including other files (ie. 'include once').

[file system]/www/project/test/test.php

the project folder has the index.php Here is an example of my code :

include_once('../config/config.php');
include_once('../database/database.php');
echo 'Test.php';

$config = new Config();
$dbhandle = new Database();

[...]

What am I doing wrong here?

The error log from Apache tells me that there is no file config.php and database.php.

Thanks for your help!

use $_SERVER['DOCUMENT_ROOT'] to relocate the path to the root path of your server like this :

 $root_path =  $_SERVER['DOCUMENT_ROOT'] ; //Returns the root path of your server

 //Then call your inlude_once with the absolute path
 include_once($root_path . '/project/config/config.php')

So now all your path are Absolute and not relative. Hope that helps.

PS : You can also get confortable using a php framework that will do the magic of routing your dependencies easier on a bigger project.

to get document root use

$_SERVER['DOCUMENT_ROOT'];

and hence

include_once($_SERVER['DOCUMENT_ROOT'] . '/project/config/config.php');

should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM