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      <i class="fa fa-map- marker">  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"); ?>
Related
As the title says, is there a way to edit a portion of an entire site with one code or page? For example, if the bottom of every page of a site said "2014", is there a way in html or css to change every page of the site to say "2015" without having to do so manually to each individual page?
I understand this can be done in php, and I understand that a server can be configured for html to read php code. Are there any flaws to this method (perhaps the html page will load slower if it's configured to read php)? Are there any other ways to do this besides using php code?
Performance Concern:
You will not see any performance difference between having PHP render basic HTML and typing the HTML yourself.
The performance impact is only noticeable on HUGE PHP applications. And even then, it's still very fast.
What you ask is common practice. This is an example of what you can do.
Make a file called index.php and put this inside:
<!doctype html>
<html>
<head>
<!--Your head stuff-->
</head>
<body>
<header><?php require_once 'header.html' ?></header>
<section class="main_content"><h2>My Page!</h2></section>
<footer><?php require_once 'footer.html' ?></footer>
</body>
</html>
Make a file called header.html and put this inside:
<h2>This is my header</h2>
Make a file called footer.html and put this inside:
<h2>This is my footer</h2>
As you can see, the practice is to use any of the built-in PHP functions to include other files in your PHP file:
include 'some_file.php';
require 'some_file.php';
require_once 'some_file.php';
I think Dreamweaver can do this, with its find and replace entire website property
Assuming all pages have a CSS file in common, you can use the content CSS property of a pseudo element like before or after to control content across all pages.
For example:
#footer:before {content:'2015';}
<div id="footer"></div>
I see multiple answers about creating php files in order to reuse headers/footers, but nothing specific enough. I can't seem to get it to work.
What exactly would the php file look like and what exactly would my html file look like (given the code as it currently is, below)? do I have to convert all my html files to php files in order to use the php include line?
<div id="footer2-wrap">
<div class="container">
<table id="header">
<tr>
<td class="phone-number"><span class='wsite-text wsite-phone'>Copyright 2015 | xxx Corporation</span></td>
</tr>
</table>
</div>
Create a new file with your footer data. Let's give it the name: footer.php:
<div id="footer2-wrap">
<div class="container">copyright etc...</div>
<div>
Then inside your master template (or index.php file), include the footer file:
include_once('footer.php');
You should work with include(), require() or require_once functions in PHP to include your files, which depends on your situation.
For instance, let's assume you have a basic php file, called index.php and you want to add sidebar.php, footer.php, navigation.php.
index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
include("sidebar.php");
include("navigation.php");
include("footer.php"); // we include footer.php here. you can use .html extension, too.
?>
</body>
</html>
footer.php (or html)
About us
Our work
Testimonials
What we do
Contact us
Yes, you must have a .php file to use PHP, your server must also have PHP installed (but most already do). PHP also adds to the loading time of a page, so take that into consideration when using it. To add the footer with PHP, you can use the PHP function include(), or, I am not sure if this is considered correct, with file-get-contents():
include():
<?php
include("footer.html");
?>
file-get-contents():
<?php
$footer = file_get_contents('footer.html');
echo $footer;
?>
You could also do the same thing with JavaScript:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;
Code taken from here.
Note: The file must be on the same domain to use JS.
First create a file called menu.php(if you use .html it wont work)
On that php file write the html code for your menu
<div id="navbar">
more code
</div>
At your main html file write
<?php
include("menu.php");
?>
The code should be able to run. Make sure you are running the code via Apache. Download Xammp or wammp. Start Apache, copy paste your project folder to the xammp folder under httdocs. Then on your browser type in localhost/[your project name] and make sure the html files are changed to .php.
I want to make a header file, but am not experienced with PHP in almost any aspect. My uncle was telling me that I can use a header file with PHP, and that it was like a CSS with HTML.
The following is the HTML I want in my PHP:
<center><nav>
<ul>
<li>Home</li>
<li>Arcade
<ul>
<li>Action</li>
<li>Arcade</li>
<li>Puzzle</li>
<li>Vehicle</li>
<li>Violence</li>
<li>Defense</li>
<li>RPG</li>
</ul>
</li>
<li>Watch
<ul>
<li>TV Shows</li>
<li>Movies</li>
</ul>
</li>
<li>Extras
<ul>
<li>News</li>
<li>Updates</li>
</ul>
</li>
<li>Support</li>
</ul>
</nav></center>
How could I incorperate that into a header file?
I have looked at other websites such as W3Schools, but they don't seem to work. I think my main issue is that I am not sure how to put the HTML into the PHP document. When I do it, I try to type out the HTML as if it were header.html, only, ya know, it's not.
But anyways, if anybody could even just point me in the right direction of how to incorperate that HTML I included into a header.php file, and what to put in the html.
From what I have seen, the best scenario for including the PHP document in the HTML code is:
<html>
<?php
header('Location: http://www.example.com/');
?>
Right now, I'm putting that HTML in every single page. This is restricting me from making navigational tab changes!
My website is http://www.gameshank.com/
Again, thanks in advanced, sooo much!
Ah... the PHP header command is used to output a HTTP header, so I'm really not sure what you're hoping to achieve via its use.
What you want to do is save your generic header HTML/PHP code into a new PHP file (perhaps called "header.php") and then include the contents of that file within each of your existing PHP pages via an include statement. For example:
<html>
<head><title>Sample HTML page</title></head>
<body>
<?php include 'header.php'; ?>
<h1>Page specific content</h1>
<p>And stuff.</p>
</body>
</html>
By doing this, each page will automatically contain the contents of the header.php file (which will appear wherever you place the include statement) and any changes you require can simply be made to the header.php file.
You definitely can!
On your main page (Lets say index.php), on the top you can put in
<html>
<?php
include 'header.php';
?>
</html>
(Or whatever your header file is called.).
Put that HTML in header.php you want to include.
You have to use include like this
<?php
include('header.php');
?>
or use require
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
While you're at this, it's also a pretty good idea split the footer off of your page and place it in a seperate PHP file like you're doing with your header.
The point of this is it allows you to make changes to code that is featured in multiple pages throughout your site without having to go through every single page and make the same change.
For instance if you have "Copyright (c) 2013 Blah Blah Blah" at the bottom of 50 pages on your site, when 2014 rolls around you don't want to edit 50 pages to change 2013 to 2014. You'd just make it once in "footer.php".
And like the others have said, simply include the file in your pages.
<?php
include 'footer.php';
?>
I have a reviews page on my website, and I wanted my website to be extremely user friendly, so I made it a sub-index. I have my index.php under a folder named reviews (found here) so the domain is just /reviews. When I try to include a PHP or CSS it abandons them and excludes them.
The code I am using to include my CSS (which is working on every other page) is:
<link rel="stylesheet" type="text/css" href="/stylesheets/index.css">
The PHP include() that I'm using is:
<?php
include('header.php');
?>
This PHP works on all pages that do not use a parent folder, ex. index.php (for my homepage).
The HTML in the PHP document is:
<html>
<center><nav>
<ul>
<li>Home</li>
<li>Arcade
<ul>
<li>Action</li>
<li>Arcade</li>
<li>Puzzle</li>
<li>Vehicle</li>
<li>Violence</li>
<li>Defense</li>
<li>RPG</li>
</ul>
</li>
<li>Watch
<ul>
<li>TV Shows</li>
<li>Movies</li>
</ul>
</li>
<li>Extras
<ul>
<li>Updates</li>
</ul>
</li>
<li>Support</li>
</ul>
</nav></center>
</html>
Anybody know any solutions to get my PHP and my CSS working in sub-folders?
My website is http://www.gameshank.com/
The homepage is using the header.php file!
If you know that header.php is in the same directory as from the file you want to include it to:
include(__DIR__ . '/header.php');
This is immune to changes in the working directory and therefore pretty fail-safe.
Magic constants Docs
You might find as well the following Q&A interesting:
what is the difference of absolute path in html and php ?
Relative paths : going down and up
PHP include with ../ failed open
Also it's important to understand the difference between the CSS-URL (URL-path is resolved in the browser) and the paths in PHP (those file-paths are resolved on the server, the browser is yet far far away).
Does this work?
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/header.php";
include($path);
Call the .PHP file by:
<?php
include('../header.php')
?>
The /../header.php won't work either...
Then clear your browser history and cookies, then it works from the editing end of things.
Strange fix, but it worked. But until the editor was cleared of history and cookies, it wouldn't work.
I know this is a basic PHP question, and I'm trying to learn the stuff. I very familiar with HTML, CSS and familiar with the CONCEPT of PHP, but not with specifics.
I have always partnered with a back end developer to accomplish this stuff and to set up wordpress sites, etc.
I'm building a very basic four or five page website (a showcase for the client's custom fishing rods: http://www.tuscaroratackle.com/index2.php). I want to call the page header (as in logo, navigation, etc., not as in the head element) dynamically from a php file, and same thing with the footer, so I don't have to rewrite all the markup on every page for these bits.
I don't intend to use a database for this site, I was just thinking I could call those two bits from another file, as you would in a wordpress setup with the header.php in the wp-content directory.
Is there an easy explanation on how to do this? (I get the basics, just looking for help on the more specific PHP calls that need to be made)
Or, if this is not an answer somebody could easy give, can you point me to a good resource to research it further?
Thx
You betcha - include and require -
using include
in your page:
<body>
<?php include 'header.php'; ?>
in your header.php
<div id="header">
<!-- content -->
<?php echo "run php stuff too"; ?>
</div>
would result in:
<body>
<div id="header">
<!-- content -->
run php stuff too
</div>
You should put the header html code in some file such as header.php and then include it with php like:
include ('header.php');
You should specify the correct path there, for example, if you put the header.php file in includes folder, you can include it like:
include ('inclues/header.php');
More Info:
http://php.net/include
Put in a separate file and use include, require or require_once.
Eg
require_once("path/to/myfile.php");
Look into PHP includes.
The way I normally do it is to create a file called includes.php with two functions header() and footer(), then call them on each page like such:
includes.php:
<?php
function header(){
echo '<div id="header">Welcome</div>';
}
function footer(){
echo '<div id="footer">Goodbye</div>';
}
?>
index.php:
<?php
include_once('includes.php');
header();
echo '<div id="content">Main page body</div>';
footer();
?>