Reputation: 2172
I have been attempting to set up a template header in PHP that will be used on all of my pages. However, I am getting several errors when I attempt to use root relative paths. I have looked at several other questions, and have not been able to fix my problem through them (often because I couldn't grasp their directory setup; hence my diagram). My setup is an Ubuntu 14.04 server running a LEMP stack. In my default.conf
file, I set up my document root as follows:
root /usr/share/nginx/html;
index index.php index.html index.htm;
Directory Structure:
...
|
|
+--ETC
| |
| +--NGINX
| | |
| | +--sites-available
| | |
| | +--default.conf
| |
| +--PHP5
| |
| +--FPM
| |
| +--php.ini
|
+--HOME
| |
| +--ADMIN
| |
| +--database.php
|
+--USR
|
+--SHARE
|
+--NGINX
|
+--[HTML]
|
+--INCLUDES
| |
| +--header.php
| |
| +--footer.php
|
+--PAGES
| |
| +--contact_us.php
|
+--index.php
+--database.txt
index.php
<?php require "includes/header.php" ?>
...
<? php require "includes/footer.php" ?>
These links do not have a root relative path...
header.php
<!DOCTYPE html>
<head> ... </head>
<body>
<?php require ("../../../../home/admin/database.php") ?>
<header>
...
<a href="/index.php">Home</a>
...
<a href="/pages/contact_us.php">Contact Us</a>
...
</header>
</body>
...but these must. If they are switched, both will crash. Also, I don't know how to do require
statement above any better, as it is above the document root, and I am not sure where it may all be called from. Is there a way to access the "server" root, not just the "document" root"?
contact_us.php
<?php require "/includes/header.php"; ?>
....
<?php require "/includes/footer.php"; ?>
...same with this page.
As you can see, I use several different types of URLs, most of them root relative paths. If I attempt to use a root-relative path in my index.php
file, however, I get a blank screen, as it is not able to find the header.php
and footer.php
files (which are required).
I am not any better off on the other pages either. In my header.php
file, you will notice that I have a URL for the contact_us.php
page as part of my header. However, when I start on the index page and click the link for the Contact Us page, I get this error, even though I have definitely spelled the relative path correctly (remember, the "html" folder is my root directory).
Warning: require(/includes/header.php): failed to open stream: No such file or directory in /usr/share/nginx/html/pages/contact_us.php on line 4`
Fatal error: require(): Failed opening required '/includes/header.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/share/nginx/html/pages/contact_us.php on line 4`
Basically, what I want is to make sure that all my URLs start from the same place. I know it would be sometimes faster to use ../
, but since many of my pages have include
statements, the URLs can get broken quickly this way. Is there a way to start from the root directory that would work on any page no matter where it is on the site, or where it gets called from?
EDIT: I have just tried the "<?php require $_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"; ?>
" idea that was suggested below by Lupin. Although this does get a bit father (the background colour loads), it then crashes and says this error message:
Warning: require(../../../../home/admin/database.php): failed to open stream: No such file or directory in /usr/share/nginx/html/includes/header.php on line 30
Fatal error: require(): Failed opening required '../../../../home/admin/database.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/share/nginx/html/includes/header.php on line 30
P.S. Just to be clear, my document root is /usr/share/nginx/html
.
Let me know if I need to include anything, or make anything clearer.
Upvotes: 0
Views: 1425
Reputation: 1244
if your server document root is /usr/share/nginx/html you can use $_SERVER['DOCUMENT_ROOT']
require $_SERVER['DOCUMENT_ROOT'].'/includes/header.php';
For the database file that is located outside of the server root - it is good practice to have the config file outside the document root, but it could be that you took it a bit too far as there is no difference if it's one level outside the root or more. in order to access the database file i think you have 2 options:
add a constant with the real location of the Database file - this will be the easiest but will impact the migration of the site between different servers and you will need to remember to change it when you run the site on a different system.
Locate the database file a bit closer to the document root - lets say one level up and then manipulate the string you get from $_SERVER['DOCUMEN_ROOT']
.
here is example to demonstrate how to get location when the database file is one level up from the root
$x = rtrim($_SERVER['DOCUMENT_ROOT'], '\/');
$x = substr($x, 0, (strrpos($x, '/')+1));
$x = $x.'/folder/database.php';
Upvotes: 1
Reputation: 128
The problem is that when you use the require function with the root path, PHP is not considering your project or web root as the root directory, but is referencing the root directory of your system!
When requesting require('/includes/header.php')
you are actually requesting:
/includes/header.php
and not what you want:
/usr/share/ngix/html/includes/header.php
So - to fix this we want some sort of constant that ALWAYS points to your server website root - like you have defined in nginx. This is where $_SERVER['DOCUMENT_ROOT']
comes in handy
http://php.net/manual/en/reserved.variables.server.php
As long as PHP/nginx are configured correctly, this should point to /usr/share/nginx/html
.
Changing your requires to:
<?php require($_SERVER['DOCUMENT_ROOT'] . 'includes/header.php'); ?>
should do the trick.
Be cautious to not confuse URLs and system paths - PHP thinks in terms of system paths, and your HTML will work in terms of URLs.
Hope that helps!
Upvotes: 3