Jump to content

PHP "include" efficiency


Kael
 Share

Recommended Posts

I've been wondering for a while:

I've got a light-moderately trafficked site that I'm working on, and there are a lot of pages that don't require php for anything, but i've got a footer that i want to add to the end of everything. i inherited this site from another webmaster, who inherited it from someone else, etc., so it's about 6 years old (!!), and is therefore composed of hundreds of pages. obviously, i would benefit greatly by having this footer in one place where i can edit it once and have it automatically updated to everything, but i'm not sure if it's efficient to make the server parse the entire document just for a little one-line "include 'file.php';" at the end. does anyone know if parsing a php file with lots of html, but only one line of php will make any difference in server performance in the future (accounting for, as any hopeful soul should, a possible increase in future traffic to the site)?

along these same lines, i had an idea the other day that i wasn't sure about. i like the concept of the template-driven site, where you use one file, then just change the body. i was thinking something like this:

<html>
<head>
<?php $title = $_GET['T'];
echo '
<title>'.$title.'</title>
';
?>
</head>
<body>
Nav-bars and such - edited similarly to the title.; stagnant content, etc.

<div id='body'>
<?php
if ($title == 'this') {
include 'this.php';
} elseif ($title == 'that') {
include 'that.php';
} elseif......

?>
</div>
<div id="footer">
<?php include 'footer.php'; ?>
</div>
</body>
</html>

However, with 300+ pages, id still have 300 pages laying around all over the place, and the only advantage would be simplicity of code (and file sizes). i could use a database, but, despite the fact that i am an avid proponent of the database, it's harder to edit crap like this in a database than it is just doing it in a text file. not to mention my employers prefer a less efficient and less visually appealing site design composed of disparate designs for each product section, but that's a different issue.

anyway, if anyone has any thoughts on any of this, do share.

-love kael

Link to comment
Share on other sites

  • 3 weeks later...

This template might make it a bit easier to understand:

<?
include("inc_header.php");

//include body of code in here

include("inc_footer.php");
?>

then in the inc_header.php all you have to do is add the code u want at the start of each page such as...

<?
echo "<HTML>\n";
echo "<Head>\n";
echo "<Title>title</title>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
echo "<LINK REL=STYLESHEET TYPE=\"text/css\" HREF=\"mainstyle.css\">\n";
echo "</Head>\n";
echo "<Body>\n";
?>

and in the inc_footer.php all you have to do is add the code u want at the end of each page such as ....

<?
echo "</Body>\n";
echo "</HTML>\n";
?>

Hopes this help - if you need this made any clearer either email me, see the attached files which are from my website or go to www.gloucesterboxers.co.uk and view the source.

inc_header.php

inc_footer.php

home.php

Link to comment
Share on other sites

  • 2 months later...

If you're only using PHP to include files, then you are right about adding unneeded overhead to your webpages. There is a far older technology called "Server Side Includes" which is supported by all webservers I know of.

The syntax of your include command would be as following:

<!--#include file="blah.html" -->

This will include the (static) page "blah.html". Nothing else needed, no <%php %> etc.

On the other side, I believe PHP (and probably SSI also) "caches" pages. That is, it stores the resulting page (with the completed inclusion) somewhere for quick retrieval, to cause minimal processor load.

Another option is also to give the "inclusion work" to your visitor, by using JavaScript to perform the inclusion. This gives minimal processing and storage load to your webserver, and lets the visitor's browser do all the work.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy