PHP include Statement Issue - php

I have a menu.php file I need included in each page on a site I am building. When I open the menu.php by itself it works fine. It's when I have it included in other files such as index.php then nothing loads.
Below is my index.php content. My menu.php is strictly html with a .css style sheet linked. I have searched and can find nothing to solve my problem. Any suggestions?
<html>
<head>
<!-- ... -->
</head>
<body>
<?php include 'menu.php'; ?>
</body>
</html>
EDIT: that is menu.php in the include, not header.php.

Let me try to explain how directories work:
ROOTFOLDER--
home.html
contact.html
PHPFOLDER---
header.php
CSS-----
stylesheet.css
if in home.html you have <?php include 'header.php';?> that wont work because the header is in the PHPFOLDER so you must <?php include 'folderphp/header.php';?>

Check your path, inclusion location etc.
For example, if a file located in a certain folder includes a file, that also includes another file.. the relative path is always based on the very original file opened.
What I do:
include $_SERVER['DOCUMENT_ROOT'].'/menu.php'; // if file is at /public_html/menu.php
or
include $_SERVER['DOCUMENT_ROOT'].'/includes/menu.php'; // if file is at /public_html/includes/menu.php
THat way no matter where you're opening from, you're calling an absolute path.

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

PHP files not executing inside include statements

I have an extraordinarily simple website:
index.html contains:
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>
It is in /usr/share/nginx/html
footer.php is in the same directory and is:
<?php
echo "<p>printed!!</p>";
?>
When I go to http://MY_IP_address/footer.php I get the proper output of just a blank page with "printed!!" at the top left.
When I go to http://MY_IP_address/ I get:
"
Welcome to my home page!
Some text.
Some more text.
"
But I do not get "printed!!" written anywhere.
I am assuming that somewhere I missed something in my config files, but I can't figure out what it is.
The file which contained include() is a file with .html extension, thus it's not interpreted by Apache. Thus, the code in index.html was not included. (Credits to #RiggsFolly for the improved explanation)
Unless your server is set to parse .html files as PHP, it will not be included.
Thus you'll need to rename it to index.php.
The include() function in PHP includes code from other PHP files and copies them into the file that uses the include statement.
Source:
https://stackoverflow.com/tags/php-include/info
http://php.net/manual/en/function.include.php
Php tags(<?php ?>) can only be parsed when file extension is php.
So,
Change file name from index.html to index.php

Index file conflict with include files

I have a problem with my index.php together with my "include files". My index.php file is located inside the main folder named mysite and the rest inside it are subfolders with the respective .php files. The sub-folder files are working perfectly, except the index.php. The include files are messed up every time I preview it in the browser.
The path for the index.php file goes like this:
mysite/index.php
The path for the include files goes like this:
mysite/pages/include/header.php
Here is the HTML file with the include files:
<html>
<head>
<?php
include ("pages/include/headertop.php");
include ("pages/include/header.php");
include ("pages/include/nav.php");
?>
</head>
</html>
Kindly correct me if I have missed something here.
By the way, I'm using XAMPP.
Thank you and More power!
edit your index.php:
include ("../pages/include/headertop.php");
include ("../pages/include/header.php");
include ("../pages/include/nav.php");
and try again "mysite/index.php"
Firstly make sure that you are calling these include commands in a .php file and not .html file.
Secondly make sure that your server is up and running well.
After this try this code:
<html>
<head>
<?php
include "pages/include/headertop.php";
include "pages/include/header.php";
include "pages/include/nav.php";
?>
</head>
</html>
P.S: just remove the brackets from include.
Please also make sure that the file names are correct and they follow the proper case.

Php Include - Subfolders

