PHP - Simple content management system
Content 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
$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
<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
<!– FOOTER –>
<tr><td align="center">
<small>
© <?= date("Y") ?> ACME Toys. All Rights Reserved.
</small>
</td></tr>
</table>
</body>
</html>
page_not_found.html
<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
`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: php, cms, code examples, web development
March 5th, 2007 at 7:10 am
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
March 9th, 2007 at 6:00 am
And how do i edit te page in the database through website?
March 22nd, 2007 at 11:40 am
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
April 23rd, 2008 at 7:42 pm
Did you ever post the admin part Marko?
March 16th, 2009 at 1:00 pm
Hey, is there a section just for latest news