php include design question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    php include design question

    so, i'm making a website. let's say i have header.php, footer.php and
    content.php.

    now in index.php I simply want to include the 3 pages. easy enough to
    do.
    but let's say the user navigates to mysite.com/content.php. now the
    header and footer will appear to be missing.
    so now the question is, how can i include the header and footer in
    content.php only if the page isn't already nested?

    i suppose i can include a variable in header.php that basically says
    "i've been included", and then in content.php if this variable isn't
    set, i could include them... is this the best/most elegant approach?
  • J2Be.com

    #2
    Re: php include design question


    "Mark" <mnbayazit@gmai l.comha scritto nel messaggio
    news:3a5a9663-281a-4717-a362-eeb7570c6462@p3 1g2000prf.googl egroups.com...
    so, i'm making a website. let's say i have header.php, footer.php and
    content.php.
    >
    now in index.php I simply want to include the 3 pages. easy enough to
    do.
    but let's say the user navigates to mysite.com/content.php. now the
    header and footer will appear to be missing.
    so now the question is, how can i include the header and footer in
    content.php only if the page isn't already nested?
    >
    i suppose i can include a variable in header.php that basically says
    "i've been included", and then in content.php if this variable isn't
    set, i could include them... is this the best/most elegant approach?


    example
    --index.php--
    <?php
    define('is_inc' ,true);
    readfile('heade r.php');
    include('conten t.php');
    readfile('foote r.php');
    ?>

    --content.php--
    <?php
    if(!defined('is _inc')){exit(); }
    echo 'mycontent';
    ?>
    ---------------------

    Yes the variable or define methods are the most used.

    Regards

    L. A. Iarrusso


    Comment

    • Mark

      #3
      Re: php include design question

      On Aug 13, 6:31 pm, "J2Be.com" <mym...@virgili o.itwrote:
      "Mark" <mnbaya...@gmai l.comha scritto nel messaggionews:3 a5a9663-281a-4717-a362-eeb7570c6462@p3 1g2000prf.googl egroups.com...
      >
      so, i'm making a website. let's say i have header.php, footer.php and
      content.php.
      >
      now in index.php I simply want to include the 3 pages. easy enough to
      do.
      but let's say the user navigates to mysite.com/content.php. now the
      header and footer will appear to be missing.
      so now the question is, how can i include the header and footer in
      content.php only if the page isn't already nested?
      >
      i suppose i can include a variable in header.php that basically says
      "i've been included", and then in content.php if this variable isn't
      set, i could include them... is this the best/most elegant approach?
      >
      example
      --index.php--
      <?php
      define('is_inc' ,true);
      readfile('heade r.php');
      include('conten t.php');
      readfile('foote r.php');
      ?>
      >
      --content.php--
      <?php
      if(!defined('is _inc')){exit(); }
      echo 'mycontent';
      ?>
      ---------------------
      >
      Yes the variable or define methods are the most used.
      >
      Regards
      >
      L. A. Iarrusso
      In your scenario accessing content.php directly outputs nothing?

      I had more of this in mind:

      --header.php--
      define('header_ inc',true);
      // header stuffs

      --content.php--
      if(!defined('he ader_inc')) include 'header.php';
      echo 'mycontent';
      if(!defined('fo oter_inc')) include 'footer.php';

      --index.php--
      include 'header.php';
      include 'content.php';
      include 'footer.php';


      Of course, this pretty much makes index.php and content.php identical,
      but the idea was that after the header and footer are loaded once, I
      could use ajax to sub in different content pages instead of reloading
      the whole page...

      But thinking about this more...that wouldn't work. If I used ajax to
      include the new content, header_inc wouldn't be defined and I'd wind
      up with 2 headers and footers.

      I guess what I'd have to do is include a variable in the URL in my
      ajax call?

      Basically...

      --content.php--
      if(!isset('dont _load')) include 'header.php';
      // content
      if(!isset('dont _load')) include 'footer.php';

      And then when I sub a page in w/ ajax I just call it with content.php?
      dont_load=1

      That should work right? Sorry I wasn't more clear the first time.

      Comment

      • NC

        #4
        Re: php include design question

        On Aug 13, 4:41 pm, Mark <mnbaya...@gmai l.comwrote:
        >
        so, i'm making a website. let's say i have header.php,
        footer.php and content.php.
        >
        i suppose i can include a variable in header.php that
        basically says "i've been included", and then in content.php
        if this variable isn't set, i could include them... is this
        the best/most elegant approach?
        Elegance is in the eye of the beholder, so I can't really say whether
        or not this is the most elegant approach. There is, however, an
        alternative. You can put header.php, footer.php and content.php into
        a subdirectory and disable HTTP access to it with .htaccess.

        This approach allows for easy theming. Say, right now you have:

        $themeName = 'default';
        $themeDir = 'themes/' . $themeDir;
        include "$themeDir/header.php";
        include "$themeDir/content.php";
        include "$themeDir/footer.php";

        Then you can work out a new set of content display files, put them
        into a different directory, say, themes/new, and switch the new theme
        on by changing a single line (the value of $themeName) in index.php...

        Cheers,
        NC

        Comment

        • Floortje

          #5
          Re: php include design question

          Mark wrote:
          --content.php--
          if(!isset('dont _load')) include 'header.php';
          // content
          if(!isset('dont _load')) include 'footer.php';
          >
          And then when I sub a page in w/ ajax I just call it with content.php?
          dont_load=1
          >
          That should work right? Sorry I wasn't more clear the first time.
          I think you are patching one bad choice with another but I dont really
          have the time to explain.
          Why dont you have a look at the larger framworks and see how they have
          done it.

          Floortje

          Comment

          • Michael Fesser

            #6
            Re: php include design question

            ..oO(Mark)
            >so, i'm making a website. let's say i have header.php, footer.php and
            >content.php.
            >
            >now in index.php I simply want to include the 3 pages. easy enough to
            >do.
            >but let's say the user navigates to mysite.com/content.php. now the
            >header and footer will appear to be missing.
            >so now the question is, how can i include the header and footer in
            >content.php only if the page isn't already nested?
            Why do you want the content.php to be callable? Includes should be
            stored outside the document root, so that they can't be reached by a
            URL. Then you won't have this problem at all.

            Micha

            Comment

            • Mark

              #7
              Re: php include design question

              On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de wrote:
              .oO(Mark)
              >
              so, i'm making a website. let's say i have header.php, footer.php and
              content.php.
              >
              now in index.php I simply want to include the 3 pages. easy enough to
              do.
              but let's say the user navigates to mysite.com/content.php. now the
              header and footer will appear to be missing.
              so now the question is, how can i include the header and footer in
              content.php only if the page isn't already nested?
              >
              Why do you want the content.php to be callable? Includes should be
              stored outside the document root, so that they can't be reached by a
              URL. Then you won't have this problem at all.
              >
              Micha
              Well, how else are people going to bookmark a particular page, or link
              it to their friends? If I don't make it accessible somehow, the only
              way people will be able to get to it is by going through the index.

              Comment

              • Mark

                #8
                Re: php include design question

                On Aug 13, 11:58 pm, Floortje <floor...@dontl ike.mailwrote:
                Mark wrote:
                >
                  --content.php--
                >
                if(!isset('dont _load')) include 'header.php';
                // content
                if(!isset('dont _load')) include 'footer.php';
                >
                And then when I sub a page in w/ ajax I just call it with content.php?
                dont_load=1
                >
                That should work right? Sorry I wasn't more clear the first time.
                >
                I think you are patching one bad choice with another but I dont really
                have the time to explain.
                Why dont you have a look at the larger framworks and see how they have
                done it.
                >
                Floortje
                Which frameworks? The only other site that I've seen doing this is
                deviantART. It seems to use AJAX to load the main content and sidebar
                separately, but gracefully downgrades if you disable JavaScript.

                That means that you need a file containing just the bare content so
                that you can load it into the page when navigating through the
                sidebar, but it needs to load the entire page when you access it
                directly... unless you have two copies of every page, one with the
                header and footer and one without, but that sounds terrible.

                If you could point me to some frameworks that do this, it would be
                much appreciated. I don't even know what to search for.

                Comment

                • Mark

                  #9
                  Re: php include design question

                  On Aug 13, 11:12 pm, NC <n...@iname.com wrote:
                  On Aug 13, 4:41 pm, Mark <mnbaya...@gmai l.comwrote:
                  >
                  >
                  >
                  so, i'm making a website. let's say i have header.php,
                  footer.php and content.php.
                  >
                  i suppose i can include a variable in header.php that
                  basically says "i've been included", and then in content.php
                  if this variable isn't set, i could include them... is this
                  the best/most elegant approach?
                  >
                  Elegance is in the eye of the beholder, so I can't really say whether
                  or not this is the most elegant approach.  There is, however, an
                  alternative.  You can put header.php, footer.php and content.php into
                  a subdirectory and disable HTTP access to it with .htaccess.
                  >
                  This approach allows for easy theming.  Say, right now you have:
                  >
                  $themeName = 'default';
                  $themeDir = 'themes/' . $themeDir;
                  include "$themeDir/header.php";
                  include "$themeDir/content.php";
                  include "$themeDir/footer.php";
                  >
                  Then you can work out a new set of content display files, put them
                  into a different directory, say, themes/new, and switch the new theme
                  on by changing a single line (the value of $themeName) in index.php...
                  >
                  Cheers,
                  NC
                  I forgot about the applications of theming :) I was thinking about
                  doing themes for my site, but I was just going to have different style
                  sheets....but including the header and footer would give me way more
                  flexibility. Thanks for the idea!

                  Comment

                  • Michael Fesser

                    #10
                    Re: php include design question

                    ..oO(Mark)
                    >On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de wrote:
                    >.oO(Mark)
                    >>
                    >so, i'm making a website. let's say i have header.php, footer.php and
                    >content.php.
                    >>
                    >now in index.php I simply want to include the 3 pages. easy enough to
                    >do.
                    >but let's say the user navigates to mysite.com/content.php. now the
                    >header and footer will appear to be missing.
                    >so now the question is, how can i include the header and footer in
                    >content.php only if the page isn't already nested?
                    >>
                    >Why do you want the content.php to be callable? Includes should be
                    >stored outside the document root, so that they can't be reached by a
                    >URL. Then you won't have this problem at all.
                    >>
                    >Micha
                    >
                    >Well, how else are people going to bookmark a particular page, or link
                    >it to their friends? If I don't make it accessible somehow, the only
                    >way people will be able to get to it is by going through the index.
                    It's still not exactly clear what you want. If the content.php just
                    contains the content of a single page, then it should not be directly
                    callable, as said. But if it's an entire page instead, then there's no
                    point in including it in the index.php ... what am I missing?

                    Usually it's like this: You have a bunch of pages, and each page simply
                    includes the header.php and footer.php (I would rather name them *.inc,
                    but that's just personal preference). Of course these page scripts have
                    to be publically available, but not the included header and footer files
                    because they are not meant to be called directly.

                    Micha

                    Comment

                    • Captain Paralytic

                      #11
                      Re: php include design question

                      On 14 Aug, 20:10, Michael Fesser <neti...@gmx.de wrote:
                      .oO(Mark)
                      >
                      >
                      >
                      >
                      >
                      On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de wrote:
                      .oO(Mark)
                      >
                      so, i'm making a website. let's say i have header.php, footer.php and
                      content.php.
                      >
                      now in index.php I simply want to include the 3 pages. easy enough to
                      do.
                      but let's say the user navigates to mysite.com/content.php. now the
                      header and footer will appear to be missing.
                      so now the question is, how can i include the header and footer in
                      content.php only if the page isn't already nested?
                      >
                      Why do you want the content.php to be callable? Includes should be
                      stored outside the document root, so that they can't be reached by a
                      URL. Then you won't have this problem at all.
                      >
                      Micha
                      >
                      Well, how else are people going to bookmark a particular page, or link
                      it to their friends? If I don't make it accessible somehow, the only
                      way people will be able to get to it is by going through the index.
                      >
                      It's still not exactly clear what you want. If the content.php just
                      contains the content of a single page, then it should not be directly
                      callable, as said. But if it's an entire page instead, then there's no
                      point in including it in the index.php ... what am I missing?
                      >
                      Usually it's like this: You have a bunch of pages, and each page simply
                      includes the header.php and footer.php (I would rather name them *.inc,
                      but that's just personal preference). Of course these page scripts have
                      to be publically available, but not the included header and footer files
                      because they are not meant to be called directly.
                      >
                      Micha- Hide quoted text -
                      >
                      - Show quoted text -
                      Of course the problem with calling them *.inc is that anyone calling
                      it from their browser will see any php code within them.

                      Comment

                      • Michael Fesser

                        #12
                        Re: php include design question

                        ..oO(Captain Paralytic)
                        >Of course the problem with calling them *.inc is that anyone calling
                        >it from their browser will see any php code within them.
                        In the worst case the same can happen with *.php and as I've already
                        said twice in this thread - such files don't belong to the document
                        root. Put them one level higher: no URL, no problem.

                        Micha

                        Comment

                        • Jerry Stuckle

                          #13
                          Re: php include design question

                          Mark wrote:
                          On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de wrote:
                          >.oO(Mark)
                          >>
                          >>so, i'm making a website. let's say i have header.php, footer.php and
                          >>content.php .
                          >>now in index.php I simply want to include the 3 pages. easy enough to
                          >>do.
                          >>but let's say the user navigates to mysite.com/content.php. now the
                          >>header and footer will appear to be missing.
                          >>so now the question is, how can i include the header and footer in
                          >>content.php only if the page isn't already nested?
                          >Why do you want the content.php to be callable? Includes should be
                          >stored outside the document root, so that they can't be reached by a
                          >URL. Then you won't have this problem at all.
                          >>
                          >Micha
                          >
                          Well, how else are people going to bookmark a particular page, or link
                          it to their friends? If I don't make it accessible somehow, the only
                          way people will be able to get to it is by going through the index.
                          >
                          Don't confuse the file with the page. You can store a file outside of
                          the document root and still include it in a page.

                          But if index.php includes is the only page ever called, how are you
                          going to determine which file to include for your content?



                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstucklex@attgl obal.net
                          =============== ===

                          Comment

                          • Captain Paralytic

                            #14
                            Re: php include design question

                            On 14 Aug, 20:57, Michael Fesser <neti...@gmx.de wrote:
                            .oO(Captain Paralytic)
                            >
                            Of course the problem with calling them *.inc is that anyone calling
                            it from their browser will see any php code within them.
                            >
                            In the worst case the same can happen with *.php and as I've already
                            said twice in this thread
                            Not that I can see you haven't. Whilst it is true that if the server
                            breaks it is possible that php code can be sent to the browser un-
                            interpreted, it will be a very very rare ocurence. However not many
                            servers are set up to send .inc files to the php interpreter and thus
                            those files are far more likely to display the code.
                            - such files don't belong to the document
                            root. Put them one level higher: no URL, no problem.
                            But you did not say this in the post where you advised calling them
                            *.inc
                            If you want to put inc in the name *.inc.php is safer.
                            >
                            Micha

                            Comment

                            • Michael Fesser

                              #15
                              Re: php include design question

                              ..oO(Captain Paralytic)
                              >On 14 Aug, 20:57, Michael Fesser <neti...@gmx.de wrote:
                              >.oO(Captain Paralytic)
                              >>
                              >Of course the problem with calling them *.inc is that anyone calling
                              >it from their browser will see any php code within them.
                              >>
                              >In the worst case the same can happen with *.php and as I've already
                              >said twice in this thread
                              >Not that I can see you haven't. Whilst it is true that if the server
                              >breaks it is possible that php code can be sent to the browser un-
                              >interpreted, it will be a very very rare ocurence. However not many
                              >servers are set up to send .inc files to the php interpreter and thus
                              >those files are far more likely to display the code.
                              Servers don't have to be setup to parse *.inc files for anything simply
                              because such files are not meant to be accessible. That's the whole
                              point. If you can reach such a file by URL, you've made a mistake and
                              an improper directory structure.
                              >>- such files don't belong to the document
                              >root. Put them one level higher: no URL, no problem.
                              >But you did not say this in the post where you advised calling them
                              >*.inc
                              I mentioned it in my first reply and referred to that in my second.
                              >If you want to put inc in the name *.inc.php is safer.
                              Yes, if the files are stored in the wrong place. Then you have to solve
                              a problem made by yourself. But if the files would be stored where they
                              belong, then you wouldn't have this problem at all and could name them
                              any way you want.

                              Micha

                              Comment

                              Working...