I\'m working on testing out a free helpdesk script on my site. I was able to get the script installed after some troubleshooting but it\'s still not working.
The error th
PHP include paths are a bit of a tricky subject. In a nutshell, the require
and include
functions work from caller script, as well as the defined PHP include paths, which may not be the actual script itself. So take for example test.php
which includes libs/library.php
, which includes functions/helpers/helper.php
:
test.php
require_once('libs/library.php');
libs/library.php
require_once('../functions/helpers/helper.php');
Now, looking at this code you'd expect library.php
's relative include to work. Unfortunately this is not the case. Actually, the require will be relative to test.php's location, so you will get a fatal error in your include.
One way to solve this is by having an include file that lists absolute paths to various commonly used directories. For example:
global.php
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
Now since the paths are absolute, we won't have a problem including the files:
test.php
require_once('global.php');
require_once(LIB_DIR . '/library.php');
library.php
require_once(FUNCTION_DIR . '/helpers/helper.php');
Another alternative, though I think less preferred method, is to use dirname(__FILE__)
, which gives you an absolute path to the current file, which you can then use for relative includes:
library.php
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');
This is not as clear as the constant names method shown above.
In case anyone was being kept up awake trying to solve this for me I wanted to post the solution I found before I went to bed.
After much searching about apache and php settings I finally came across information on the .htaccess file. For those of you who don't know (like me) what an .htaccess is here is the definition from the apache.org site:
.htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
In the parent directory from where my hesk script is located I had a .htaccess file that has these lines in it :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^filename/([A-Za-z0-9_%\s\.-]+)/([0-9]+)/([0-9]+)/oneSOURCE\.html$ /functions/download2.php?file=$2&filename=$1&key=$3 [L]
RewriteCond %{REQUEST_URI} "/userfiles/documents/"
RewriteRule (.*) index.html [L]
RewriteCond %{REQUEST_URI} "/functions/download2.php"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_URI} "/functions/databaseDump.php"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_URI} "/functions/queryResults.php"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_URI} "/functions/checkForDocs.php"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_URI} "/reports/"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(php)$ index.php/$1 [L]
RewriteRule \.(html)$ index.php/$1 [L]
RewriteRule (.*) - [L]
RewriteRule (.*) index.php/$1 [L]
I created a .htaccess file with "RewriteEngine off" in it and uploaded to the hesk directory. Now the relative paths from the original script are working just fine.
I apologize if this question and answer doesn't belong in stackoverflow. I initially thought the issue had to do with php and coding but it seems it had to do with the directory's apache configuration.
PS If anyone is curious on the route I took to find this solution feel free to ask in the comments.