How to include php headers and footers on html site
# July 9, 2013 at 11:41 am
hrechkaness you’re the MAN. I have two more weeks before graduation in a Web MultiMedia Design class. I love it from UX to php. The instructors just touch on php the rest is on me.
Just reading your post and comments have got me where I wanted to be with the include tag.
Thanks# August 3, 2014 at 10:57 am
< ?php include 'header.php'; ?>and
< ?php include 'footer.php'; ?>in this post no longer works.
What do you mean by this? What are you trying to do, and what is the problem you are encountering? Please describe what you are trying to do, what actually happens, and any error messages.
Please update post to
<?php include('header.php'); ?>and
<?php include('footer.php'); ?>
require, et.al.) are language constructs, not functions. Placing parenthesis around the filename is not necessary, and, in fact, can be misleading.
The only thing wrong, syntax-wise, with your example above was the space between the
?php. I don’t know if that is an actual typo in your code, or just how you wrote it here.
<?php include 'header.php'; ?> <!-- HTML content --> <?php include 'footer.php'; ?># December 27, 2015 at 7:52 am
Mine works like this. Most pages will have a file with these statements:
$directoryToPageGroups = 'pageparts'; $thisPageGroup='/002_list_of_cats'; $common='/000-common'; include $common.'/my_functions_and_variables.php'; include $thisPageGroup.'/015_start_html.php'; include $thisPageGroup.'/020_start_body.php'; include $common.'/800_footer.php'; include $common.'/899_finish_page.php';
So, I have folder with folders, each containing a set of page parts for each page. The page part files all have the same names, but their folder names show which is which. Then inside those page parts, there are a lot more include statements, or links to css and js. Here is part of what goes into most 015_start_html.php files.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <? echo $copyright;?> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="favicon.ico"> <title><?=basename($baseURL).'/'.basename($_SERVER['REQUEST_URI'])?></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <? $timeChecks=addTimeCheck($timeChecks, number_format(microtime(true),6,'.',''), __LINE__, basename(__FILE__));?> <link href="<?= $cssURL?>css.page.layout.css" rel="stylesheet"> <link href="<?= $cssURL?>css.footer.css" rel="stylesheet"> <link href="<?= $cssURL?>css.css" rel="stylesheet"> <link href="<?= $cssURL?>css.menus.css" rel="stylesheet"> <link href="<?= $cssURL?>wg.css" rel="stylesheet"> <!--link href="<?= $cssURL?>fonts.css" rel="stylesheet"--> <? include_once $basePath . 'lib/SqlFormatter.php'; include $cssPath . 'styles-for-stripes.php'; include $cssPath . 'randomized-orange-gradient.php'; include $cssPath . 'css.php'; ?> <link href="<?= $cssURL?>stripes.css.all.rights.reserved.css" rel="stylesheet"> <link href="<?= $cssURL?>random.gradients.css.all.rights.reserved.css" rel="stylesheet"> <link href="<?= $thisPlayCssURL?>thisPlay.css" rel="stylesheet"> <link href="<?= $cssURL?>cssResponsio.css" rel="stylesheet"> <link href="<?= $thisPlayCssURL?>cssResponsio.css" rel="stylesheet">
All those variables are set by my_functions_and_variables.php, called at the beginning. Those variables automatically adjust to the url and path of where the code is running, so the app can be moved and run without having to reconfigure.
COMMENTS WORTH AT LEAST WHAT YOU ARE PAYING FOR THEM, probably not more.
Organizing files for a website is pretty big topic. People go about it all kinds of ways.
One of the main objectives always is having files that can be reused and isolating the code that is unique, so you can combine the unique stuff with the stuff that is reused as efficiently as possible. Stuff that gets reused usually includes that top and bottom of a page, also the style sheets, shared functions, especially functions related to interacting with a database or any other systems that your web pages connect with, library elements, such as bootstrap or jquery.
I think it would be hard to do this well without having worked on some good examples that others have put together first. There are elaborate frameworks that provide solutions to this. I have developed a scheme Iike, but the big problem with doing my own is that it looks weird to every programmer who sees it and is looking for what they are accustomed to finding.
You must be logged in to reply to this topic.