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
Related
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.
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
I am a beginner in web development. I can use PHP server sides scripting.
I have the following design:
Pages 5
All have the same header and footer
Is it possible to keep the header as well as the footer in different files so that every page will use the header/footer as components.
Yes you can use php include function.
You can write something like this. This header and footer will be constant for your all the 5 page. and in each page you can change your code.
<?php
include '/Path/To/header.php';
// Here you can write your page script without header and footer.
include '/Path/To/footer.php';
?>
I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern
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