Include header, how to? - php

<html>
<head>
<!--#include virtual="header.php" -->
</head>
<body>
aa
</body>
</html>
I'm trying to put the header in a different file and call it with an include. But I can't make it work. Right now the only text in the header is the word header. The header is at: www.chusmix.com/game/header.php how can I call it? I don't care if I use include virtual, I just don't know how to do it.
Thanks

<?php include('header.php'); ?>
Your include needs to be in PHP -- the way you attempted to include it is a very old method of SSI (sever side includes) and has nothing to do with PHP.
In order to include this way, the page doing the including must be parsed as PHP, and on most servers that means ending the filename with a .php extension. You can have plain HTML in a .php file so you can just rename the file (if its not already) to .php and then include the line I wrote above.

include('header.php')
include_once('header.php')
require('header.php')
require_once('header.php')
take your pick ;)
To clarify: include and require differ only as much, that if file does not exist, include will only display warning, while require will throw fatal error.
The *_once functions on the other hands make sure file is included only once. Try including the same file two times, you'll get an error. *_once makes sure the 'including' is done only once.
On the other hand, *_once functions bring you a bit of overhead, but that should be taken in account when writing bigger applications.

You need to include it in the php code:
include (header.php);

Related

Which naming convention is correct for included HTML code fragment files?

