serving content to users

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

serving content to users

Post by mabufo »

I wasn't quite sure where to put this, but it has to do with design(non visual, more or less content organization) and php, but on somewhat separate levels. I suppose I'll leave it up to moderator to move it if this isn't the right area.

The website I am working with is set up to use a series of includes to serve content to the user. For example, the website visual layout is stored in a file called index.php, and a php include would be called from where the website's actual content is supposed to be. So the page would become http://localhost/index.php?page=news.php. This was because the include was something like this:

Code: Select all

include($_GET['page']);
(This of course, ran the risk of remote injection, and was just sloppy to begin with.) The page would then show the news, with the rest of the page layout around it. I guess this was originally set up to easily change elements of the visual side, without changing every page on the site. Convenience over organization/security.

What I need is a better idea of how to better serve the content in general. Initially, my friend and I had no idea how many pages we would end up with, so we ditched the static approach. We really didn't know of any other way to do what we wanted. Are there any alternative techniques to serve content to users? What to large websites do? Where can I learn more about it?

Thank you in advance.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Databasing. :P

Databases are the most useful tool that you'll ever have in a dynamic website. I'm currently refactoring my blog to be even more database driven than it currently is, and I find that as you control more by use of the database, the website becomes more customizable, easily updated, and the framework ends up being reusable (of course, you have to design it for re-usability).

To respect the fact that you are "new" at this, here's the kinds of tables you'll need:
  • Options (logo, modules, template, etc.)
  • Content (all pages, sorted by 'folder' names)
  • Folders (or categories, subcategories, etc.)
  • Users (even if you don't support a community, you should give administrative users usernames and passwords to make updates)
Of course, it can become a lot more complex than that, but the more complexity you go for, the more SQL you'll need to be familiar with. Basically, you'll want a strong back-end that doesn't rely on anything hard-coded beyond itself. My site contains one viewable PHP file, and everything else is a class or module (it's ruled by mod_rewrite, but you could still manage it through URL variables). Just aim for the most dynamic site you can muster up, and dont be afraid to skip some stuff or save it for later... In web design, you've got all of the time in the world.
Arawn
Forum Commoner
Posts: 42
Joined: Sat May 05, 2007 6:03 am

Re: serving content to users

Post by Arawn »

mabufo wrote:What I need is a better idea of how to better serve the content in general. Initially, my friend and I had no idea how many pages we would end up with, so we ditched the static approach. We really didn't know of any other way to do what we wanted. Are there any alternative techniques to serve content to users? What to large websites do? Where can I learn more about it?
Large websites try to server as much static content as possible not using dynamic methods, such as, PHP.
From Scalable Internet Architectures By Theo Schlossnagle

Application developers place so much focus on efficient web application design that it is easy to ignore an important and simple truth: Most content, by volume and by count, is static. From industries such as online pornography to common news sites, static content rules the screen, and the approaches to serving dynamic content are typically inefficient when used in the realm of static content serving.
It's a good book, I would recommend reading it.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Instead of using includes directly you should consider a simple template engine:

template.cls.php

Code: Select all

class Template{
  function execute($file)
  {
      ob_start();
      include_once($file);
      $buff = ob_get_contents();
      ob_end_clean();

      return $buff;
  }
}
That is literally all the code you need to develop complex, templatized HTML pages.

A template would look something like:

layout.tpl.php

Code: Select all

<html>
  <head>
    <title><?php echo $this->title; ?></title>
  </head>
  <body>
    <?php echo $this->body; ?>
  </body>
</html>
You initialize the template engine and use the template like so:

index.php

Code: Select all

$eng = new Template();
$eng->title = 'This is the title';
$eng->body = 'This is the body';

echo $eng->execute('layout.tpl.php');
The above template engine relies on PHP and it's expando property support. Assigning the 'title' and 'body' members and their values at runtime instead of declaring the member statically inside the class. Really you could use either technique.

Now inside index.php you can dynamically include data from HTML files or database tables, CSV, etc, etc...

index.php?page=2

Code: Select all

$eng = new Template();

$page = (int)$_GET['page'];

// Assume connection to database somewhere in bootstrap code
$sql = sprintf('SELECT * FROM pages WHERE id = %d', $page);
$res = mysql_query($sql);
$arr = mysql_fetch_array($res)

$eng->title = strip_tags($arr['title']);
$eng->body = $arr['body']; // Run this HTML through HTML purifier 

echo $eng->execute('layout.tpl.php');
The number of dynamic sections of your web site is now only limited by your PHP template (layout.tpl.php) which is a reflection of your database schema. If you have several pages which reqauire different layouts, you can adjust those layouts using conditionals or just create separate layouts and use them conditionally.

If you are concerned about taxing your server, this approach is quite streamlined and doesn't actually burn a lot of cycles as the template engine is so simple it has hardly any over head, unlike Smarty. :)

Caching is an breeze to implement as well. Simply take the results before echo-ing to screen and save them to a static HTML in a cache directory. Of course you need to add the logic into index.php to check for cached pages and use them instead of generating a page on the fly, but thats trivial as well, assuming you have static pages, at least in nature.

The coolest part of this technique (output buffering) is you can perform all sorts of filtering on the results before display. For example, you can check referrer and if it's Google, Yahoo, etc. You can hilite keywords in your HTML pages. There are countless other things you can do as well.

Cheers :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: serving content to users

Post by Christopher »

mabufo wrote:The website I am working with is set up to use a series of includes to serve content to the user. For example, the website visual layout is stored in a file called index.php, and a php include would be called from where the website's actual content is supposed to be. So the page would become http://localhost/index.php?page=news.php. This was because the include was something like this:

Code: Select all

include($_GET['page']);
(This of course, ran the risk of remote injection, and was just sloppy to begin with.) The page would then show the news, with the rest of the page layout around it. I guess this was originally set up to easily change elements of the visual side, without changing every page on the site. Convenience over organization/security.
Actually what you have implemented is a very basic Front Controller. That is the direction that most frameworks take these days, so your instincts are good (though your implementation is a little lacking.
mabufo wrote:What I need is a better idea of how to better serve the content in general. Initially, my friend and I had no idea how many pages we would end up with, so we ditched the static approach. We really didn't know of any other way to do what we wanted. Are there any alternative techniques to serve content to users? What to large websites do? Where can I learn more about it?
The combination of a Front Controller and MVC are the direction most PHP developers are going -- although there is some debate about what exactly the implementation of those patterns looks like. Typically you would use specific parameters to include a file that contains a class of the same name and call a method in that class. Take a look a some frameworks to fine examples.

I you would like some simple code example, I or others can provide them.
(#10850)
Post Reply