So this is my first time creating a test site with xampp. I originally had all my php files in one folder and just recently decided to organize the data (yes, hindsight i should've started with an organized folder structure.) Anyways, I have my setup like this:
" [ ] " implies it is a FOLDER
Installed on my C:\ drive
[xampp]
-[htdocs]
--[QMS]
---[rev3]
----[css]
----[js]
----[DPU]
----[login]
----index.php
----header.php
----config.php
In my "config.php" file I tried to define a root path (this may be the error):
$path = $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/";
.
Then in my header.php file I have:
<?php
require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/config.php";
include $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/login/session.php";
.....
?>
HTML - located in the <head> section
<link rel='stylesheet' type='text/css' href='<?php echo $path . "css/searchBar.css"; ?>'/>
<link rel='stylesheet' type='text/css' href='<?php echo $path . "css/tables/filtergrid.css"; ?>'/>
<script type="text/javascript" language="javascript" src='<?php echo $path . "js/jquery.dataTables.js" ?>'></script>
<script type="text/javascript" language="javascript" src='<?php echo $path . "js/jquery.loader.js" ?>'></script>
... MANY OTHER scripts and stylesheets.
.
My index.php is:
require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/header.php";
When I launch this in Chrome - I get the following errors for ALL of my scripts and stylesheets (19 errors total):
"NOT ALLOWED TO LOAD LOCAL RESOURCE file///C:/xampp/htdocs/QMS/rev3/ ......etc..."
My site was working perfectly when all my files were in the same folder and I wasn't using SERVER['DOCUMENT_ROOT'], but now I have no idea what to do...any advice?
.
NOT ALLOWED TO LOAD LOCAL RESOURCE
The url you use in <a>
, <link>
, <script>
, etc tags should be relative to the document-root, eg:
<link rel="stylesheet" type="text/css" href="/QMS/rev3/css/searchBar.css" />
Don't confuse paths on disk with url's, they're two completely different things :)
Advise on determining the root path
I recommend not to rely on the $_SERVER['DOCUMENT_ROOT']
variable, but determine the root-folder like this (in config.php
):
define('ROOT_PATH', __DIR__);
You'll have a constant named ROOT_PATH
which will contain C:\xampp\htdocs\QMS\rev3
(without a trailing /
).
Now you can do things like:
require ROOT_PATH . '/header.php';
The path in ROOT_PATH
differs from the document-root. If you really want to use the document-root, do this (so that ROOT_PATH
will then contain C:\xampp\htdocs
):
define('ROOT_PATH', __DIR__ . '/../..');
# ...
require ROOT_PATH . '/QMS/rev3/header.php';
Canonicalized absolute pathname
It might be wise to use realpath()
as mentioned by Capsule (expand all symbolic links and resolve references to /./
, /../
and extra /
characters):
$rootPath = __DIR__ . '/../..'; # or just __DIR__
$realPath = realpath($rootPath);
define('ROOT_PATH', $realPath ?: $rootPath);
If realpath()
fails to resolve the path it will return false
, that's why there's a little check.
If by removing the /
from this line
require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/header.php";
you manage to include the header.php file then you also need to change $path
to:
$path = $_SERVER['DOCUMENT_ROOT'] . "QMS/rev3/";
In general - if you use a config.php to set your $path
and you include it in all of your pages as you should, then use the $path
variable and not $_SERVER['DOCUMENT_ROOT'] . "QMS/rev3/
. This way you can mantain your code easily.
About the NOT ALLOWED TO LOAD LOCAL RESOURCE, take a look at this answer
Do not use document_root for defining main path.
Put this line of code in your config.php
$path = rtrim(dirname(__file__),"/")."/";
Now your path variable has your root path including leading slash (/).
You can use this.
Try "QMS/rev3/header.php" without the "/" at the begining
After trying a lot of solutions
i made like this, because it's the way it work local and online.
You can define a variable with the path you need like this :
$path = "";
if ($_SERVER['HTTP_HOST'] == "localhost")
{
$path = $_SERVER['DOCUMENT_ROOT']."/mysite";
}
else
{
$path = $_SERVER['DOCUMENT_ROOT'];
}
include_once($path.'/include/config.php');