I'm using php include. Now the files are in the sub-folder.
The error goes exactly like this:
Warning: include(/headertop.php): failed to open stream: No such file or directory in D:\ROLDANKING\xampp\htdocs\mysite\pages\print_design.php on line 11
The HTML/PHP file is this:
<html>
<head>
<title>PRINT DESIGN</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="../images/art_favicon.png" type="image/x-icon"/>
<link rel="stylesheet" href="../css/body.css" type="text/css" media="screen"/>
</head>
<?php include ("headertop.php"); ?>
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<body>
<div id="contents">
</div>
</body>
<?php include ("footer.php"); ?>
</html>
Assuming you have the paths correct and files in place you can try this...
<?php
include ("sub-folder/headertop.php");
include ("sub-folder/header.php");
include ("sub-folder/nav.php");
?>
The thing you want to avoid is having to change the path to an include on each page. You can do that with something like this:
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"; ?>
That will work nicely online, but to work in XAMPP, you need to set up a vitrual host so that the link points to the same thing: http://sawmac.com/xampp/virtualhosts/
Warning: include(/headertop.php): failed to open stream: No such file or directory in D:\ROLDANKING\xampp\htdocs\mysite\pages\print_design.php on line 11
says there is NO headertop.php file in your directory
check if the file exists: D:\ROLDANKING\xampp\htdocs\mysite\pages\headertop.php
also you can just use:
<?php
include ("headertop.php");
include ("header.php");
include ("nav.php");
?>
instead of:
<?php include ("headertop.php"); ?>
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<?php include ("SUB_FOLDER/headertop.php"); ?>
try this
include '../headertop.php';
or
include '/headertop.php';
I have come across dozens of webpages over the last 3 days and I think I have collectively tested exactly what most people coming here are looking for.
ABSOLUTE PATHS. They must be manually established on your local host, and then again on your Live website, but this is nothing shy of declaring a variable set to a particular pre-built function.
Stay with me..
Every time I reference a link in a php (includes and echos), or in html and css, I reference a variable set to the root directory + that original link.
e.g.
background-image: url(<?php echo $root; ?>images/bg-0.jpg);
The only downside to this is the visibility of extra code and tediousness of adding a $variable to each and every link in a css or php document. And lord forbid javascript, because I havent even touched the root of that. Heh. Puns.
Anyways, to make this work..
In my Styles.css Doc, I simply convert it to Styles.php, encase the CSS code in
<style type="text/css"> *CSS* </style>
tags, which enable the echo of a PHP variable but as a string, or in our case an absolute path precursor.
In my header file, I now include my CSS as a PHP include with a new Root Variable.
include $php_root.'includes/css/styles.php';
Notice how this $variable is different than the echo's? The way I declared the variables plays a huge role in how CSS/HTML perceives a root destination, and how PHP sees it.
So these were my set variables.
// WAMP Localhost
$root = "http://localhost/PZD/";
$php_root = $_SERVER['DOCUMENT_ROOT'] . "PZD/";
// Live Server
$root = "http://prozechdesigns.com/";
$php_root = $_SERVER['DOCUMENT_ROOT'];
These may be set in your main header.php include itself.
Or if you're like me with database files to connect to and communicate with, you might pair it with a database connect.php file. I did; that way, when I call upon my init.php file or need to edit the init.php, I do not need to worry about which $root variables are being used after overwrite between localhost and live website.
You may not be using WAMP as you're reading this, and at your time and date or configuration, your PHP Root location may not be set like mine is. But if you are using WAMP and perhaps wish to find out where your root is set or change it, look for the httpd.conf file located by default in "wamp/bin/apache/apache#/conf".

Include files from parent to other directories

I am building a website and has index.php in the root folder. The other pages are up to 2 folders deep and separated out by their different categories.
So the index.php file would be correct;
<?php include ('/inc/head.php'); ?>
But the other pages would have to be;
<?php include ('../inc/head.php'); ?> or <?php include ('../../inc/head.php'); ?>
But this breaks the relative paths for the css and script files in the includes files themselves as they then don't map correctly! Any solutions?
You're talking about paths set in head.php? Why not just use absolute paths? So rather than using css/style.css you'd do /css/style.css, etc.
Okay, so I was getting confused and setting the relative paths in the footer and head sections as well as the 'includes'.
So the solution for the head and footer sections are,
<script src="../../js/bootstrap.min.js" type="text/javascript"></script>
Should be set as an absolute path as #ultranaut said:
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
and the include, if the page is two folders deep from the root should then be:
<?php include ('../../inc/head.php'); ?>
Hope this helps some other PHP newbie too!

Categories