include() header and footer

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

    include() header and footer

    I'm ashamed that I need to ask this question. I've been using PHP for
    almost a year now, and have used HTML extensively in the last few
    years.

    Often, when I create a new site, I use include() to add an HTML header
    at the top of the page and a footer (ususally including a menu) at the
    bottom.

    Up to now I've created two or more copies of each header and footer,
    the only difference being the path to linked files. For example, in
    the root directory I need to reference a stylesheet as
    'style/screen.css', but in files within another directory I use
    '../style/screen.css' or '../../style/screen.css'.

    Now this is the part I'm ashamed about! I realised today that I could
    just use '/style/screen.css' within the file in the root directory and
    any other directory you care to mention. I've no idea how I missed
    this and I'm sure I've never seen it in anyone else's HTML or in any
    turtorials.

    Can someone please confirm that it's ok to do this (i.e. one header
    file and one footer file, using '/dir/file' as the link rather than
    'dir/file' or '../dir/file')?

    I hope this makes sense and that you can forgive my stupidity?

    Jez
  • Zac Hester

    #2
    Re: include() header and footer

    Jez,

    Yup, this is ok to use. There's a couple situations that this won't work,
    though:

    1. If your site is being hosted through the "User Directory" method. That
    would make your web root here:



    However, your browser, will see the web root as:



    and will request all files from the actual web root of the server. Note
    that the browser doesn't care where your document is, it's all based on the
    domain name.

    2. If you're using any kind of path-style queries, this might confuse
    things on your part, but is usually workable.

    If you haven't seen many sites/tutorials using this technique, it's because
    file system pathing seems to be the most misunderstood and yet most simple
    aspect of computer operating systems. The idea of a root node escapes most
    web designers (HTML authors). It also becomes an issue when developing web
    sites on a desktop computer and uploading them to the server. Most people
    need to keep checking and rechecking their work before they upload it to the
    server. In this case, the local filesystem will map your files based on the
    root of your drive. And, unless you're building your site right off the
    root of your drive, your pictures aren't going to show up. But who needs to
    check to make sure your pictures and style sheets are showing up when your
    editing code that won't be visible until it's on the server, right? I think
    the desire to "verify" all the linked resources on a web page stems from
    people's lack of understanding of the file system in the first place.

    On _all_ of the sites I design, I use either a full URL (http://...), or the
    absolute path to any outside resources for the same reason you had to use
    two template files: the same HTML code may be served from any directory on
    your site.

    You should develop a sixth-sense for code duplication. When you start
    noticing you have two of the same or two very similar pieces of code
    existing on your web site accomplishing the same or very similar tasks, you
    should have an alarm going off in your head: "There's gotta be a better way
    to do this..."

    I hope this helps,
    Zac

    I hope this helps,
    Zac



    "Jez" <jez.hailwood@b tinternet.com> wrote in message
    news:ad15f8ee.0 307111135.52050 3e2@posting.goo gle.com...[color=blue]
    > I'm ashamed that I need to ask this question. I've been using PHP for
    > almost a year now, and have used HTML extensively in the last few
    > years.
    >
    > Often, when I create a new site, I use include() to add an HTML header
    > at the top of the page and a footer (ususally including a menu) at the
    > bottom.
    >
    > Up to now I've created two or more copies of each header and footer,
    > the only difference being the path to linked files. For example, in
    > the root directory I need to reference a stylesheet as
    > 'style/screen.css', but in files within another directory I use
    > '../style/screen.css' or '../../style/screen.css'.
    >
    > Now this is the part I'm ashamed about! I realised today that I could
    > just use '/style/screen.css' within the file in the root directory and
    > any other directory you care to mention. I've no idea how I missed
    > this and I'm sure I've never seen it in anyone else's HTML or in any
    > turtorials.
    >
    > Can someone please confirm that it's ok to do this (i.e. one header
    > file and one footer file, using '/dir/file' as the link rather than
    > 'dir/file' or '../dir/file')?
    >
    > I hope this makes sense and that you can forgive my stupidity?
    >
    > Jez[/color]


    Comment

    • Ian.H [dS]

      #3
      Re: include() header and footer

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Whilst lounging around on Fri, 11 Jul 2003 14:14:28 -0600, "Zac
      Hester" <news@planetzac .net> amazingly managed to produce the
      following with their Etch-A-Sketch:
      [color=blue]
      > Jez,
      >
      > Yup, this is ok to use. There's a couple situations that this
      > won't work, though:
      >
      > 1. If your site is being hosted through the "User Directory"
      > method. That would make your web root here:
      >
      > http://somedomain.com/~username/
      >
      > However, your browser, will see the web root as:
      >
      > http://somedomain.com/
      >
      > and will request all files from the actual web root of the server.
      > Note that the browser doesn't care where your document is, it's all
      > based on the domain name.[/color]


      @define(SITE_BA SE, dirname(__FILE_ _));
      @include(SITE_B ASE . '/some_file.php') ;


      Now it doesn't matter what the URL may be, as it deals with the
      filesystem, not relative URLs =)


      HTH.



      Regards,

      Ian

      -----BEGIN PGP SIGNATURE-----
      Version: PGP 8.0

      iQA/AwUBPw8bdWfqtj2 51CDhEQKw0QCfSr rCvdSaASKp0GaeR D1PqFqBw5wAnRkZ
      MdUm6p2q7AS18Id 5zTbHl2M7
      =mEuP
      -----END PGP SIGNATURE-----

      --
      Ian.H [Design & Development]
      digiServ Network - Web solutions
      www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
      Programming, Web design, development & hosting.

      Comment

      • Zac Hester

        #4
        Re: include() header and footer

        "Ian.H [dS]" <ian@WINDOZEdig iserv.net> wrote in message
        news:fp6ugv8pae 6cn32tfb9g05anu g0280oeqc@4ax.c om...[color=blue]
        > -----BEGIN PGP SIGNED MESSAGE-----
        > Hash: SHA1
        >
        > Whilst lounging around on Fri, 11 Jul 2003 14:14:28 -0600, "Zac
        > Hester" <news@planetzac .net> amazingly managed to produce the
        > following with their Etch-A-Sketch:
        >[color=green]
        > > Jez,
        > >
        > > Yup, this is ok to use. There's a couple situations that this
        > > won't work, though:
        > >
        > > 1. If your site is being hosted through the "User Directory"
        > > method. That would make your web root here:
        > >
        > > http://somedomain.com/~username/
        > >
        > > However, your browser, will see the web root as:
        > >
        > > http://somedomain.com/
        > >
        > > and will request all files from the actual web root of the server.
        > > Note that the browser doesn't care where your document is, it's all
        > > based on the domain name.[/color]
        >
        >
        > @define(SITE_BA SE, dirname(__FILE_ _));
        > @include(SITE_B ASE . '/some_file.php') ;
        >
        >
        > Now it doesn't matter what the URL may be, as it deals with the
        > filesystem, not relative URLs =)[/color]

        That's almost identical to how I set include/require paths in PHP (I never
        did find the define() method a very elegant construct). However, it doesn't
        address pathing in client-rendered HTML. PHP includes are not relative to
        the domain name. PHP includes are _always_ based on the file system.
        Images, style sheets, and JavaScripts are not based on the file system.
        They are always based on the web site's "root" (domain name) since the
        client has no way to reference the server's file system.

        Good code snippet. Keep 'em comin'

        Take care,
        Zac


        Comment

        • Jez

          #5
          Re: include() header and footer

          Thanks both for being so understanding!

          Jez


          "Zac Hester" <news@planetzac .net> wrote in message news:<3f0f1ad1$ 1@news.enetis.n et>...[color=blue]
          > Jez,
          >
          > Yup, this is ok to use. There's a couple situations that this won't work,
          > though:
          >
          > 1. If your site is being hosted through the "User Directory" method. That
          > would make your web root here:
          >
          > http://somedomain.com/~username/
          >
          > However, your browser, will see the web root as:
          >
          > http://somedomain.com/
          >
          > and will request all files from the actual web root of the server. Note
          > that the browser doesn't care where your document is, it's all based on the
          > domain name.
          >
          > 2. If you're using any kind of path-style queries, this might confuse
          > things on your part, but is usually workable.
          >
          > If you haven't seen many sites/tutorials using this technique, it's because
          > file system pathing seems to be the most misunderstood and yet most simple
          > aspect of computer operating systems. The idea of a root node escapes most
          > web designers (HTML authors). It also becomes an issue when developing web
          > sites on a desktop computer and uploading them to the server. Most people
          > need to keep checking and rechecking their work before they upload it to the
          > server. In this case, the local filesystem will map your files based on the
          > root of your drive. And, unless you're building your site right off the
          > root of your drive, your pictures aren't going to show up. But who needs to
          > check to make sure your pictures and style sheets are showing up when your
          > editing code that won't be visible until it's on the server, right? I think
          > the desire to "verify" all the linked resources on a web page stems from
          > people's lack of understanding of the file system in the first place.
          >
          > On _all_ of the sites I design, I use either a full URL (http://...), or the
          > absolute path to any outside resources for the same reason you had to use
          > two template files: the same HTML code may be served from any directory on
          > your site.
          >
          > You should develop a sixth-sense for code duplication. When you start
          > noticing you have two of the same or two very similar pieces of code
          > existing on your web site accomplishing the same or very similar tasks, you
          > should have an alarm going off in your head: "There's gotta be a better way
          > to do this..."
          >
          > I hope this helps,
          > Zac
          >
          > I hope this helps,
          > Zac
          >
          >
          >
          > "Jez" <jez.hailwood@b tinternet.com> wrote in message
          > news:ad15f8ee.0 307111135.52050 3e2@posting.goo gle.com...[color=green]
          > > I'm ashamed that I need to ask this question. I've been using PHP for
          > > almost a year now, and have used HTML extensively in the last few
          > > years.
          > >
          > > Often, when I create a new site, I use include() to add an HTML header
          > > at the top of the page and a footer (ususally including a menu) at the
          > > bottom.
          > >
          > > Up to now I've created two or more copies of each header and footer,
          > > the only difference being the path to linked files. For example, in
          > > the root directory I need to reference a stylesheet as
          > > 'style/screen.css', but in files within another directory I use
          > > '../style/screen.css' or '../../style/screen.css'.
          > >
          > > Now this is the part I'm ashamed about! I realised today that I could
          > > just use '/style/screen.css' within the file in the root directory and
          > > any other directory you care to mention. I've no idea how I missed
          > > this and I'm sure I've never seen it in anyone else's HTML or in any
          > > turtorials.
          > >
          > > Can someone please confirm that it's ok to do this (i.e. one header
          > > file and one footer file, using '/dir/file' as the link rather than
          > > 'dir/file' or '../dir/file')?
          > >
          > > I hope this makes sense and that you can forgive my stupidity?
          > >
          > > Jez[/color][/color]

          Comment

          Working...