Creating a blog from scratch with PHP
Blog Series
- Part 1 – The Structure
- Part 2 – SEO URLs
- Part 3 Comments with Disqus
- Part 4 Categories
- Part 5 Sidebar, Categories and Archives
- Part 6 Pagination
- Part 7 Tags
- Part 8 Tags Sidebar
This tutorial covers creating a very basic blog. It will consist of posts only. The frontend will consist of only two pages, an index page to list all posts and a view page to view a post.
Reading: How to create a blog from scratch php
There will be a backend control panel to manage posts and admins, this guide will also be there contain a user authentication system to login administrators.
Demo
Download source files
Admin Demo:
See also: How To Create A Website Free Of Cost in 2023?
Username: Demo Password: Demo
I will reinstall the demo every hour.
The file structure will be set up as follows:
There will be two MySQL tables, blog_posts and blog_members.
blog_posts structure:
See also: How To Start A Fantasy Sports Site: FanVictor
blog_members structure
Each page needs a database connection, this is opened in config.php:
Start output buffering, then headers can be used anywhere . Start sessions needed for the admin area.
Define the database connection details and then open a PDO connection.
Also set the time zone, adjust it as needed.
‘php?id=
//If the form has been submitted, process it if(isset($_POST[‘submit’]))){ $_POST = array_map( ‘stripslashes’, $_POST ); //collect form data extract($_POST); //very simple validation if($postTitle ==”){ $error[] = ‘Please enter the title.’; } if($postDesc ==”){ $error[] = ‘Please enter the description.’; } if($postCont ==”){ $error[] = ‘Please enter the content.’; }
Next, if no error was set, insert the data into the database, this uses prepared statements using the placeholders :postTitle, :postDesc, etc. to bind the matching array elements on execution to add the data into the correct columns. After pasting, the user will be redirected back to the administrator. An action status is appended to the URL ?action=added.
See also: How to Create HTML Email and Send it with the Mac Mail App
if(!isset($error)){ try { //insert into database $stmt = $db-php?id=
if(isset($password)){ $hashedpassword = $user-php’); //if not logged in redirect to login page if(!$user-‘; } if($postDesc ==”){ $error[] = ‘Please enter the description.’; } if($postCont ==”){ $error[] = ‘Please enter the content.’; } if(!isset($error)){ try { //insert in database $stmt = $db-php’); } //Display message from add/edit page if(isset($_GET[‘deluser’]))){ //If userid is 1, ignore if($_GET[‘deluser’] !=’1 ‘){ $stmt = $db-php”