How to create a simple CMS with PHP
If you're in need of a CMS (Content Management System) for small sites based on PHP and flat files (*.txt), just follow the 5 steps below to create a really simple one within a few minutes.
Requirements:
- A custom-made website template or a self-made one
- A source code editor
- PHP installed on your server
Step 1:
Rename your index.html/index.htm to index.html and put the code below at the very beginning of the page.
<?php
/* The variable below is an array of valid pages. When you add a new page, it must be added to this array. */
$validPages = array(
'news',
'about',
'gallery',
'links',
'contact'
);
?>
<!-- HTML code begins here -->
<!DOCTYPE html PUBLIC etc...
Step 2:
Create the navigation list as shown below. Study exactly the code, how the additional pages will be created according to the array of valid pages shown in Step 1.
<!-- ##### BEGIN NAVIGATION ##### -->
<ul id="menu">
<li><a href="index.php">Home</a></li>
<li><a href="index.php ?p=news">News</a></li>
<li><a href="index.php ?p=about">About us</a></li>
<li><a href="index.php ?p=gallery">Gallery</a></li>
<li><a href="index.php ?p=links">Links</a></li>
<li><a href="index.php ?p=contact">Contact</a></li>
</ul>
<!-- ##### END NAVIGATION ##### -->
Step 3:
Copy and paste the code below, where your content goes, whereas home.txt will be the default content for the index.html.
<!-- ##### BEGIN CONTENT ##### -->
<?php
if (isset($_GET['p']) && in_array($_GET['p'], $validPages) && file_exists("includes/" . $_GET['p'] . ".txt")) {
include("includes/" . $_GET['p'] . ".txt");
} else {
include("includes/home.txt");
}
?>
<!-- ##### END CONTENT ##### -->
Step 4:
With a normal source code editor create your content files home.txt, about.txt, gallery.txt, links.txt and contact.txt and store them in a folder named includes.
Fill in your content in the individual *.txt files using usual HTML code and CSS for formatting, for e.g.:
<h1>Welcome to our Homepage!</h1>
<p>Here goes your text...</p>
<p><a href="http://www.openDesigns.org">Open Designs</a></p>
Step 5:
Upload the files (index.html and folder includes with the *.txt.files) to your host via FTP and you're done!
Update the content of your sites by simply editing the individual *.txt files and upload them again.
If you want more sites, edit the index.html as described in Step 1 as well as the navigation list (Step 2), create the additional *.txt files and upload them to your server.
Tips:
For the News section in the right column you can use a simple PHP include, for e.g.:
<!-- ##### BEGIN NEWS ##### -->
<?php include("includes/shortnews.txt"); ?>
<!-- ##### END NEWS ##### -->
Create a shortnews.txt file, fill in the content and store it as well in the includes folder on your server.
If you want internal links, use e.g. HTML code like this:
<a href="index.php ?p=news">Read more...</a>
If you want to expand your little CMS, use a small web-based file manager written in PHP like ile Thingie, in order to edit and upload files, to create new folders etc. directly on your server without FTP.
With the power of PHP you can have a simple working CMS for small sites within a few minutes and a few steps, using only one page. Isn't that easy?
Credits:
InfernoForums (Site Templating System Using PHP - Version 1)