PHP - Simple content management system

PHP LogoContent management system, or CMS, is a very common web application type. The main idea is to enable site administrator to easily change the content of a web site, without need to know anything about web design and web development process. Usually, CMS provides easy-to-use interface for changing the web site content.

We will see a very simple CMS in this example. It consists of 4 files and an sql script for creating database tables needed.

This is main script. This script will connect to database and retrieve requested page. If page does not exists, user will be redirected to page_not_found.html.
File is well commented, so you should not have any problems understanding it.

The page is called with

page.php?page_name=name

e.g.

page.php?page_name=about_us

Files provided here are just for reading page content. You can practice your PHP skills by writing administrator part - web form and script for writing a page content. Our other examples could be a good guide to accomplish this task.

page.php

<?php
$page_name = $_REQUEST['name'];
// Get the Body of the page
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$sql = "SELECT * from pages WHERE name='$page_name'";
$result = mysql_query($sql) or die(mysql_error() );
// If the page is not found, redirect to a static page
if(mysql_num_rows($result)==0) {
    header("Location: page_not_found.html");
}
$row = mysql_fetch_assoc( $result );
$body = stripslashes( $row["body"] );
// Include the header
include("header.php");
// Print the body of the page
echo $body;
// Include the footer
include("footer.php");
?>

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>ACME Toys</title>
</head>
<!–  BODY –>
<BODY text="#000000" BGCOLOR="#ffffff">
<center>
<!– HEADER –>
<table width="90%" height="100%" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="green"><td align="center"><br>
    <font face="Arial" size="5" style="bold" color="white">ACME Toys</font><br><br>
</td></tr>
<tr><td valign="top"><br>
<!– Main Content–>

footer.php

<br><br><hr></td></tr>
<!– FOOTER –>
<tr><td align="center">
<small>
    &copy; <?= date("Y") ?> ACME Toys. All Rights Reserved.
</small>
</td></tr>
</table>
</body>
</html>

page_not_found.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<code><html>
<head>
<title>ACME Toys</title>
</head>
<!–  BODY –>
<BODY text="#000000" BGCOLOR="#ffffff">
<center>
<!– HEADER –>
<table width="90%" height="100%" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="green"><td align="center">

    <font face="Arial" size="5" style="bold" color="white">ACME Toys</font>

</td></tr>
<tr><td valign="top">

<!– Main Content–>
<h1>Page Not Found</h1>
The requested page was not found on this server. Please specify a different page and try again.

<hr>
<!– FOOTER –>
<tr><td align="center">
<small>
    © 2006 ACME Toys. All Rights Reserved.
</small>
</td></tr>
</table>
</body>
</html></code>

page.sql

CREATE TABLE `test`.`pages` (
   `name` VARCHAR(50) NOT NULL,
   `body` TEXT DEFAULT "
)
ENGINE = MYISAM
CHARACTER SET utf8;
INSERT INTO pages (name, body) VALUES ('about_us', '<h1>About Us</h1>
ACME Toys has been established in 1850 to provide toys to children all over the world'
);

Related Marakana Courses

  • PHP and MySQL Bootcamp Training

Technorati Tags: , , ,

5 Responses to “PHP - Simple content management system”

  1. Marko Says:

    This example is also discussed in my book PHP and MySQL by Example (Prentice Hall 2006) and also thought in our PHP and MySQL Bootcamp.

    Marakana.com, back when it was PHP based, was started on this very CMS system. But the code evolved over the years. If you have any questions, feel free to leave a comment here and I'll get back to you.

    Cheers,
    Marko

  2. Mike Says:

    And how do i edit te page in the database through website?

  3. Marko Says:

    The admin part of the CMS is not shown here. Should be easy to write it. I'll post it at some point, have the code somewhere…

    Marko

  4. hehehehedude Says:

    Did you ever post the admin part Marko?

  5. George Says:

    Hey, is there a section just for latest news

Leave a Reply