Main Content RSS FeedLatest Entry

PHP Mini Sites Cms: template system and rewrite rules

In this fourth post we’ll have to actually do something useful with our code. Simply add the following at the end of the previous script.


if($found)
{// set up variables with content
 $title=$items[0];
 $url=$items[1];
 $post=$items[2];
 $tags=$items[3];
 $description=$items[4];
 $author=$items[5];
 $date=$items[6];
}

else

{ // if we get here there was a 404 error either for malformed url or because we couldn't read the file
 $title="Page not found";
 $url="404.html";
 $post='<h1>Ooops! Page not found</h1><p>Don\'t worry, this may happen. Please go back to the <a href="'.$config_site.'">Home Page</a>.';
 $tags="Not found, 404";
 $description="This page could not be found";
 $author="";
 $date="";
}

Now we have everything set up. We only need to populate our theme with the relevant content. We’ll use psuedo tags in the theme, like {title}, {post} and so on.

We’ll place those meta tags in our template, or the Artisteer template. Then save this file as “theme.html” or whatever name you prefer to use.

Here’s an extra simple theme with no styles, just to show the concept:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="description" content="{description}" />
<meta name="keywords" content="{tags}" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<link href="put-your-css-here.css" type="text/css" rel="stylesheet" />
<link href="i/favicon.ico" rel="SHORTCUT ICON" />
<script type="text/javascript" src="put-your-javascript-here.js"></script>
</head>
<body>
<h1>{title}</h1>
<h2>{blog}</h2>
{post}
</body>
</html>

This approach is slower than echoing the variables in the page itself, as it requires reading the theme and performing string replacements, but gives us a clean separation between the theme and the code, and makes this project easily scalable.

$fp=@fopen($config_theme,"rb");

if($fp!==false)
{
 if( false !== ($data=fread($fp,filesize($config_theme))))
 {// theme was read in, we can fill it with the data

     $data=str_replace("{title}",$title,$data);
     $data=str_replace("{post}",$post,$data);
     $data=str_replace("{tags}",$tags,$data);
     $data=str_replace("{description}",$description,$data);
     $blog='';
     if($author!='' &amp;&amp; $date !='')
     {
      $blog="Posted by ".$author." on ".date($config_date,$date);
     }
     $data=str_replace("{blog}",$blog,$data);
     echo $data;
 }
 fclose($fp);
}

else

{
echo 'Internal error'; // could not read the theme!
}

Makes sense? Hope so, because there’s not much more to be done. Save the script as index.php, then create or edit the .htaccess file. We need a rewrite rule to pass everything the user requests to index.php.

The .htaccess rewrite rule

Now, if there’s something I really don’t like to do it must be writing rewrite rules. I’m no Apache guru and have never studied them formally like I did for programming. But this one is really simple to use and understand.


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

First, we turn on the rewrite engine and say everything is relative to the web root.

Then, there are two rewrite conditions, almost identical. They check if a file (-f) or a directory (-d) exists. If so, the existing file or directory is served.

Otherwise, the rewrite rule is invoked, and points to /index.php, our code. Note that no redirection is performed, so this will return an OK 200 status and the user’s browser will still display the originally requested url.

That’s almost all, folks

Okay that’s all. The simple cms is ready. However it’s still a bit poor, even adding a nice artisteer template it will have no navigation. We may want to add RSS, a sitemap, even some widgets. As long as there’s no user submitted content, like comments, we can do everything without writing to our server. And we’ll see how in the next post.

Feel free to comment, ask questions and suggest how to expand this simple flat file php CMS. I’m not sure it’s worth to add an administrative panel, as this is really meant to be a set and forget script, but will wait for your suggestions.

Thanks for reading so far, have fun!

Recent Entries

Hostgator Coupon September October 2009

Are you looking for an Hostgator Coupon code? If so, act right now as I don’t know how long this offer will be available. Enter ‘autumn’ without quotes in the Coupon Code field to get 20% off your hosting plan. As of today (september 2009, but should be okay for october as well) it works,…………….

Read the entire article: Hostgator Coupon September October 2009

Remove item from array remove null values

Say we want to drop a value from an array, this is simple: just unset it!

unset($myarray[13]);

Sometimes however we may want to perform some more complex clean ups. Such as if we have just exploded an URL for url rewriting, and want to get rid of the null array items placed where the slashes were.
First approach…………….

Read the entire article: Remove item from array remove null values

Simple Flat File CMS in PHP – Part 3

In this third post, we’ll have to actually do something useful with our code. Simply add the following at the end of the previous script.

if($found)
{// set up variables with content
$title=$items[0];
$url=$items[1];
$post=$items[2];
$tags=$items[3];
$description=$items[4];
$author=$items[5];
$date=$items[6];
}

else

{ // if we get here there was a 404 error either for malformed url or because we couldn’t…………….

Read the entire article: Simple Flat File CMS in PHP – Part 3

Flatfile PHP CMS tutorial – Part 2: the code

Time to define the structure for our flatfile CMS or Blog, and see the PHP code needed to parse it.

Read the entire article: Flatfile PHP CMS tutorial – Part 2: the code

Flat file PHP CMS or Blog, simple and customizable

Need a flatfile CMS or Blog, running low on resources, yet expandable and with support for custom themes and SEO urls? This tutorial series will show you how to quickly build it, with little effort.

Read the entire article: Flat file PHP CMS or Blog, simple and customizable

ZEND Optimizer

PHP is an interpreted language, this is a quick run down on Interpreters and Compilers.

Read the entire article: ZEND Optimizer

International characters in PHP

Your feed displays funky characters? European accents turn into a mess? Properly encoding UTF8 characters is the solution.

Read the entire article: International characters in PHP

PHP Tutorial : Validate email address – simple way with filter_var() function

While browsing php.net without any scope I discovered a very cute & nice way to replace the usual regular expression email checking thing.
That way is via filter_var() php function which does the things 10 times faster :

<?php
$email = “someone@example.com”;

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo “Invalid email address”;
}
?>

Read the entire article: PHP Tutorial : Validate email address – simple way with filter_var() function

PHP Tutorials : Rename a mysql table

If you want to rename a mysql table via php you just need an extremely simple query like the following one:

<?php
//include db credentials

mysql_query(“rename table TheOldName to TheNewName”);
?>

Read the entire article: PHP Tutorials : Rename a mysql table