SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Includes in the <head> tag
-
Jun 1, 2007, 02:51 #1
- Join Date
- Apr 2003
- Location
- US
- Posts
- 95
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Includes in the <head> tag
Complete PHP novice here, so apologies for asking what may be a really basic question. Currently I only use PHP for include files, to make site maintenance easier. Nothing database driven. Just this kind of thing...
Code PHP:<?php include "../includes/masthead.php"; ?>
However I've only ever used includes on elements within the <body> tag. Is it possible (and normal practice) to include elements of the <head> too, or at least those that stay the same or similar from page to page? Things like...
the doctype declararation
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="description" content="" />
<meta name="keywords" content="" />
etc.
And if so, is the way of 'calling' them the same as in the body tag?
E.g.
Code PHP:<?php include "../includes/doctype.php"; ?> <?php include "../includes/faviconlink.php"; ?> <?php include "../includes/meta-desc-1c.php"; ?> <?php include "../includes/meta-keywords-1c.php"; ?>
Thanks.
-
Jun 1, 2007, 03:44 #2
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Yes, no problem with doing it like that. The browser only reads what the php throws out, how it gets there is up to you.
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Jun 1, 2007, 03:59 #3
- Join Date
- Jun 2004
- Location
- "Then I figure the most good good guy will win."
- Posts
- 1,666
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In addition to what Spike says...
I like to use readfile() vs. include() if the content I am including is static (i.e. no dynamic server-side stuff.)
readfile() is a bit more secure and a tiny bit faster (from what I have read.)
So, if the stuff you are including is just html, then give the include a .html extension and readfile() it.
Here is an example:
PHP Code:<?php
readfile($_SERVER['DOCUMENT_ROOT'].'/path/from/root/includes/doctype.html');
readfile($_SERVER['DOCUMENT_ROOT'].'/path/from/root/includes/faviconlink.txt');
include($_SERVER['DOCUMENT_ROOT'].'/path/from/root/includes/meta-desc-1c.php');
include($_SERVER['DOCUMENT_ROOT'].'/path/from/root/includes/meta-keywords-1c.php');
?>
-
Jun 1, 2007, 05:07 #4
- Join Date
- Mar 2007
- Posts
- 196
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for sharing that mhulse, what about require_once vs include? I typically use require_once but I'm not sure if one is faster than the other.
Kayzio - We don't hesitate, we accelerate.
-
Jun 1, 2007, 10:54 #5
- Join Date
- Jun 2004
- Location
- "Then I figure the most good good guy will win."
- Posts
- 1,666
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi, for sure... Glad I could help... I am definitely no guru, so it would prob be best for you to read-up about the differences via the below links:
http://us.php.net/require
http://php.net/require_once
http://php.net/include
http://php.net/include_once
http://php.net/readfile
Also, found these via google search:
http://www.raditha.com/wiki/Readfile_vs_include
http://selfimprovement.ch/technology...vs_include.php
I hope that helps.
Have a great day,
Cheers,
Micky
-
Jun 1, 2007, 13:17 #6
- Join Date
- Apr 2003
- Location
- US
- Posts
- 95
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the advice guys.
-
Jun 1, 2007, 14:53 #7
- Join Date
- Jan 2007
- Location
- Australia
- Posts
- 137
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
require_once as opposed to include - require_once acts differently in terms of error handling, and (as its name suggests) only includes the file once. It is also slightly slower. I suggest you use include for all php files to be included. readfile() would be a good idea, however ideally you should keep all your static content in PHP files and have those PHP files ensure they are not directly accessed, just for security purposes (say you had some admin only static content...). In this case, eval(readfile()) would be slightly faster than include anyway.
Check out my SitePoint articles and blog posts!
Bookmarks