How do I include other files in PHP? - 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。

Related

Trying to Create an Audit Trail For Log in User History [duplicate]

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。

php - include html pages outside the root folder

On my php site I'm trying to include html pages outside the root folder (the html pages change automatically)
if I include an html page I get the 404 error for the linked files and if I include the linked files too they aren't displayed correctly
path/site/root/index.php
...
include("path/html/index1.html");
include("path/html/index1_files/img.png");
...
path/html/index1.html
...
<img src="index1_files/img.png">
...
result:
You need to specify the directory properly. So, in your case, when linking to the image, replace <img src="index1_files/img.png"> with <img src="./index1_files/img.png">. The ./ goes back into the folder the file is saved in, and ../ goes up one level. Your code is looking for a folder called index1_files within the current page.

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

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

included files php do not appear in a browser

I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.

Categories