By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,353 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,353 IT Pros & Developers. It's quick & easy.

include() header and footer

P: n/a
Jez
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
Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <je**********@btinternet.com> wrote in message
news:ad**************************@posting.google.c om...
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

Jul 16 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Fri, 11 Jul 2003 14:14:28 -0600, "Zac
Hester" <ne**@planetzac.net> amazingly managed to produce the
following with their Etch-A-Sketch:
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.

@define(SITE_BASE, dirname(__FILE__));
@include(SITE_BASE . '/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/AwUBPw8bdWfqtj251CDhEQKw0QCfSrrCvdSaASKp0GaeRD1PqF qBw5wAnRkZ
MdUm6p2q7AS18Id5zTbHl2M7
=mEuP
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #3

P: n/a
"Ian.H [dS]" <ia*@WINDOZEdigiserv.net> wrote in message
news:fp********************************@4ax.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Fri, 11 Jul 2003 14:14:28 -0600, "Zac
Hester" <ne**@planetzac.net> amazingly managed to produce the
following with their Etch-A-Sketch:
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.

@define(SITE_BASE, dirname(__FILE__));
@include(SITE_BASE . '/some_file.php');
Now it doesn't matter what the URL may be, as it deals with the
filesystem, not relative URLs =)


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
Jul 16 '05 #4

P: n/a
Jez
Thanks both for being so understanding!

Jez
"Zac Hester" <ne**@planetzac.net> wrote in message news:<3f********@news.enetis.net>...
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" <je**********@btinternet.com> wrote in message
news:ad**************************@posting.google.c om...
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

Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.