Is it better to have HTML files for including as header and footer in a PHP application instead of PHP files?
I usually do something like the following to keep things neat and consistent:
<?php require_once('includes/header.inc.php'); ?>
<!-- Html here -->
<?php require_once('includes/footer.inc.php'); ?>
If your webapp is written in php I see no reason to deviate away from the php extension as you may want to add some dynamic content into the include at a later date.
including as an header and footer in
php application is good
like
index.php
include_once('header.php');
// your code regarding this page
include_once('footer.php');
It depends - if you have static header and footer html is enough. If you need there any kind of dynamic, then php comes to fight.
when you are building just some very simple php web page i suggest using require_once to include basic layout of the page. It doesnt matter if you are including html or other php code and you should know whether you would benefit from php or not.
Also including html is faster then including php code which is just calling many echos or prints.
Provided you use secure PHP code, it does not make a (significant) difference. I would suggest separating the main application concerns from your header and footer files, to avoid placing costly operations which may be unecessarily repeated in these files. Some guidelines to address possible security issues can be found here: http://phpsec.org/projects/guide/.
If you are using kinds of include for inclusion, the file type doesn't matter, the PHP code in it will execute, if there is one. Note that this is also a subtle security concern.
Because of this, I don't think the file type is relevant in this. The real question is what have you got in the footer and the header, e.g. PHP processing, database calls, ...?
I personally think that is better to require_once your php header and php footer. Maybe it is a bit slower thant hmtml (but not very relevant) but this unnecessary "optimization" doesn't worth the certainly needed php functions used in your header like session_start() or mysql_close() in your footer.
My personal point of view though :)
There is nothing better in including HTML
moreover - a real life will tell you that you can't use plain HTML anyway.
I've answered similar question recently, with strong reasoning: Using PHP include to separate site content
This is what I do
index.php
<?php
include_once('header.php');
//Your code goes here
include_once('footer.php');
?>
and yes it is a better practice to use php files as header and footer.
Related
I'm currently developing a Web platform app in HTML and PHP stuff. I have to develop a registration/login system, and I'm asking myself a question.
I wrote a login form, and I would like to know if is it better to store it in a php function (with a simple echo("all my login form")) or to store it in a .html file. Obviously, the call or the concerned include() will be written in a if($_session()) statement.
I'm just asking about the correct and logical architecture aspect I should follow.
Because you're designing something serious i recommend you to use include instead of functions. Why?
the html is separated from php stuff
cleaner, highlighted html files in web editors
you avoid once and for all those double quotes/escapes and such
you give designers a way to change your file at will; a designer will not touch PHPs.
easier to update files; think templates
an include() can still go onto a function if you insist :)
IMHO, and regardless of language, an include should never have side effects other than importing variables and functions into the current scope.
Hence if you do use an include, it should contain a function that when called produces output. The mere act of calling include should not generate any output.
Include would "include" new code every time it is called. The normal way is to define a function in a php file and require it, before you use it anywhere with require_once, so you would only load the functiondefinition once and could use it everywhere.
You should put in a .html file, it's always better to separate your applications logic and presentation code.
Also, consider using a template engine.
It is better to include in most cases but it depends on how big the file is that u have called
well calling functions again and again each time you wont even like .
well in one of cases u can call a function if its short and not called on other pages
but if u want to implement a logic everywhere like check login ,logout then include is the way to go .
It is better to use include() for your registration system by which you can get a function just by calling it and you don't have to create it at any other pages .just include it and all work is done.this method reduce the redundancy problem and make you code short & simple.
I use include() but tend to name the files blah.template.php instead of .html.
Then, within the template, I can use simple looping, variable substitution, and simple if-else statements.
If the project is larger, I use a minimal templating class that wraps the call to include(). This class, among other things, wraps the call with ob_start() and ob_get_clean() (I think those are the calls) and allows getting the template's output as a string.
Also, for repetitive bits of HTML code, I think it's legit to use "picture functions" that return the HTML code. Picture functions allow you to parameterize the code and add some logic.
How can I update multiple pages when editing one page? For example, I am editing index.html, and I am changing its footer (which all other pages have). When I change the footer I want the other pages on my domain to be updated as well. Thanks!
This is a major reason why platforms like PHP and ASP.NET exist; they can help you dynamically generate HTML containing common sections like headers and footers that are defined in one place. One simple alternative that may be available for you is Server Side Includes.
Update:
Since you said you're using PHP, rather than having pages with the .html extension, you should write your pages in PHP. That way, you can include separate files containing your headers and footers in each page, so any changes to those only have to be done in one place, i.e.:
<?php include 'header.php'; ?>
<!-- Your page-specific content -->
<?php include 'footer.php'; ?>
There are a few ways to do this. You can use an editor that opens files of a certain project, and use regular expressions to edit.
Or a more common way to achieve this is to use SSI's or Server Side Includes (http://en.wikipedia.org/wiki/Server_Side_Includes). This is more common, and less likely to destroy pages if a regular expression isn't formed properly.
If you're using a different engine (like PHP) you can do something like file includes (require_once, include, etc), to pull the information from a single place. This is similar to SSI's in concept, except it requires another engine to parse the page.
Modern day IDEs for Web Design (DreamWeaver) include this type of functionality within them too. You only need to set it up.
For such automatic updating you can create a function in PHP
function generate_footer() {
echo "<p>Page maintained by Lucifer</p>";
}
and in the HTML code you just have to write this
<div class= "footer">
<?php generate_footer()?>
</div>
this is in php you can use any other server scripting language you wish
Hey all. i was wondering what is the best practice to include a footer
in all of my pages. i mean i have about 1000 of them. should i use the
php "include" function: include 'static_footer.html' or is it a bad practice ?
If you have 1000 .php file pages, you may want to look into using an Model-View-Controller solution (like storing the page information in a database and using Code Igniter or something similar to display the information), or a Content Management System of some description.
But, as far as I know, your best bet would be to use the include() function.
include 'footer.php';
There is always the option of using the auto_append_file directive in php.ini to automatically include a file rather than modifying every single page
I believe it is perfectly fine to use an include function. This way, you'll be mimicking a sort of template engine, and it is a good way to avoid using the same code over and over again.
I would say that is definitely a good idea, as it helps adhere to the DRY principle.
Depending on your scenario, it might be worth looking at some php_value setting in a .htaccess file (if you're in a web environment). You can auto_prepend a file to the output, which would save you adding an include statement to every file. This might not suit your needs, but for simple applications, it can.
i would like to know your point of view on where to position the PHP code on .php page and why?
a) top of the document
b) just above the html elements where i am going to use it.
thank you.
c) In a different file and use a template engine such as smarty
http://www.smarty.net/
Your life will be beautiful and awesome after smarty.
EDIT : I won't downvote other solutions , but it's a very ugly anti-pattern to mix html code with php, you have good, stable and easy solutions to avoid that, use it now or your website will be a big mess of spaghetti code.
Depends on the purpose.
Database query related posts that determine the contents within the part, I call it before there is any input. Also any type of PHP commands that contain raw header information should be presented before any output is made.
Any content related stuff can be positioned anywhere on the page. PHP code is really everywhere - where ever, and however you want to create the HTML from your PHP dynamically.
My pages usually take this structure:
<?
include 'start.php';
$pagetitle = 'the services we offer (branding, web, print etc.)';
$metatitle = 'Our Creative Services (branding/logo, web, print)';
$scriptinclude = 'whatwedo.js';
include 'header.php'; // contains the <body><head></head><body> and a few more elements to start the header/menubar etc.
?>
<div class="full_grid" id="index_slide">
// content here, mixed with PHP if you like...
</div>
<?
include 'footer.php'; // contains the footer HTML, as well as </body></html> etc. to wrap things up.
?>
I put as much code as I can at the top. And only use php withi HTML where I need loops or output data.
This gives me a better overview of the code and it's easier to work with.
Keep your code and HTML as separate as possible. Have them in entirely separate files where you can.
Your HTML should be as much pure HTML as possible, and your PHP code should contain as little HTML as possible.
Obviously, you're producing a web page, so there will have to be some mixing, but keep it as limited as possible: The only code you should mix in with your HTML should be the one-liners to place specific bits of PHP-generated code into your HTML template.
It completely depends what you're doing with it. Personal preference for me is to create any functions I need at the top and then scatter inline php throughout the document calling the functions at the top of the page.
If something needs calculating and it can be done at the top, it's much easier to read and debug if you keep it all in one place. And keeping this the same throughout all your files will help too. What you could do is just include a config file at the top of the page with any site-wide functions you need too, so you don't have to copy and paste through all your files.
If you are only using one PHP file then definitely put all PHP code at the top, then the HTML below with variables where necessary.
For example, $title = 'Page title'; at the top of the page, then <h1><?=$title?></h1> in the HTML portion of the page.
However a better solution is to have two (or more) files. The main one contains all the PHP logic to grab/process data, while the second one is a "view" file containing mostly HTML. The simply include your view file from the main PHP file.
I have a website which consists of a bunch of static HTML pages. Obviously there's a lot of duplication among these (header, menu, etc). The hosting company I plan to use supports PHP, which I know nothing about. Presumably PHP provides some sort of #include mechanism, but what changes to I need to make to my HTML pages to use it?
For example, suppose I have a page like this
index.html
<html>
<head></head>
<body>
<h1>My Common Header</h1>
</body>
</html>
Obviously I need to move the common part into it's own file:
header.html
<h1>My Common Header</h1>
Given the example above (and assuming all files are in the same directory):
What do I add within the body tag to get header.html included?
Do I need to rename index.html or add some special tags to indicate that it's a .php file?
Do I need to make any changes to header.html?
Update: I want to emphasise that my objective here is simply to find the lowest-friction means of reducing duplication among static HTML files. I'm a bit reluctant to go down the server side includes route because I don't yet know what type of server (IIS/Apache) I'll be hosting the files on, and whether includes will be turned on or off. I was drawn towards PHP only because it is about the only thing I can presume will be available that will be able to do the job. Thanks for the responses.
Thanks,
Donal
You are looking for include (or one of its derivative such as include_once, require, require_once):
header.php
<h1>My Common Header</h1>
index.php
<html>
<head></head>
<body>
<?php include('header.php'); ?>
</body>
</html>
And so on, for your footer for example.
You don't need to use PHP to get this functionality, and it's generally a bad idea to do so due to potential security concerns. Essentially, you're swatting a gnat with a nuclear bomb. If you're not using a dynamic language, then you're looking for server side includes.
In IIS, for instance:
<!--#include virtual="file.inc"-->
Be aware that you often have to configure the server to utilize them, as this feature is often turned off by default. Both IIS and Apache support server side includes, but they use different configurations.
You can find more information here:
Server Side Includes
EDIT: I don't mean that it's a bad idea to use PHP, just using PHP solely for including other files. It creates a larger attack surface by bringing PHP into the mix when it's not needed, thus the potential for security issues when the functionality of PHP is not required.
EDIT2: I think it's a bad idea to assume you won't be a target because of your size, and thus you can ignore security. Most sites are compromised by automated worms and turned into malware hosts, spam zombies, or pirated software/media servers. Apart from the fact that you might end up being involved with infecting others, your site can become blacklisted and it can cost you real money in bandwidth overage charges. We're talking hundreds or thousands of dollars.
Just because you're a small site doesn't make you any less of a target. Just being on the internet makes you a target.
Forget doing it on the server altogether.
If all you really want to do is maintain some static pages -- and don't anticipate ever having to really use PHP -- I'd just do it with Dreamweaver, which will allow you create and manage templates and variable content on your end.
No includes needed. No templating engine needed. (These would be overkill for what you are trying to accomplish.)
You should first change the file extensions of index and header to be .php, then you can do:
<html>
<head></head>
<body>
<? include 'header.php'; ?>
</body>
</html>
And your header.php file just has
<h1>My Common Header</h1>
While you can just use the "include", "require", or "require_once" directives to include things in one page, you might have better luck with a template engine like Smarty
While using an include file for the header is a solution I went a different route when I faced the problem several years back: I wanted all pages to use the same layout (which I assume is rather common ;-). Thus, as I only wanted to change the content of the page I made the page content the file that gets included and have a master template file that includes header and footer. For setting the page to be included I resorted to creating quite small php scripts that only set a variable that holds the page to get included. In some cases the page can also get named by a GET parameter. Of course this requires proper validation of that parameter. In the long run I don't need to worry about the HTML itself anymore -- all I do is write small snippets (which should be complete for themselves of course) that get included.
A possibly even better solution would be to use an existing template framework. Due to the contraints I had back then I wasn't able to do so, but I would do it when facing the same issue again.
Back in the day, I used SSIs (the "<!--#include virtual="file.inc"-->" method described above by Mystere Man) quite a bit for static HTML pages and I would definitely recommend using that.
However if you want to eliminate any uncertainty about whether support for that will be enabled on the server, you could develop your separate files locally and merge them into the resulting files before uploading to your server. Dreamweaver, for example, supports doing this in a seamless fashion.
Or you could do it yourself with a rather simple script in your language of choice by doing simple string replacement on markers in the files, replacing {{{include-header}}} with the contents of a "header.html" file and so on.
Edit
Oops! Somehow I didn't see Clayton's post with the same note about Dreamweaver.
OK this is a semi-programming related question only.
PHP does have include(), which is really easy to use, but it doesn't contribute to future maintainability. I wouldn't recommend it, especially for big sites.
I'm a pro-frameworks. I've used CodeIgniter, CakePHP and even Smarty template engine. If you are serious about PHP, do consider CakePHP. There's this "layouts" concept where you frame your header, footer, css, javascript outside of the main content; e.g. for the "about us" page, your content would be something like:
This is an about us page that tells you a whole bunch of stuff about us...
CakePHP takes this this content, and wraps your layout around it:
header
css
javascript
This is an about us page that tells you a whole bunch of stuff about us...
footer