Hi,

I've been trying to figure this out and it's bugging the hell out of me!

I have a header.php file included on all my pages. I want to know if I can have the page title and meta tags in the header.php file, that way when I include it in every page, the page title and meta tags will be included too.

I searched around and found a few examples, but they seem to be the opposite of what I want. The whole point of this is so that I DON'T have to change the page title and meta tags on EVERY single page of my site. Rather, I would do it with an include. It doesn't necessarily have to be in the header.php file, but as long as I can include the page title and meta tags into every page.

Can someone help me out?

Thanks.

    i suggest three files.... header.php footer.php and yourfile.php

    <!-- HEADER.php -->
    <html>
    <head>
      <title>SITE TITL</title>
    </head>
    <body>
    
    <?php  include('/path/to/header.php') ?>
    
    whatever else goes here : the acutal page<b>!</b>
    
    <?php  include('/path/to/footer.php') ?>
    
    <!-- FOOTER.php -->
    </body>
    </html>
    

      That's not what I'm asking. I already have 3 files (header.php, footer.php, mypage.php). I have no problem including them, but that's not what I want.

      I want to know about meta tags being in an include file also. 😉

        How bout something like this:

        ------ header.php --------

        <?php

        if (!$title) {
        $title = "the default page title";
        }

        ?>

        <html>
        <head>
        <title><?php print ($title); ?></title>
        </head>
        <body>

        ----- end header.php ---------

        ------- pagename.php -------

        <?php

        ob_start();

        $mytitle = "this is the pages title";
        $keywords = "this is the pages meta keywords";

        print ("this is mypage content");

        $contents = ob_get_contents();
        ob_end_clean()

        include ("header.php");

        print ($contents);

        include ("footer.php");

        ?>

        ------ end pagename.php --------

          Either you're not reading the answers correctly, or you're not asking it right!

          Seems to me ednark did answer your question.

          Metat ags go in the head tag.


          <head>
          <meta>
          </head>

          So you can have one set of meta tags for all your pages that use header.php

          If you wanna make the meta tags dynamic too, then you have to add some code to it.

          HTH
          kamy

            kamy99 is right on the money

            your <meta> headers must be written into the <head> block contained in header.php

            alternatively you can call those headers with the header() function

            only problem is all header() call must be made before anything else is actually printed or echoed or in any way written to the user...

            this is why you are using the ob functions.. to stop the output untill your headers are written... but these are unecessary if you are writing the <meta> tags into the html in the <head> block. the ob functions are only necessary if you wish to include some header() calls after you have written output... the ob_ makes sure the output you have written isn't sent yet

              Well I've figured it out. I put the page title and meta tags into my header.php file and everything shows up ok!

              My other problem is that I have the header.php include inside of a table at the top of every page. Now, when I view my page source, I have some HTML first, then I see the page title and the meta tags. My question is, do the meta tags have to be at the top for them to be picked up by search engines? Kind of hard to explain. For example:

              page.php:

              HTML
              <table>
              then the coding for my header.php shows up here.

              So the meta tags are down a few lines, is that OK? I'm guessing there shouldn't be a problem?

              Thanks for all the help.

                the <meta> tags must be inside the <head> tag...

                depending on how the search engine works it may or may not find them... but it will definately if they are in the <head> tag

                  a month later

                  Hi,
                  I'm seaching the web the wole morning now, but can't find a desent answer.
                  How about the indexing at search enginges when you inlcude you metatags in an include?
                  Some say spiders for example don't read the included data?!

                  So is it wise to use this method at all?

                  Greetings,
                  John.

                    The spiders can't detect if the text has come from ans include or not - they don't see the PHP code just the finished product. To the search engine it just looks like normal page.

                    I generate meta tags with PHP all the time - I have a common INDEX.PHP?REGION={num} and depending on a REGION variable generate different titles, descriptions and META tags.

                      Write a Reply...