The PHP include is not working - php

I have researched so much in blogs and forums and can not seem to find the solution to this problem. I am using the Wamp directory: C:\wamp\bin\apache\apache2.4.9\htdocs\lr
the lr at the end of the /htdocs is just the folder that I am holding my webpage in.
The code inside of my index.php :
<html>
<?php include('includes/head.php'); ?>
<body>
<?php include 'includes/header.php'; ?>
<div id="container">
<?php include 'includes/aside.php'; ?>
</div>
<footer>
© phpacademy.org 2011. All rights reserved.
</footer>
</body>
When I load the index.php in chrome or any browser the only thing that I am seeing is the footer which happens to be the only thing that is NOT set up in a php include tag.
Here is the code inside of my head.php
<head>
<title>Website Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/screen.css">
Very basic code which only gives me more confusion on why it is not working. If you would like more examples of code that I am using let me know. I will gladly provide some as I really need this to continue creating the database for my login/registration page(s).

It is likely that PHP does not find the files to be included.
Are you sure the folder 'includes' is in the same directory as your index.php?
Also, I would suggest to use "**/**includes/..." with a slash at the start to be sure that it searches the included files from the root directory, and not from the relative path of your index.php.
Besides, shouldn't your root be C:\wamp\www\ in WAMP?

You can check if the included file exist, this can give
you a clue to what is going on
if(file_exists('includes/head.php')):
include 'includes/head.php';
else:
echo 'file either not readible or does not exist';
endif;

Try to close head tag first in your head include file, if that doesn't work, I will dig deeper :D
EDIT:
Sometimes there are problems with file path. I usually use:
<?php include $_SERVER['DOCUMENT_ROOT']."/filename.php";?>
In Your case that probably be:
<?php include $_SERVER['DOCUMENT_ROOT']."/lr/includes/header.php";?>

Related

php not executing in sub directories / php only working in root directory

I am refurbishing a website for a friend, e.g. making it more easy to program/maintain.
The server is running PHP 5.6 and in order to make life easier for me I wanted to uses php's include function to easily include stuff like the head or menu in every web page.
The file structure I use is index.php in the / directory and e.g. history.php in /pages. The files I am including e.g head.php lie in /php.
My problem is that in index.php
<?php include ('php/head.php'); ?>
perfectly executes and includes the designated file but in all sub directories such as /pages the same php code in history.php just doesn't execute at all leaving me with a blank line in the source code. I figured that this has to do with my PHP config or that said might be wrong, but I couldn't find the issue. I also tried calling the tech support of my web hosting provider but although they told me that everything should be working now I still get a beautiful blank line.
I've been searching for a solution to my problem for quite some days now, but I sadly haven't had any luck so far.
Hope the community can help
Thanks in advance
If you set the include_path to the full path then no matter in which sub-directory you have scripts they will always use the full path to the required file.
<?php
/* If you set this on every page the path will always be correct */
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/php/' );
include( 'functions.php' );
?>
<!doctype html>
<html>
<head>
<title>Include PHP files</title>
<?php
include( 'head.php' );
?>
<body>
<?php
include( 'menu.php' );
?>
<!--
regualar html,javascript etc
-->
<?php
include( 'footer.php' );
?>
</body>
</html>
Is it simply a file paths issue? If you're in a sub-map, then the path to head.php will be different:
<?php include ('../php/head.php'); ?>

Going one level up using "../" in php not working?

