Question

I think I found that when I have an errordocument custom error page, include does not always work because the include requires a directory and the page that has the error may be in a different directory.

directory1/directory2/thispagecausesa404error.php

directory1/thispagecausesa404error.php

If I have include includepage.php; in my errordocument page, then it won't work.

I resolved to adding include_once $_SERVER['DOCUMENT_ROOT'].'/includepage.php'; to the errordocument page, which solves the problem, but I suspect that I am doing something wrong or not seeing the proper way to have a flawless include on an error page.

What am I doing wrong?

Was it helpful?

Solution

I'd recommend never relying on DOCUMENT_ROOT being set correctly. Instead, use a relative path from the current script via the __DIR__ magic constant, eg

include __DIR__ . '/relative/path/from/this/script/to/includepage.php';

The path can also contain parent directory traversal via ../ if required, eg

include __DIR__ . '/../../some/dir/includepage.php';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top