Building a Custom CMS with PHP and MySQL: A Step-by-Step Guide

Home - Technology - Building a Custom CMS with PHP and MySQL: A Step-by-Step Guide

Content Management Systems (CMS) allow users to manage website content efficiently. While platforms like WordPress exist, sometimes you need a custom-built CMS tailored to specific requirements. This guide walks you through creating a CMS using PHP and MySQL. Whether you’re an individual developer or working with Web Development Services in India, mastering CMS development can be highly beneficial.

Why Build a Custom CMS?

Before diving into development, here’s why building your own CMS might be the right choice:

  • Full Control – Customize features according to your needs.

  • Lightweight & Fast – No unnecessary bloatware slowing things down.

  • Enhanced Security – Reduce vulnerabilities found in pre-built CMS platforms.

  • Better Performance – Optimize database and front-end for speed.

Prerequisites

To follow this guide, you need:

  • Basic knowledge of PHP & MySQL

  • A local server environment (XAMPP, MAMP, or WAMP)

  • A text editor (VS Code, Sublime Text, or PHPStorm)

  • Basic understanding of HTML & CSS

Step 1: Setting Up the Environment

First, install XAMPP or WAMP to set up a local development environment. Start Apache and MySQL services.

Create a Database in MySQL

Use phpMyAdmin to create a new database:

CREATE DATABASE custom_cms;

Then, create a users table to manage admin logins:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin', 'editor') NOT NULL
);

Create a posts table to store content:

CREATE TABLE posts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    author VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Step 2: Setting Up the PHP Backend

Database Connection

Create a file db.php to handle database connections:

<?php
$host = 'localhost';
$dbname = 'custom_cms';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}
?>

User Authentication System

Create a login.php file for authentication:

<?php
session_start();
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();

    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user'] = $user['username'];
        $_SESSION['role'] = $user['role'];
        header("Location: dashboard.php");
    } else {
        echo "Invalid credentials";
    }
}
?>

Step 3: Building the CMS Dashboard

Create dashboard.php to display a user-friendly interface for managing content.

<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit();
}
?>
<html>
<head><title>Admin Dashboard</title></head>
<body>
    <h1>Welcome, <?php echo $_SESSION['user']; ?></h1>
    <a href="add_post.php">Add New Post</a> | <a href="logout.php">Logout</a>
</body>
</html>

Step 4: Creating and Managing Posts

Add Post Page (add_post.php)

<?php
session_start();
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = $_POST['title'];
    $content = $_POST['content'];
    $author = $_SESSION['user'];
    
    $stmt = $pdo->prepare("INSERT INTO posts (title, content, author) VALUES (?, ?, ?)");
    $stmt->execute([$title, $content, $author]);
    echo "Post added successfully!";
}
?>
<form method="post">
    <input type="text" name="title" placeholder="Title" required><br>
    <textarea name="content" placeholder="Content" required></textarea><br>
    <button type="submit">Add Post</button>
</form>

Displaying Posts (index.php)

<?php
include 'db.php';
$posts = $pdo->query("SELECT * FROM posts ORDER BY created_at DESC")->fetchAll();
foreach ($posts as $post) {
    echo "<h2>{$post['title']}</h2>";
    echo "<p>{$post['content']}</p>";
    echo "<small>By {$post['author']} on {$post['created_at']}</small><hr>";
}
?>

Step 5: Enhancing the CMS

Adding User Roles

Modify the users table to allow different user roles (admin, editor).

  • Admin can manage users and posts.

  • Editor can add and edit posts but not delete them.

Implementing Security Best Practices

  • Use Prepared Statements to prevent SQL injection.

  • Hash Passwords using password_hash() before storing them in the database.

  • Restrict Access to sensitive pages by checking user roles.

Conclusion

Building a custom CMS with PHP and MySQL allows for greater control, security, and performance. Whether you’re developing for personal projects or as part of Web Development Services in India, understanding CMS development can significantly enhance your skills. For expert web development solutions, visit Dignizant Technologies.

Hitvanshi Kevadiya

Table of Contents

Recent Articles