First of all, there are plenty of questions asked in the same category but none of those problems i face.. so thats why i opened up this question.. and secondly im not php developer... know just basics include, require for header and footer..
this is my directory structure:
xampp>htdocs>website>"folders" and "files"
"folders" are css, sass, js, pictures and projects.
"Projects" folder has projecta.php and projectb.php
"files" are index.php, header.php, footer.php, menu.php
so above is my folder structure.. everything is working great when the files are in root directory.. say for example, including header, menu and footer php in index page is working fine..
so now i wanted to include the header, footer and menu page for projecta in "projects" folder, so i tried this
<?php include "../footer.php"; ?>
it is not working.. look at the below picture
it looks like the css files are not executing..
for more info here is the header snippit
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_des; ?>">
<meta property="og:title" content="<?php echo $page_title; ?>" />
<meta property="og:description" content="<?php echo $page_des; ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://www.example.com/<?php echo $page_url; ?>" />
and here is menu snippit
<ul>
<a <?php if ($page_nav == 'index.php') { ?>class="active"<?php } ?> href="index.php"><li><h2 class="mt">Home</h2><i>Go to</i></li></a>
<a <?php if ($page_nav == 'about.php') { ?>class="active"<?php } ?> href="about.php"><li><h2 class="mb">About</h2><i>Me</i></li></a>
<a <?php if ($page_nav == 'projects.php') { ?>class="active"<?php } ?> href="projects.php"><li><h2 class="mt">Work</h2><i>My</i></li></a>
<a <?php if ($page_nav == 'contact.php') { ?>class="active"<?php } ?> href="contact.php"><li><h2 class="mb">Contact</h2><i>Me</i></li></a>
</ul>
and this is how i include header and menu in my pages,
<?php
$page_title = "title a";
$page_des = "description a";
$page_url = "index.php";
include "header.php";
?>
<?php
$page_nav = basename($_SERVER['SCRIPT_NAME']);
require "menu.php"; ?>
so someone please help to find a valid answer? yes, i know, you guys will find plenty of mistakes in the code shown above.. but thats okay.. i need answers.. thanks...
found an answer: first of all thanks for everyone who gave thier suggestions.. i tried all the below, but nothing works.. so i found this code works fine.. but the thing is it somewhat looks like the answers by #Dane Caswell and #Scott Dallas.. but i have no idead why thier answer doesnt work and why this one works..! but anyways thank you everyone.. im pasting the answer here cause someone might find it useful..!
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/your directory/common/header.php";
include_once($path);
?>
This problem is because the PHP interpreter has far greater access the file system than just the public_html or htdocs (site root) directory. It has access to all files and folders above the root. Can make it difficult to establish the actual current working directory. PHP will check the current working file location for the file reference before failing. see PHP Docs for include()
Files are included based on the file path given or, if none is given,
the include_path specified. If the file isn't found in the
include_path, include will finally check in the calling script's own
directory and the current working directory before failing.
The the solution I usually use is to define the servers document root using PHP $_SERVER variable. This solution is not completely fool proof as it depends on the server environments' document root configuration.
include($_SERVER['DOCUMENT_ROOT'] . '/footer.php');
I have found that this can also be annoying is you are working on a local server where the public_html or htdocs folder may contain sub folders for each website, like yours for example. The $_SERVER['DOCUMENT_ROOT'] will on most configurations return the absolute path to the server 'public' root, not your websites subfolder. This means you have to add the website folder to the include path.
include($_SERVER['DOCUMENT_ROOT'] . 'your_website/footer.php');
This is pretty annoying because it means that when the website is uploaded to a production server it is likely you will not have a additional sub folder for the website, so this include would need changing.
Obviously this is a real pain and not great practice, I afraid I don't know what the solution to that problem would be, albeit I am sure there would be a better way. Hopefully this can help you to get going and maybe someone else can provide (us both) a better practice solution.
This annoys me as well.. You can use
$_SERVER['DOCUMENT_ROOT']
I like to do this at the top of all my files when I'm starting a new project:
<?php include($_SERVER['DOCUMENT_ROOT'] . "/inc/config.php"); ?>
In my config file, I store lots of variables as useful values that I am going to use throughout my project so I don't have to keep typing $_SERVER['DOCUMENT_ROOT']
Or you can just stop being lazy like me and use $_SERVER['DOCUMENT_ROOT'] as much as you like :)

PHP - Main file wont include header

