You are on page 1of 11

Content Management System Admin Panel

Tutorial
So when a user enters correct username and password in login.php page, that is
verified by verify_user.php, which in turn transfers to admin_panel.php page.

Our admin_panel.php page performs 8 different jobs.

1. Creating a New Category.


2. Deleting a existing category.
3. Creating a New article
4. Displaying all the existing articles.
5. Displaying articles based on categories.
6. Deleting existing article.
7. Editing existing article.
8. Logging out of Administration Panel.

admin_panel.php

<?php
session_start();
if(isset($_SESSION['name']))
{
if(!$_SESSION['name']=='admin')
{
header("Location:login.php?id=You are not authorised to access this page unless you
are administrator of this website");
}
}
else
{
header("Location:login.php?id=You are not authorised to access this page unless you
are administrator of this website");
}
?>
<?php
/*
connecting to mysql database
hostname : localhost
username : root
password : 123456
*/
$con = mysql_connect("localhost","root","123456");
if(!$con)
{
die("connection to database failed".mysql_error());
}

9. /* selecting the database "cms" */


$dataselect = mysql_select_db("cms",$con);
if(!$dataselect)
{
die("Database namelist not selected".mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#hold #log {
color: #EE4902;
}
</style>
<link href="admin_style.css" rel="stylesheet" type="text/css" />
</head>
10. <body>
<div id="hold">
<div id="top">
<h2 align="center">CONTENT MANAGEMENT SYSTEM
ADMINISTRATION PANEL</h2>
</div>
<div id="log">
<?php
echo "Welcome ".$_SESSION['name'];
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<a href=logout.php>Logout</a>";
?>
</div>
<div id="left">
<a href=new_category.php >Create New Category</a><br/>
<a href=remove_category.php >Remove a Category</a><br/>
<a href=create_new.php >Create New Article</a><br/>
<a href=admin_panel.php?id=viewall>View all Articles</a><br/><br/>
<b>Articles by Category</b>
<?php
$qry=mysql_query("SELECT * FROM category ", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
/* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry))
{
echo "<li><a
href=admin_panel.php?cat=".$row['category'].">".$row['category']."</a></li>"
;
}
?>
</div>
<div id="right">
11. <?php
if(isset($_GET['id'])=="viewall")
{
$qry=mysql_query("SELECT * FROM articles order by articles.id DESC ",
$con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
echo "<table>";
/* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry))
{
echo "<tr>";
echo "<td><a href=articles.php?id=".$row['id'].">".$row['title']."</a></td>";
echo "<td><a href=edit_article.php?id=".$row['id'].">edit</a></td>";
echo "<td><a href=delete_article.php?id=".$row['id'].">delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
?>
12. <?php
if(isset($_GET['cat']))
{
$cat=$_GET['cat'];

$qry=mysql_query("SELECT * FROM articles WHERE category='$cat' order


by articles.id DESC", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
echo "<table>";
while($row=mysql_fetch_array($qry))
{
//echo $row['title'];
echo "<tr>";
echo "<td><a href=articles.php?id=".$row['id'].">".$row['title']."</a></td>";
echo "<td><a href=edit_article.php?id=".$row['id'].">edit</a></td>";
echo "<td><a href=delete_article.php?id=".$row['id'].">delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
?>
</div>
</div>
</body>
</html>
Explanation for admin_panel.php

The first line is to start a new session.

session_start();

And the following if condition is to verify whether the logged in user is a authorized
administrator. If the session is not verified as admin, the url will be directed to
login.php page with a message passed through the url variable id. The following code
does that.

if(isset($_SESSION['name']))
{
if(!$_SESSION['name']=='admin')
{
header("Location:login.php?id=You are not authorised to access this page unless
you are administrator of this website");
}
}
else
{
header("Location:login.php?id=You are not authorised to access this page unless
you are administrator of this website");
}

Then as usual php connects to mysql database using hostname, username and
password with mysql_connect().

$con = mysql_connect("localhost","root","123456");
if(!$con)
{
die("connection to database failed".mysql_error());
}

Then the database CMS is selected using mysql_select_db() function.

$dataselect = mysql_select_db("cms",$con);

Then administrator is welcomed in the admin_panel.php page using the session[]


statement.

echo "Welcome ".$_SESSION['name'];


Then a hyperlink option to logout of the admin panel is given, which when clicked,
the control will transfer to logout.php page, which in turn does the actual session
logout process.

echo "<a href=logout.php>Logout</a>";

A hyperlink to create a New Category is placed, which when clicked will transfer to
new new_category.php page.

<a href=new_category.php >Create New Category</a><br/>

An option to remove a category is placed, which when clicked will transfer to


remove_category.php page.

<a href=remove_category.php >Remove a Category</a><br/>

Also an option to create a new article is placed, which when clicked will transfer to
create_new.php page.

<a href=create_new.php >Create New Article</a><br/>

View all articles options is placed which when clicked will pass a value “viewall”
through the url variable id to the admin_panel.php page itself.

<a href=admin_panel.php?id=viewall>View all Articles</a><br/><br/>

Then the CMS database table category is queried to display the names of all the
available categories. And the category names are displayed as hyperlinks, which when
clicked will pass the respective category name to the url variable “cat” in the
admin_panel.php page itself.

$qry=mysql_query("SELECT * FROM category ", $con);


if(!$qry)
{
die("Query Failed: ". mysql_error());
}
/* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry))
{
echo "<li><a
href=admin_panel.php?cat=".$row['category'].">".$row['category']."</a>
</li>";
}

Next a conditional statement is used to check whether the url variable id holds the
value “viewall”.

if(isset($_GET['id'])=="viewall")

If the value is set the CMS database table articles is queried to display the names of all
the available articles.

$qry=mysql_query("SELECT * FROM articles order by articles.id DESC ",


$con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}

And these article title’s are displayed as hyperlinks in the 1st column of each table
row.

echo "<td><a href=articles.php?id=".$row['id'].">".$row['title']."</a></td>";

The 2nd column of each table row is filled with edit option, which when clicked will
pass their corresponding article id to the edit_article.php page.

echo "<td><a href=edit_article.php?id=".$row['id'].">edit</a></td>";

The third column of the table rows are filled with delete option, which when clicked
will pass the corresponding article id to delete_article.php page.

echo "<td><a href=delete_article.php?id=".$row['id'].">delete</a></td>";

A conditional statement is used to check whether the url variable is set. If it is set the
CMS database table articles is queried with the particular category name received
through the url variable “cat” to display all the articles belonging to that particular
category.

if(isset($_GET['cat']))
{
$cat=$_GET['cat'];
$qry=mysql_query("SELECT * FROM articles WHERE category='$cat' order
by articles.id DESC", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
echo "<table>";

And those articles are displayed in the 1st columns of the table, whereas the 2nd and
3rd columns are used for editing the articles and deleting the articles respectively.

while($row=mysql_fetch_array($qry))
{
echo "<tr>";
echo "<td><a href=articles.php?id=".$row['id'].">".$row['title']."</a></td>";
echo "<td><a href=edit_article.php?id=".$row['id'].">edit</a></td>";
echo "<td><a href=delete_article.php?id=".$row['id'].">delete</a></td>";
echo "</tr>";
}

Output :
admin_style.css
#hold {
float: left;
height: 500px;
width: 900px;
position: relative;
}
#top {
float: left;
height: 60px;
width: 900px;
position: relative;
background-color: #F60;
}
#log {
height: 25px;
width: 900px;
float: left;
position: relative;
}
#left {
float: left;
width: 300px;
position: relative;
padding-top: 10px;
padding-left: 20px;
}
#right {
float: left;
width: 500px;
padding-top: 10px;
padding-left: 20px;
position: relative;
left: 20px;
}
#hold #top h2 {
color: #EE4902;
}
#hold #top h2 {
color: #000;
}
#hold #top h2 {
color: #FFF;
}
#work_area {
float: left;
width: 800px;
position: relative;
padding-top: 20px;
padding-left: 50px;
}
#hold #work_area h2 {
color: #EE4902;
}
#hold #work_area #form1 p {
color: #EE4902;
}
#hold #work_area p {
color: #EE4902;
}
#hold #work_area #form1 {
color: #EE4902;
}

table
{
border:1px solid black;
border-collapse:collapse;
}

td
{
border:1px solid black;
}

You might also like