I do have an index.php file which includes different parts of the page via PHP.
For example this includes the <head> section of the HTML page into the index.php file:
<!doctype html>
<html>
<head>
<?php include($_SERVER['DOCUMENT_ROOT']."/PATH-TO-FILE/head.php"); ?>
</head>
<body>
...
</body>
</html>
The content of "head.php" may be something like this:
<meta charset="utf-8">
<title>Title of page</title>
<meta name="description" content="Description text...">
...
<link rel="stylesheet" href="stylesheet.css">
...
and so on...
Technically "head.php" is not a PHP file because it does not contain any PHP code. Neither is it a valid HTML document. It is just a HTML code fragment.
Until now I have always named these HTML code fragment files either *.html or *.php.
My question is whether this is correct or not?
Is it advisable to not give it a file extension at all? Instead of "head.php" simply "head"?
Please keep in mind that certain directives can be set in the server's .htaccess file concerning the caching and expiration of *.html and *.php files. Removing the file extension and renaming it from "head.php" to "head" may exclude it from the mentioned directives in the .htaccess file.
While searching I found this question here on StackOverflow:
What extension should I use for files containing fragments of HTML
But that question was asked 6 years ago and there are so many different file extensions mentioned there, it's difficult to say which one to use.
Can anyone give an updated answer concerning this issue?
If you use include, I'd strongly recommend using *.php. include executes the code in it, even if it's raw HTML. (in that case it's just output without much further processing)
Hence, use *.php for files output that way. (Else you might get also bad highlighting in editors when using <?php one day in it for some reason; also, if someone opens the *.html file directly, he'll see the raw code then)
In case you are using e.g. readfile(), then use *.html to highlight that it is raw HTML code only, nothing ever will be executed.
It basically depends on how you include the file.
P.s.: To no extension at all. Not really advisable, that's usually what you use for binary files and directories. Note that extensions really just are to give you oversight what happens.
I would leave as php. Your are including that file with php. Itself head file is useless as its own.
What if you add some php to that file, for further development? Add php extension back not an efficient lifecycle.
More importan is where you store those files. Putting them under a templates folder will tell to anyone, that those files are templates.

Using PHP Virtual() to include another PHP file fails

I have HTML and PHP files that include the "header" HTML for my website. In HTML files I include this header using
<!--#include virtual="/top.ssi" -->.
top.ssi in turn includes other files:
<!--#include virtual="/navbar.ssi" -->
<!--#include virtual="/advertising/slider_advertising.ssi" -->
and slider_advertising.ssi includes:
<!--#include virtual="/advertising/advertising.php" -->
This is the critical file as it prepares advertising data for display.
All the above works great when run from HTML files.
Now I have some PHP-driven webpages (Logon.php) that also want to display the header of the website using top.ssi. This code starts with:
<!DOCTYPE HTML>
<html>
<head><title>Login to HVmusic</title>
<?php
virtual("/top.ssi");
...
Here is where the problem comes in. The virtual("/top.ssi"); executes OK, but stops executing after it encounters the PHP file that is included in top.ssi <!--#include virtual="/advertising/advertising.php" -->. The output from advertising.php is displayed and then the PHP output from logon.php stops. So the output stops without even displaying the entire header.
If I remove the statement for <!--#include virtual="/advertising/advertising.php" --> then the logon.php displays it's page normally (without, or course, that bit of the header displayed by advertising.php). So this tells me that PHP is having a problem with a virtual() file that includes another PHP file.
Is there any way to fix this? Is it some known restriction in PHP? I've been googling for two days and can find no mention of this issue. Thanks for any help you can offer.
Thanks for the tips, that clarified my thinking. I have just converted all my web pages to PHP and this solved the issue. Most of my pages were already PHP, so it wasn't much work to convert the HTML pages. All the include files I used were easily converted to use <? php require "filename"; ?> instead of <!--#include virtual="filename" -->
The underlying problem probably has to do with some conflict about running in PHP, then calling virtual() which calls an apache instance, which then calls another PHP instance to process the PHP include files I used.

PHP Include - full url

I've been using the php include function for my navbar for my website. It works well but....
My HTML says
<?php include 'include/navbar.html'; ?>
Now if I have a HTML page in /techpages/toptech.html so I would like to change my php include to <?php include 'http://example.com/include/navbar.html'; ?>. The problem is I get heaps of errors then.
Can someone help me?
Can someone answer one of these questions?
How can I make the PHP include function work with a full URL?
HTML has ../ to go into the parent folder. Is there a CSS equivilant?
Any help would be much appreciated.
Here are two approaches which I think will work for what I imagine you are trying to achieve:
You could include via include $_SERVER['DOCUMENT_ROOT'].'/include/navbar.html'; - this will always include the same file regardless of in what file in what directory you put the include
PHP has an include_path which specifies where to look for file-includes, you can add the /include directory in this include_path and from there on always include via include 'navbar.html'. But to be able to do this you have to have permission to access/modi the php.ini...
One of the options of doing that is:
include($_SERVER['DOCUMENT_ROOT']."/include/navbar.php");

In a PHP webpage, reading text from an external PHP to make updating content across pages easy

I'm writing a small website that has multiple pages. I'd like to have the same footers on each page, but I don't want to manually update 10 pages of HTML everyday. I'd like to put a PHP call to an external file in each HTML page (now .php pages, thanks to #br14np) so that when I update the PHP file, all the pages - when loaded - will show the same footer text.
<p><?php footertext.php ?></p>
is my wild guess at loading the content in the file of the afformentioned name but to no avail. (In footertext.php the code is: <?php print("Test numba one") ?>).
How can I go about doing this? I'd prefer an answer involving PHP.
UPDATE:
This is the exact code I'm using. Everything is in the same directory.
Main File:
<html>
<head>
</head>
<body>
<p> Content: <?php include "footertext.php ?></p>
</body>
</html>
Footer Content:
echo 'Test numba TWO!';
Use the include function. Just give it the path to your file. Example:
<?php include "footertext.php"; ?>
There are a few other functions that do similar things, such as require_once(). You can read more about that here.
Response to update
You're missing closing quotation marks after "footertext.php. Another tip that may help this situation is to turn on php error reporting. This will display any syntax or other errors on your page. Just insert the following code at the very top of your pages:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
Also make sure you have opening and closing php tags (<?php ... ?>) in your footertext.php file.
The best fit solution for your query is include or require functions of php. now you need to identify which one out of those are your choices based on their functional behavior.
PHP include and require Statements
In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.
The include and require statements are used to insert useful codes written in other files, in the flow of execution.
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the
include will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename.ext';
or
require 'filename';
You may like to go through the details of
Include,
Require,
Require_once &
Include_once.
Enjoy!
Anand Chavan

Breaking up PHP Websites

I am wondering how I can break up my index.php homepage to multiple php pages (i.e. header.php, footer.php) and build a working index.php page using those separate php pages. I know WordPress uses this with different functions like:
GetHeader();
GetFoodter();
But when I tried to use those functions, it errors. I am guessing they are not native functions to PHP.
What would I need to do to get this functionality?
include 'header.php';
include 'footer.php';
Go with an MVC framework like Zend's. That way you'll keep more maintainable code.
You could do the following:
<?php
include('header.php');
// Template Processing Code
include('footer.php');
?>
The include() statement includes and evaluates the specified file.
so if you create index.php as:
<?php
include("1.php"); include("2.php"); include("3.php");
?>
processing it will combine three php files (result of parsing them by php) into output of your index.php ... check more at http://pl.php.net/manual/pl/function.include.php
Also, if i recall correctly, you can also use
<?php
require('filename');
?>
the difference being, if php can't find the file you want to include, it will stop right there instead of keep excecuting the script...
If your server is configured accordingly, you can use PHP's built in auto append/prepend settings and set it in a .htaccess file:
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"
Info:
www.php.net/manual/en/configuration.changes.php#configuration.changes.apache
www.php.net/ini.core#ini.auto-prepend-file
www.php.net/ini.core#ini.auto-append-file
I realize this is an old question, which already has a perfectly valid accepted answer, but I wanted to add a little more information.
While include 'file.php'; is fine on it's own, there are benefits to wrapping these sorts of things up in functions, such as providing scope.
I'm somewhat new to PHP, so last night I was playing with breaking things into files such as 'header.php', 'footer.php', 'menu.php' for the first time.
One issue I had was that I wanted to have the menu item for a page/section highlighted differently when you were on that page or in that section. I.e. the same way 'Questions' is highlighted in orange on this page on StackOverflow. I could define a variable on each page which would be used in the include, but this made the variable sort of global. If you wrap the include in a function, you can define variables with local scope to handle it.
You could also look into a template engine like Smarty. That way you define the the header and footer and all other common elements in a single file, then fill in the rest through smaller templates or direct output.
Use include statements to just include those files to your Page
I think it's
include '[filename]'

Categories