First time poster. Glad this has a newbie section. I asked some basic php questions (on another topic) at my web host's forum and got flamed for not knowing enough. To help learn php I'm trying different things on my website...

I'd like to do the classic design using header/body/footer since my site uses the same header and footer for most pages. There are dozens of examples on php help pages, but I can't get any of them to work. Depending on what example I use, I either get unexpected T-string errors, unexpected T_echo errors or just the page without the header or footer. Is there a definitive method to do this correctly?

Am I overlooking a setting in htaccess or php.ini? I tried to set htaccess to parse htm pages as php, but that prevents regular htm pages from working.

Thanks in advance!

    If all you need is to include a header and a footer in your pages just put

    include '/your/path/header.php';

    Sounds like you're not placing the right path.

      I like to declare functions in my include file that I can then call in the desired place on each page. Here's a fairly simple example.

      /includes/include.php:

      <?php
      // FILE: include.php
      // PURPOSE: standard header and footer functions
      
      // function to output start of each page:
      function page_header($title="Untitled")
      {
         echo <<<END
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
      <html lang='en'>
      <head>
      <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
      <title>$title;</title>
      </head>
      <body>
      END;
         return(TRUE);
      }  // end page_header()
      
      // function to output end of each page:
      function page_footer()
      {
         $year = date('Y');
         echo <<<END
      <p style="text-align: center; font-size: small; border-top: solid 1px gray">
      Copyright &copy; 2005 - $year by Me</p>
      </body>
      </html>
      END;
         return(TRUE);
      }  // end page_footer()
      
      /* 
      NOTE: leaving out the closing PHP tag on this include file will help
      prevent accidentally outputing unwanted newlines/spaces when you
      include it.
      */
      

      Then in any file that I want to use that format:

      <?php
      // load the include file and thus its function definitions:
      include_once $_SERVER['DOCUMENT_ROOT'].'/includes/include.php';
      // output the beginning HTML:
      page_header("Sample Title");
      ?>
      
      <h1>Test Page</h1>
      <p>This is the text unique to this page.</p>
      
      <?php
      // output the ending HTML:
      page_footer();
      ?>
      

        All the files are in the same directory.
        I think I'm having trouble understanding how the browser treats php files.

        Version 1) [INDENT]Put the page header info and css link, menu structure in header.php.
        Index.html only contains html tables,
        footer.php only has the page end tags and some menu javascript.[/INDENT]
        Result: Header, body, and footer are displayed, menu works, but all css is ignored.

        Version 2)[INDENT] Put the css link and page header info in index.php
        Put he mwnu system in header.php and footer.php
        Include header and footer statements in the right places in index.html file as [/INDENT]

        <?php include ("header.php"); ?>

        Result: header.php and footer.php are ignored and only index.html is displayed.

        I'll try the NogDog way next...

          Sorry to barge in on the subject, however. Did you say index.html? Are you parsing your html documents as PHP, if not then that could be your problem. I know a lot of hosting providers do html over php priority meaning if you have index.html and index.php by default index.html will be displayed and also by default index.html cannot parse PHP thus doing a PHP include is useless.

          Do a file check -> include method.

          <?PHP
          if(file_exists('./header.php')) {
          require('./header.php');
          } else {
          print('Blah, this mutha couldn\'t find my header file :( please email me and let me know');
          end; // You can also do exit;
          }
          ?>

          Logically you would do it all in .php or .html files you shouldnt mix extensions unless its .tpl files for templating but thats for a later date when you learn what smarty is 🙂

          <?PHP
          if(file_exists('./header.php')) {
          require('./header.php');
          } else {
          print('Blah, this mutha couldn\'t find my header file :( please email me and let me know');
          end; // You can also do exit;
          }
          // This is all my normal content :)
          ?>
          <div>blah blah blah lets have a blah party</div>
          <?PHP
          if(file_exists('./footer.php')) {
          require('./footer.php');
          } else {
          print('Blah, this mutha couldn\'t find my header file :( please email me and let me know');
          end; // You can also do exit;
          }
          ?>

          Logic:

          1.) Header.php includes all the main template header stuff, beginning global layout such as global wrapper, content wrapper and css files and Javascript.

          2.) Content is what will change on a per page basis

          3.) Footer.php includes the copyright and the ending of global layout stuff such as wrapper and content ends.

            Let me update what I meant for Version 1)
            Put the page header info and css link, menu structure in header.php.
            Index.php only contains html tables,
            footer.php only has the page end tags and some menu javascript.
            Result: Header, body, and footer are displayed, menu works, but all css is ignored.

            I'll have to play with what info to put where. Thanks for everyone's input
            Stuck in meetings all day so no useful work will get done today.

              That sounds like a DOM problem not a PHP problem unless you are incorrectly including your CSS files. The server and the browser will treat them as where they are call in the browser not the server.

              Do an HTTP directly to the CSS file thats the best course for a dynamic situation like this, unless you do a root to. Or remember that The server and browser will treat them as where called from over its callers location. This is why you see alot of sites use http://www.examples.com/styles/v1.css or /styles/v1.css

              Remember this aswell, all images in a CSS file go from where the CSS file is located not from where its called from. So if you have root/images and root/styles and you do v1.css located in styles folder you would do ./../images/image.jpg in the css file.

                Write a Reply...