PHP file include general query - php

In a website say abc.com, I have products, about and contact page. These pages have same Header, Footer section. Should we include header and footer with PHP or We copy/paste HTML/CSS coding done in Index page? Which is the best proctise?

Use PHP to include your header and footer, that way if you ever need to make a change you only have to edit one file to update the entire website.

You can set up a cool method like this:
<?php
$title = "This is the page title"; // Make your header use this
require "Header.php";
?>
Put the page contents in between.
<?php
require "Footer.php";
?>

The widely accepted way is to have one central file (e.g. index.php) that has the header and footer (you can choose to include them or just have them in the file), and that central file includes the content files in its content area.
There are other ways to do this, but definitely do not copy&paste the same header/footer to many pages as that will make future editing a nightmare.

You should use PHP to include them.
Also you could think about using a Template-Engine which would offer you some nice features
like Nesting content and defining base-layouts which you can extend.
Twig has those features:
http://twig.sensiolabs.org/

create file like footer.php
Set your code in this and you can add this in index.php page like below
<?php
require_once( 'footer.php' );
?>
same way for header

Related

Single Header and Footer File

I am wanting to create a single file for my header (including menu) and footer so I don't have to edit every single file. Eventually set up a mobile version of our site (including this info in case it is relevant to method).
From what I have read, the easiest way to have single header/menu/footer files is to use PHP and implement a 301 redirect in order to maintain SEO rankings.
Is this correct?
Our web hosting service has Apache handling (.cgi .pl .plx .ppl .perl .shtml).
You can look into a templating engine like Twig.
you can create a base template that includes the header and footer, and extend that in all your other templates to override the body
http://twig.sensiolabs.org/
Maybe I'm oversimplifying the problem, but can you not just write header.php and footer.php and just:
<?php include 'header.php'; ?>
[content here]
<?php include 'footer.php'; ?>
in header part you can write the navigation code and and save as header.php and same thing as footer.php what do you want to write in footer section and save and where do you want to include you can include as
content

How to include header/footer/etc.php with content in between for each page?

I'm looking for an object-oriented solution or possibly a framework that already exists to do what I'm hoping to do for a somewhat small back-end/admin project.
For instance, the system is to manage Projects. So there will be a header/footer/menu on every page. Two pages, for instance, will be projects.php and notes.php.
To include the header and footer on these pages I would normally do:
//header.php
<!-- Header Content Here -->
include menu.php
//projects.php
include header.php
<!-- My Projects Content Here -->
include footer.php
//notes.php
include header.php
<!-- My Projects Content Here -->
include footer.php
and have my links to each page just be '/projects/' or '/notes/'
I've also tried:
//index.php
include header.php
include projects.php
include notes.php
include footer.php
//projects.php
if(isset($_GET['projects']) {
<!-- My Projects Content Here -->
//notes.php
if(isset($_GET['notes']) {
<!-- My Notes Content Here -->
and have the links in my menu be '/index.php?projects' and '/index.php?notes'.
Both are frustrating and don't seem very fluid to me. I would like to upgrade my tactics here but am unsure the best way to go about it.
What's the best way? Is there a Framework that is lightweight and can manage these for me? I would like to keep the links at '/projects/' and '/notes/' but just be able to create an object that calls the content from projects.php and places it in automatically on that link click.
TLDR; - I don't want to have to place header/footer/etc.php as an 'include X' into every PHP page template file manually. I would like for it to know that every page needs it unless otherwise assigned.
Create a file called something like page.php:
if(!empty($_GET['page'])) {
// Could add check to see if file exists
$page = $_GET['page']; // Being "notes" or "projects"
include 'header.php'; // Assuming this includes <head>, header, menu, functions etc.
include $page.'.php'; // Makes notes.php or projects.php
include 'footer.php';
} else {
echo 'Someting went wrong';
}
For links in menu it should be page.php?page=notes or page.php?page=projects
Every file? Are you really sure you want to do that? It might cause data crash.
But if you insist, look for your php.ini file and find auto_prepend_file
http://php.net/manual/en/ini.core.php#ini.auto-prepend-file
auto_prepend_file header.php // For Example
Now that you did that if you don't remove header.php from every program (or make sure include_once is used) then you might have php errors flying around.

How to change many webpages in a website?

I am making website and this website will have more than 20pages.
I am using my template to add a webpage. This template has header and footer, so I just add the body of new page.
But, what if I want to change navigation bar in the header, then I have to change all 20 pages that I already made to correct.
I want to know better way.
I read a book, and it says about "php include" function.
Should I use this function in the header and footer of each webpage to call header and footer file?
If I want to change the navigation bar in the header, all I can do is changing only one header file, then rest of website will be changed.
Is this correct way?
In this case, what do you do?
I am a beginner, so please advise me.
Thank you in advance.
You should build your website as following. The header.html would contain the navigation.
header.html
<!doctype html>
<html>
<head>
...
</head>
<body>
<nav>
...
</nav>
footer.html
<footer>
...
</footer>
</body>
</html>
page.php
<?php
require_once 'header.html';
?>
Your content goes here
<?php
require_once 'footer.html';
?>
You should use require_once so the header and footer will be imported only once per script and if the header or footer cant be found, the script will throw an exception and stop the "application".
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!
Yes this is the correct way or you could just copy your header and footer code to every .html file (if you don't like PHP)
Yes, this is the correct way. Try to think of the DRY principle - don't repeat yourself. Elements of your web page that are common across multiple pages can be coded once, then called in. If you need to update these elements, you update them once and it affects all pages.
You then "include" these elements into your page, and the elements are self contained files. As a basic example you would have header.php
<html>
<head><!-- all of your head meta tags in here--></head>
<body>
<div id="header"><!--your header elements and top menu in here --></div>
and footer.php:
<div id="footer"><!-- your footer elements in here--></div>
</body>
</html>
Don't forget in both header and footer files you can then put dynamic code if you wish. Then for each of your pages you would simply call these files in using include, include_once, require or require_once
<?php
include('header.php');
//this is where your actual page content goes
include('footer.php');
?>
A very basic example, but hopefully that makes sense to you.
Correct.
If you include another php file it will calculate/ask for input/do output (whatever you do in the file) as part of your main file.
If you include a php file that has a function you can later call this function without it showing in your main file. (Saves space in your main file)
So your 20 pages may only need a few lines of body text and the rest is header and footer. This will make changes very easy

html extract header and footer to separate documents

I have several html documents which share the same header and footer and would like to extract header and footer to separate html files and include them to other html documents.
The idea is very simple: when I change something in header, the change has to be made on all pages.
Until now I used PHP include function. But I have read some articles that this has impact on performance.
What is the best way to do this for clean html pages ?
Is the PHP the way to go ?
If it is, should I insert the whole html content into PHP echo ?
There isn't really a good way doing it in pure HTML, since iframe or an ajax-request using Javascript wouldn't be a good solution here.
I would say the best way is to use the PHP include or PHP require-function:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php
You won't see any performance issues. Remember that include is a little bit faster than include_once, since include_once will have to make an extra check if the file has already been included.
You want to include header in all html pages, so php include is better.
no need of echo, just create a header.html(or any html file).
Then use
<?php
include "header.html"; //or name of your headerfile
?>
add this php code to all your pages.Thus all pages will point to same header file, and i dont think it will affect performance. Same idea for footer too :)

Dynamic header with PHP?

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();
?>

Categories