I have a problem with including header.php and footer.php on my index.php
The structure of the website is like this.
I have an index.php page and I have also 4 folders which are (about us), (support), (assets), (includes) that they have pages inside those folders. In the (includes) folder I have header.php and footer.php. In my (assets) folder I have 2 folders which are (css), (js). Inside my (css) folder I have my style.css file and in the (js) folder I have my custom.js file.
In my (includes) folder, the header.php file is including the style.css like this. . Moreover, in my (includes) folder the footer.php file is including the custom.js like this.
Furthermore, in my (about us) folder I have about-us.php page. When I use <?php include "../includes/header.php"; ?> and <?php include "../includes/footer.php"; ?>, it all works normal.
My problem is this. When I try to include header.php or footer.php in my index.php page they do not work. I have tried to include them like <?php include “/includes/header.php"; ?> and <?php include "includes/footer.php"; ?> but they do not work. I have tried different methods but nothing is working. The problem is that I can include header.php and footer.php in my sub pages which are inside a folder but not in the index.php which is outside a folder. In addition, If i have to make any change to the header or footer I will have to change it twice, one for the index.php which will be hardcoded and one in the header.php or footer.php which are included in the sub pages.
How can I make it so I can include header.php and footer.php from the (includes) in my index.php page and also working on about-us.php page which is inside the (about us) folder ?
Thanks.
Referencing files is difficult [non-intuative (pick a word)] when you are bouncing around different levels of a folder structure. You could write a book on relative and absolute file paths.
If might be better, before your site gets too big to create yourself a config.php file, define a proper base URL for your site, and then include that in each of your pages, along with that, that you are trying to reference.
For example. In the root directory of your site, create a file called config.php and use the following:
<?php
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"]);
Then in ALL of your other files, you can reference the config.php file first, which will define the ROOT_PATH for that document, and then call your other files.
So in your header.php file
<?php
include "./config.php";
include(ROOT_PATH . "/assets/css/style.css");
Then when you call header.php from index.php, header.php will have the absolute route to the CSS file to reference.
I hope I have explained that well enough.
EDIT / ADDITION:
config.php files are a handy addition to any PHP project for many other things as well as defining URLS. You can keep your DB connections in there, arrays of static things you use often etc. They become the "go to" place for consistently used things.
This will work.
include "./includes/header.php";
Otherwise, you can include the full file path location of the header.php and footer.php in the index.php
Related
I just started creating a website at my home.
Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.
So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.
Then I did include the header & footer pages inside the index.php page by using these following codes.
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
Undoubtedly, they worked fine without any trouble.
Then I created a directory as account inside the htdocs.
Now I've a login.php page inside the account directory (/account/login.php).
Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.
So, how can I include them while the login page is in account directory?
When creating sub directories and including files it is always simpler to use absolute file paths.
The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.
Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript
I think it's a file path problem,you can use this code:
<?php include '../header.php' ?>
<?php include '../footer.php' ?>
Load the file of the first level directory。
I just started creating a website at my home.
Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.
So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.
Then I did include the header & footer pages inside the index.php page by using these following codes.
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
Undoubtedly, they worked fine without any trouble.
Then I created a directory as account inside the htdocs.
Now I've a login.php page inside the account directory (/account/login.php).
Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.
So, how can I include them while the login page is in account directory?
When creating sub directories and including files it is always simpler to use absolute file paths.
The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.
Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript
I think it's a file path problem,you can use this code:
<?php include '../header.php' ?>
<?php include '../footer.php' ?>
Load the file of the first level directory。
I'd like to use a single header.php file that I will call on all my pages. I'd like to know if this directory structure is correct:
/public_html
index.php
header.php
/page1
index.php
/page2
index.php
Inside page 1 and 2 I will call the header.php file using the include function :
Will I be able to call the header.php file even though it is located one level above page1 and 2?
A better practice would be to store all the common files ( like header.php) in folder includes , as shown below:
/public_html
index.php
/includes
header.php
/page1
index.php
/page2
index.php
You can access this file ( header.php ) from /page1/index.php simply by doing this
include '../includes/header.php';
From /public_html/index.php, you just do this:
include 'includes/header.php;
ADDED FEW THINGS HERE:
define('BASE_URL', $_SERVER['SERVER_NAME'] . "/");
You may use this to define the server name. Then you can include your files using this constant as below:
include BASE_URL . 'public_html/includes/header.php';
This is equivalent to :
include 'YourServer/public_html/includes/header.php';
This way, you don't have to worry about the level of directory to be jumped using ..
I have a file here: public_html/wiki/index.php
Im calling these files:
<?php
include "../auth.php";
include "../header.php";
?>
However, my page looks like this:
If I paste the same file in public_html/index.php then it will look like this:
I think I've narrowed it down to my header.php file which is being called from /wiki/ but header calls files from ../css.. and ../js and so on.
How can this be adressed? I've looked at many posts already which tells me to define a global variable but it doesn't help me. Like so:
<?php //config file in root folder
define("ROOT", __DIR__ ."/");
?>
and then calling them with this:
<?php
include_once("../config.php");
include (ROOT ."auth.php");
include (ROOT ."header.php");
?>
How should I do this so I can get all my javascripts and the actual site with me in another folder?
NOTE: I WANTED THIS AS A COMMENT BUT DO NOT HAVE THE REP
We have a different folder structure, but it is good practice to put your includes in one folder and from there link all the JS files (best practice is, place them in a pre-footer for speed) and then do
<?php include $_SERVER['DOCUMENT_ROOT'].'/includes/page-footer.php'; ?>
Just change the file name to suit you :) hope this helps
the problem was my include file didnt specify links as /js/file.js
i had them as js/file.js.
thanks #cd001
Hello I'm having my first serious go with PHP to create a sample script for my self. It has a basic structure, in my root folder I have:
index.php
core folder (holds most of my php function files)
includes (holders my header.php and footer.php)
sites - (sites has 3 further folders site A, B, C)
CSS
js
All pages are made up by taking a header.php and footer.php from the includes folder and then each page has its own content in the middle. The header.php contains (as well as basic html and links to javascripts stylesheets ect) includes from core folder like so:
include_once '/core/connect.php';
Now these all work great using the index.php which provides links to the 3 different sections of the site, sitea, siteb and sitec.
But when you navigate out of the document root to say /sites/sitea/index.php all those links are now broken.
What is the best way to go about building the links in the header.php section so they are relative site wide no matter which folder you are in?
The idea behind this is that you do only have ONE file for each process.
So process all pages through index.php
index.php would contain, for example,
require('header.php');
include('content.php');
require('footer.php');
That way, it won't break the site if your content doesn't show.
Your index is always loaded from the same path, so header/footer wouldn't change. Just content.
When you're including you want to use a real path, not a relative path...
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
/* something happens here */
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/footer.php');
The best way is to always use physical path from wherever you are - this way every page that include other page with includes won't get break:
PHP 5.2 and down:
require(dirname(__FILE__) . '/core/connect.php');
PHP 5.3 and above
require(__DIR__ . '/core/connect.php');