How to include php file in a php file - php

Hey I would like to include a navigation.php file in my website, but i cant figure out how to include an external file in a .php file, and the only way i could find to include the navigation.php into the files was to make a copy of it in each directory.
<?php include("navigation.php"); ?> works when i make a copy of the navigation.php in each directory.
<?php include("http://www.mysite.net/format/navigation.php"); ?> gives me an error.
I would like to only have one of these navigation.php files in a "Format" directory and then make each page fetch the file and display it.
Also ill be monitoring this question for a while so just ask me if you need any additional info.

Put that file one place. Then refer to it in each file by its proper path.
// navigation.php is in the same directory
<?php include("navigation.php"); ?>
<?php include("./navigation.php"); ?>
// navigation.php is one directory below the current directory
<?php include("../navigation.php"); ?>
// navigation.php is two directories below the current directory
<?php include("../../navigation.php"); ?>
But this can get tedious if you have lots of files in lots of directories. The best way to do this is to use the full path to the file on the file system:
// Full path relative to the root of the account
<?php include("/full/path/to/navigation.php"); ?>
But this can get tedious if the path changes. So define a variable or constant to hold that value for you so you only need to change it in one place:
// In a config file
define('INCLUDE_PATH', '/full/path/to/');
// In each page after you include your config file
<?php include(INCLUDE_PATH . "navigation.php"); ?>

Related

PHP - problem including header and footer inside my index website

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

How do I include other files in PHP?

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。

How to include php db config file form sub directory

I want to use this database config file which located in-> "/main/config.php" directory. Now please tell me how can i include that config.php from-> "/main/admin/login.php" file.
i have simply tried following but not worked-
<?php
include "/main/config.php";
?>
thanks in advance
If you are trying to go back to the file directory then this will include the file. However you have missed the brackets.
<?php include("../main/config.php"); ?>

include files from subdirectory which calls from another directory

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

How can i get link path if i want include another folder

I want to know that how can i give include path of another folder.
For example, i have a project named test folder. In test folder has one folder which is assets folder and index.php file. In assets folder has two folder which are include and xyz folder. In include folder has two file which are header.php and footer.php.In header.php file has three link home, product and customer. In xyz folder has product.php and customer.php file.
In index.php, product.php and customer.php i want to include header.php and footer.php.
But i can't right path.
<?php include('../../header.php');?>
...........
<?php include('../../footer.php');?>
Please help me, how can i give right path which if i click product link then header.php and footer.php will be load.
Try this :
<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>
Ref: http://php.net/manual/en/function.dirname.php
If your Directory look something like this:
src
->includes
->header.php
->footer.php
web
->index.php
And you want to include the header and the footer in the index.php, do the following:
<?php
//index.php
include __DIR__.'/../src/includes/header.php';
echo "foobar";
include __DIR__.'/../src/includes/footer.php';
?>
http://php.net/manual/en/language.constants.predefined.php
Basically i have used like below
<?php include_once('../include/header.php');?>
<?php include_once('../include/footer.php');?>
echo realpath('../../header.php');
echo realpath('../../footer.php');
I think that if you can replace
../../header.php by ../include/header.php and
../../footer.php by ../include/footer.php.
then you will get accurate result.

Categories