I'm trying to make a website and to reuse as much code as possible. I'm using the PHP include statement, but it won't seem to work for me. At the moment I'm only trying to get it to work with the header but I will be doing the same with the nav menu and a footer. I'm only new enough to HTML and have only started learning the include part of PHP today so any help is appreciated. The header file is in an 'includes' folder which is contained in the main website folder. Thanks.
<body>
<?php include"Includes/Header.php";?>
<div class="container-fluid">
<div id="menu">
<ul id="menu">
<li>Home</li>
<li>Our Products</li>
<li>Our Brands</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<div id="img1">
<img src="lasange.jpg" alt="lasange">
</div>
</body>
Header Code
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font- awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="NewHomepageStyleSheet.css">
</head>
<body>
<div id="toplocation">
<i class="fa fa-fax"></i>
(01)-8393790&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<i class="fa fa-map- marker">&nbsp Unit 10,11,12, Baldoyle Industrial Estate, Dublin 13, Ireland</a> </i>
</div>
</body>
</html>
Try this but make sure this below code must be written in php file:
<body>
<?php include("Includes/Header.php"); ?> // or
<?php require("Includes/Header.php"); ?>
<div class="container-fluid">
<div id="menu">
<ul id="menu">
<li>Home</li>
<li>Our Products</li>
<li>Our Brands</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<div id="img1">
<img src="lasange.jpg" alt="lasange">
</div>
</body>
Because your page requires the header, change include to require. This will force PHP to throw an error when it doesn't find the file (instead of just ignoring and proceeding). The text of the error will be the key
My bet is that your path is not correct. If you use a relative path, it must be relative to the file with the include statement itself, not relative to the main website folder. My advice is to use absolute paths instead.
Finally, while you're in development I suggest that you either enable display errors so you can see error details in the browser, or find out where PHP logs errors so you can check there when something goes wrong.
Putting all of that together, I suggest you replace your include line with:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require "$_SERVER[DOCUMENT_ROOT]/Includes/Header.php";
?>
This should be a comment, but its getting a bit long....
In terms of structure this is messy (this not about your problem but your approach). The HTML head part should not appear inside the body tags, and frequently you'll find you want to modify the HTTP headers - which requires PHP code before any html output.
Although there's a case for putting self-contained fragments in individual include files, these should be well formed (i.e. open and close tags within the same file).
A better approach is to use functions to output structural elements of the content and invoke these from your page script, in addition to ensuring that your HTML is well formed it also protects you against security vulnerabilities introduced by emitting partial content directly from an include file when its URL is typed directly into a browser.
consider:
<?php
define('BASE_PATH', dirname(__FILE__)); // defining as root for your app simplifies includes
include(BASEPATH . '/pageTemplate.php');
include(BASEPATH . '/menu.php');
?>
<html>
<head>
<title>My demo page</title>
<?php template_required_css_and_js(); ?>
</head>
<body>
<?php template_body_start(); ?>
...
<?php menu(); ?>
...
<?php template_body_end(); ?>
</body>
</html>
(BTW: check '.' is in your php.ini include_path and the permissions on the file allow your webserver to read it. Reading the error log is always a good idea when you have a problem).
This is a collection of points from the comments, all of which are pretty good:
Your syntax could do with having a space: include "file.php";.
Your includes should be treated as case sensitive, on LAMP case sensitivity is a standard. It is a good habit to get into.
Includes need to run on a PHP page, if your page is HTML it will not include anything. PHP can include any page, it doesn't need to always call a PHP page.
Turn on PHP Error logging to check what the reason for the include not displaying is. See PHP Error Displaying.
Be sure that your include file does contain something to output to the browser!
Includes and requires and their _once counterparts are all practically identical in their way they work (there's a shocking number of folks on this question with answers thinking just changing from include to require will make it work, or changing to include_once will make it suddenly work.
The difference with require is that if the call fails then PHP ends the script with an error. Hence the contents included is required (high-hat!).
Directories
Includes typically start from your current working PHP directory, so if you have a file at /home/account/public_html/horses/index.php and you are including a file from the root html directory /home/account/public_html/include.php this can not be reached relatively from the current directory which is /horses/.
To sidestep this whole issue above you should use best practise of including files with an absolute file path, such as using $_SERVER['DOCUMENT_ROOT'] if including files which are within the public website area of your account. If including files from other parts of your account then still give an absolute path but manually, (using a DEFINE or suchlike can help) like : /home/account/extras/include.php
A few examples of each scenario:
Relative path:
include "folder/file.php";
Above: The folder needs to be in the same directory as the file running the include.
include "../parentfolder/folder/file.php";
Above: This will not work. Includes can not change relative directories above the current working directory.
include $_SERVER['DOCUMENT_ROOT']."/folder/file.php";
// reference address: /home/account/public_html/folder/file.php
Above: This include will be displayed from any page on any location of your website, as long as the include is withing the HTML Document Root (typically public_html). I recommend using $_SERVER['DOCUMENT_ROOT'] as the standard way or referencing includes.
define("RootHost","/home/account/secret");
include RootHost."/folder/file.php";
// reference address: /home/account/secret/folder/file.php
The above code would typically be used for including content that is not reachable by the website browser such as being not within your public_html HTML document root folder.
PHP root is not read the same as your standard root. Try replacing your code with this:
include($_SERVER['DOCUMENT_ROOT']."Header.php");
above Solution is right just small change:
<?php include_once("Includes/Header.php"); ?> // or
<?php require_once("Includes/Header.php"); ?>

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".

Parent directories and PHP

I really hope there's a simple solution to this.
<?php include("header.php"); ?>
Let's say I have a php header in my root folder simply titled header.php. In that header there is a link back to the home page, main.php, and main.php is also located on the root. No problem so far. Here's what header.php looks like. Simple stuff, right?
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="headerwrap">
<div id="linkbox">
<img src="images/mainlogo.png" />
</div><!-- End linkbox -->
</div>
However, let's say I have other pages in subdirectories. Subpage.php is located in a child directory of the root, and so it has to look back to the root to get the included header.php.
<?php include("../header.php"); ?>
That wouldn't be a big deal, except that header.php links back to main.php and also style sheets, none of which are in *subpage.php's directory, thus causing an error when someone on Subpage tries to get back to Main via the link in the header.
I'm just hoping there's a simple way to make this work, and that I don't have to copy and redirect all includes into every subdirectory. Also, there are too many pages to really reasonably include them all on the root. Sorry if this answer is posted elsewhere; I've looked and just have no real idea what I'm looking for. Thanks for your help. Hope all that makes sense.
You could just hard code main.php's path within header.php:
<img src="http://website.com/images/mainlogo.png" />
As opposed to a php prob this seems to be an html prob..
Your links should be relative links with a preceding / i.e.
Text
instead of
Text
how about using absolute links. header.php should also reference main.php absolutely, then there should be no troubles:
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php"); ?>
You can use the base html tag:
<base href="http://yoursite.com/" />
This way you can use that url as the base for all your links/stylesheets/images and you don't need to worry if they're in a subdirectory.
the best thing to do is to get in the habit of using
$_SERVER['DOCUMENT_ROOT']
that way you have no confusion as to what directory you're in, etc.
so including your header for example would be as simple as :
include $_SERVER['DOCUMENT_ROOT'] . "/header.php";

Categories