Create a Lightweight Blogging System Using PHP and MYSQL
This post will help you to create your very own lightweight blogging system. This system is completely created by using PHP and mysql. This system consists of 4 files namely admin.php, index.php, art.php and .htaccess. We will go through each files deeply. This time on we have used automatic setup system which will automatically create database and tables. In this system we have created a database article and table article.
Lets see the screens of various pages:
Lets go through various sections of this blogging system Lets create the index.php file
Lets create the admin.php file
Lets create the art.php file
Lets create the .htaccess file
Alternatively you can download the whole system here.
Found bugs, feel free to report them. We'll be glad to fix them ! :D
Lets see the screens of various pages:
The homepage of the Blogging system showing the titles of the posts.
When clicked on any post, the page of the blogging system
The admin control panel of the blogging system where the admin can add new articles
Lets go through various sections of this blogging system Lets create the index.php file
// A function to initiate automatic setup of database and tables function setup() { echo(' <p style="color: #008000; text-align: left; font-size: 15pt;"">-Automatic setup is started...</p> '); global $host,$username,$password,$link; //$link=mysql_connect($host, $username, $password); $sql= 'CREATE DATABASE article'; if (!mysql_query ($sql, $link)) die(' <p style="text-align: center; font-size: 20pt;"><span style="color: #FF0000;">Failed to create database! </span><br><span style="font-size: 12pt;">>>Please check the parameters and database server<<</span></p> '); $sql = "CREATE TABLE `article`.`article` ( `ID` INT NOT NULL , `title` TEXT NOT NULL, `body` TEXT NOT NULL, `time` TEXT NOT NULL ) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;"; mysql_query($sql) or die('Setup Failed'); echo(' <p style="color: #008000; text-align: left; font-size: 15pt;"">-Automatic setup completed successfully. Your Database is ready!</p> '); } // Setting up your blog title echo "<div class='header'>Your Blog Title Here</div><br/><div class='container'>"; //Displaying the post title in the index page while(@$rows=mysql_fetch_array($result)){ $title=$rows['title']; $time=$rows['time']; $link=$_SERVER['PHP_SELF']; $l=strrpos($link,'/'); $link=substr($link,1,$l-1); $link=$_SERVER['HTTP_HOST'].'/'.$link.'/'.str_replace(' ','_',$title); echo"<div class='time'>$time</div><a style='text-decoration:none;' href='http://$link'><span style='font-size: 14pt;'>$title</span></a><br/>"; }
Lets create the admin.php file
// Inserting data into the article table when post is clicked ! if (isset($_POST['add'])){ $id=$cn; $title=$_POST['title']; $body=$_POST['body']; $time=date('H:i', time()); $body=str_replace("\n",'<br>',$body); //var_dump( $body); $body=str_replace("<",'\r\n<',$body); $sql="INSERT INTO article(ID,title,body,time)VALUES('$id', '$title', '$body', '$time')"; $result=mysql_query($sql); if (!$result) die (mysql_error()); } // Changing the title of the Blog echo "<div class='header' align='center'>Your Blog Title Here</div>"; // Displaying the List of articles published $sql="SELECT * FROM article"; $result=mysql_query($sql); while(@$rows=mysql_fetch_array($result)){ $title=$rows['title']; $link=$_SERVER['PHP_SELF']; $l=strrpos($link,'/'); $link=substr($link,1,$l-1); $link=$_SERVER['HTTP_HOST'].'/'.$link.'/'.str_replace(' ','_',$title); echo"<br><a style='text-decoration: none;color: white;font-size: 10pt;' href='http://$link'><span>>$title</span></a> "; }
Lets create the art.php file
// Selecting the clicked post from the index page and displaying it here $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $link=mysql_connect($host, $username, $password); $sql="SELECT * FROM article"; $db_name="article"; $result=mysql_select_db($db_name); $result=mysql_query($sql); @$cn=mysql_num_rows($result); $cn++; if (isset($_GET['title'])) { $title=str_replace('_',' ',$_GET['title']); $sql="SELECT * FROM article WHERE title='$title'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); echo "<div class='header'>Your Blog Title Here</div><br/>"; echo '<div class="link"><a href="index.php" >Back</a></div><div class="tm">'.$rows['time'].'<br/><center><span>Time</span></center></div><div class="post"><p align="center" style="font-size: 20pt;"><b>'.$rows['title'].'</b></p><p align="left">'.$rows['body'].'</p></div><div class="footer">CMS Designed By dProBuk. <a href="admin.php">Admin Panel</a></div>'; }
Lets create the .htaccess file
RewriteEngine on RewriteRule ^([A-Za-z0-9\_]+)$ art.php?title=$1
Alternatively you can download the whole system here.
Found bugs, feel free to report them. We'll be glad to fix them ! :D
Comments
Post a Comment