Should I use PHP for header and footer?

Discussion in 'Programming' started by ElectCon, Jan 18, 2013.

  1. #1
    Hello everyone, nice place you have here :)


    I have a small website for my business, right now it has 5 pages but I would like to get it up to 10. The most it will ever have is 15 or so. It is hosting on GoDaddy.

    Every page has the same exact header, sidebar, and footer. Since it's a pain right now when I want to change something, it's only going to get harder when I have more pages.

    I did some searching on the ways to have a single file for the header and another for the footer so that you only have to change it once. It seems as if doing it with PHP is the best way, but a lot of what I read was 1-3 years old. I am not sure if anything changed.

    For example, one article said to use this:

    <?php include("header.php"); ?>
    Code (markup):
    Another article said to use this:

    <?php include 'header.php' ?>
    Code (markup):
    Does it matter? Will one be supported in more browsers?


    A second question I had is about changing all my existing pages into .php. My website has already been index by many search engines. I read that I could keep the pages as .htm if I put this into an .htaccess file:

    AddType application/x-httpd-php .html .htm
    Code (markup):
    Is that true? Is there any disadvantage in doing this?


    Or should I be looking to do this in a totally different method instead of PHP?


    Thank you very much for your help.
     
    ElectCon, Jan 18, 2013 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    <?php include 'header.php' ?> works. Adding the parentheses won't make it not work. It works on the server, not in the browser, so it will work in all browsers either way, as long as you have PHP on the server. For GoDaddy that means a Linux server, not a Windows server. If you're currently on a Windows server, they can move you. Call tech support. If you don't know which server you're on, create a file called test.php. Put this in it.
    
    <?php
    phpinfo();
    ?>
    
    Code (markup):
    Put the page on your site. Go to http://www.yoursite.com/test.php (where http://www.yoursite.com is your site address). If you have PHP running you'll see a long page listing what functions are available. (You don't care what they are, as long as you get a listing.) If you don't have PHP, you'll just see your file. Adding PHP as an application type won't do anything if PHP isn't running on the site.
     
    Rukbat, Jan 18, 2013 IP
  3. ElectCon

    ElectCon Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply.

    I am on a Linux server and I have PHP.

    So I guess doing this with PHP is the best way?


    What about renaming the pages to .php? Can I get away with leaving them as .htm or will that cause some type of possible problem?
     
    Last edited: Jan 18, 2013
    ElectCon, Jan 18, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    for your purpose the ' or " doesn't matter.

    if you are using variables it does matter

    '$variable' doesn't work
    '' . $variable . '' does work
    "$variable" does work

    :) hope you learned something!
     
    EricBruggema, Jan 18, 2013 IP
  5. DrShirts

    DrShirts Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #5
    I use this trick for menus
    you can do <? @include "menu.php"; ?> and then use folders for different sub areas
    that way a different menu.php loads for each folder

    @ suppresses any errors incase there is no menu there!
     
    DrShirts, Jan 20, 2013 IP
  6. DrShirts

    DrShirts Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Out of the box Apache (if you use that) will not process .htm and .html pages as php, so it will cause a problem

    you CAN howerver change the handler in the apache config to something like
    addhandler x-httpd-php5 .php .htm .html

    If your on share hosting ... forget about it. you cant do it. Although you could creatively ad an .htaccess rewrite rule to redirect all .htm requests to their php equivalent
    so users can still use .htm but they will actualy be loading .php

    RewriteRule ^(.*).htm /$1.php [R=301,QSA]

    --
    Like my answer? Like my post! Thanks.
     
    DrShirts, Jan 20, 2013 IP
  7. experiencesid

    experiencesid Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    if you just use php files for your header footer as well as side bar then you dont have to worry about adding php scripts if needed and also no need to write any htaccess script.
     
    experiencesid, Jan 21, 2013 IP
  8. DrShirts

    DrShirts Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Last time i checked creating a .htm file and putting html code in there will not work... So im confused about your comment.
     
    DrShirts, Jan 21, 2013 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    .htm and .html are the same thing, so putting HTML in an .htm or .html file will work. Putting PHP in an .html (or .htm) file won't work, but putting HTML in a .php file will. (Either put it outside the php tags or echo it - either one will give you the same result.)
     
    Rukbat, Jan 21, 2013 IP
  10. BncApps

    BncApps Member

    Messages:
    149
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #10
    This is exactly how I do it. There is no sense in modifying each page when you need to change the header or footer. Or, if you have a central config file included in easch page, just have a variable in there containing the content for the header and footer, then Echo it in place :)
     
    BncApps, Jan 23, 2013 IP
  11. i8k

    i8k Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #11
    To better streamline my code, especially when working on projects for clients, a great way to go about headers and footers is to use uri segments to determine where you are and then use if statements to spit out appropriate header and footer info based on the current uri.

    Example:

    Say you have an about us page (about.php). In header.php you can check the uri for the presence of about.php and if true, you can then build the meta data based on that to provide a specific title, description, keywords, etc, etc.

    This way there is only 1 header and 1 footer file and they can generate custom content for any page that utilizes them.
     
    i8k, Jan 24, 2013 IP
  12. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #12
    It works to use header/footer and navi in includes()
    for basic sites I make a Template that has all the include file calls and then I only have to edit the Meta section/Titles. and the body content. It's a fast easy way to get pages up. you can also create an interface Text area for body content and assign it to various article writers etc. They only have to submit the text area and its posted to the body content of the correct page, by writing it to a fwrite txt file and having the includes include the text there. The CSS formatting will already be on the Template page. Yet the writers can add tags like <b> <i> <u> etc if necessary into the text area. Basic faster-function stuffs
     
    ezprint2008, Jan 24, 2013 IP