Library tutorials & articles

Simple PHP includes

Introduction

Nobody likes frames. But if you're not using frames, you have to update every page of your site every time you modify your menu. Don't you wish there was another way? Well, there is. Simple PHP includes allow you to create a professional, frameless site without too much work. Want to learn how? Read on...

PHP includes are very simple and really practical. How they are used is they allow you to use one line of code in every page that you want your menu on in place of say 30 - 50. Also, you never have to edit this code once it's there, you can just edit your menu file. They work by taking all the content of a file and putting it into the file being viewed at the location where the INCLUDE tag is.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Simple PHP includes.

  2. 18 Jan 2003 at 04:46

    Hi!


    Thanks for the great tutorial! It really helped me a lot.
    One question still:
    If i want include more than 1 (txt)file with just 1 link, (for instance text1.txt in tablecell A, and text2.txt in Table cell B, and perhaps a picture in Table cell C) how do i put that in the hyperlink?


    Hope you can help me out on this one.


    Thanks again!

  3. 22 Jan 2003 at 11:41

    I'm new to PHP and would like to use virtual paths for my PHP includes like I do in ASP:


    <!--#include virtual="/common/myinclude.asp"-->


    I've tried:


    <$ include ("/common/myinclude.php") ?>


    I get an error message "Unexpected T_INCLUDE at line 3..."


    Thanks!


    Liz
    PS Your tutorial is well written. You'll have a book contract before long!

  4. 22 Jan 2003 at 14:36

    I discovered that the virtual root of my Web account was different from what I thought it was.


    The root starts several directories below where I thought it started.


    I'm new to the particular hosting service where my site is!


    Thanks again.


    Liz


  5. 08 Mar 2003 at 15:10

    Oh, yeah, what a great tutorial - not! There's no security whatsoever and its capability is low. So, if you're smart (Not to mention a master PHP coder Hint Hint), use the following:


    Code:

    <?php
       if(!empty($HTTPGETVARS[page]) and fileexists("./$HTTPGETVARS[page].inc")) {
           include("./$HTTP
    GET_VARS[page].inc");
       } else {
           include("./main.inc");
       }
    ?>


    The include will be main.inc if the variable page is not defined. Now, you can make all your content with a .inc extension and include it with the one page that has this code. For links, use http://yoursite/index.php?page=NameOfPage.inc


    There you go.

  6. 29 Mar 2003 at 15:36

    Quote:
    [1]Posted by Zidane-Tribal on 8 Mar 2003 03:10 PM[/1]
    The include will be main.inc if the variable page is not defined. Now, you can make all your content with a .inc extension and include it with the one page that has this code. For links, use http://yoursite/index.php?page=NameOfPage.inc


    There you go.



    FYI, I thought the original tutorial was helpful but what Zidane added is certainly more practical (eventhough his tone was unnessecary).  Anyway, slight correction.  To reference the pages, you would use http://yoursite.index.php?page=NameOfPage   (no need for the .inc).


    Enjoy!

  7. 09 Apr 2003 at 11:24

    zidane,


    your reply was so helpful, that i've built a site around it and registered just to thank you.


    so... thank you.  




    fame

  8. 10 Jul 2003 at 19:50

    If security is not an issue, what is wrong with simply making you header, footer, sidebar content as .txt includes for example and just use the <?php ;?> where you want them to show up in your .php files that contain main content?
    Also, where can I go to learn how to manage the whole site with PHP? I find a lot of stuff is either too hard to understand or doesn't explain all the terms it uses.

  9. 12 Sep 2003 at 16:19

    I gave the "corrected" code a try, but I'm using Javascript rollovers in the menu bar and now the buttons pause before loading, even though the script preloads them--and not only that, but it does that for every "page" in the site.




    Is there any way to check for cached versions of my preloaded buttons so it doesn't have to load them fresh for every page?  Maybe some kind of If-Then-Else instead of using the

    Code:
    <? include("menu.inc") ?>
    ???


    Shizuka

  10. 03 Sep 2004 at 17:53

    email me , a your instructions as follows  just gives you a name for the first page when creating your example for the first half of the code but what about the second half of the code with the other half of the table what do you name it and on the third part of the code you dont say what to name it either, i am a newbie and and reading your instructions for this example loses me real quick cause you leave most of it out this intruction and wher to put or use the link in php??? im lost for a newbie can u give plain er instructions please php for dummies, lol

  11. 05 Sep 2004 at 21:02

    Try using this:


    include $SERVER[DOCUMENTROOT]."/common/yourfile.php";


    This should do it.


    Breckenridge Guy!

Leave a comment

Sign in or Join us (it's free).

Jamie Lindgren Uhm... I dunno why I'm typing this, cuz nobody is gonna read it probably, but anyway... I am (guess my age!! haha... 14) years old and I like programming, web design, and graphic design. I'm pursui...

Related podcasts

  • The SitePoint Podcast: Compuwhatnow?

    Published 1 year ago, running time 0h35m

    In this show, the guys discuss Compuserve shutting down. You remember Compuserve, right? Also: US states gang up on Amazon with an “affiliate tax”, PHP 5.3 and Firefox 3.5 are released on the same day, the debate raging over the standard format for video in HTML 5, and XHTML 2 rides off into the ... php, html, on, can

Related jobs

Events coming up

  • Aug 23

    DrupalCon Copenhagen 2010

    Copenhagen , Denmark

    Rasmus Lerdorf, author of the original PHP scripting language, will be joining Drupal Founder, Dries Buytaert as a keynote speaker at the conference. Author and developer Jeremy Keith, will also be presenting on HTML5. All other presentations have been voted on by members of the Drupal Community: http://cph2010.drupal.org/news/initial-program . The main venue will be set aside for an Open Source ‘unconference’/bar camp on 23 August.

Want to stay in touch with what's going on? Follow us on twitter or Facebook!