Reputation: 3960
I'm working locally. My <?php include ('header.php'); ?>
is working fine for those files in the root. E.g. mysite/mypage.php
works perfect but mysite/mydir/mypage.php
can't find any includes, all of them located at the root. I defined BASE_URL
in header.php:
<?php
define('BASE_URL', 'http://localhost/bmmi/');
?>
Then, lets say I have in contact.php
:
<?php
$title = 'Contact Us';
include (BASE_URL . 'header.php');
?>
and still I'm getting an error because it can't find the includes
. Any idea what's going on? Thanks.
EDIT
Still getting an error. This is what I have now:
<?php
define('BASE_URL', '/Applications/MAMP/htdocs/bmmi/');
$title = 'Custom luxury homes';
include (BASE_URL . 'header.php');
?>
And this is one the errors: Failed to load resource: the server responded with a status of 404 (Not Found)
localhost/bmmi/portfolio/img/logo.png
The path should be like this: bmmi/img/logo.png. As you can see is adding the img/logo.png in portfolio. It is happening with all dependencies, .css, .js and images. Any idea? Thanks.
Upvotes: 0
Views: 1913
Reputation: 24383
The problem is that you need the path on the file system, not the URL when accessing it by the browser:
define('BASE_URL', '/path/to/webroot/bmmi/'); // <-- like this
$title = 'Contact Us';
include (BASE_URL . 'header.php');
Alternately, you could also make it relative to the directory that contains the current file, which would make your script work if you ever change the path to the web root:
define('BASE_URL', __DIR__ . '/bmmi/');
Note: If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL, however the URL would need to output a PHP script to be parsed by your server. I can almost definitely guarantee this is not the case, and if it is, you likely have a major security issue because whoever controls the other server can execute arbitrary code in your app.
Finally, you may be better to just append to the current include path and save yourself from having to specify the BASE_URL
constant every time. This way you can just include it without specifying the path:
set_include_path(
get_include_path()
.PATH_SEPARATOR.__DIR__.'/bmmi/'
.PATH_SEPARATOR.'/some/other/path/'
);
include('header.php'); // Will now also look for the file in __DIR__.'/bmmi/'
Upvotes: 3