PHP Code for Tiny Blog Home Page
Blog is another word for weblog. A weblog is a website that is like a diary or journal. Bloggers often write about their opinions and thoughts. What bloggers write is called posts, which are blocks of writing on the blog. Posts often include links to other websites, and often images and even videos as well. Blogs can have many writers. If they have more than one writer, they are often called community blogs, team blogs, or group blogs. But more and more blogs let any visitors leave comments in an interactive format. Signing up and logging in is often required for this privilege. This interaction with visitors is an important part of many blogs.
Forums always have lots of writers plus an administrator and they're about a specific area of interest and visitors get to post rather than just comment on others' posts, whereas blogs are written by one or a few people and they're more about this person or group's opinions and visitors comments on these opinions. Both forums and blogs can have categories—especially larger ones. Forums can have Q and A, but blogs rarely do.
Blog posts are commonly displayed in reverse-chronological order—i.e., from latest to earliest. Blog comments are posts in which a person says what they think about the blog post. Such comments can be considered posts, replies, or comments. Blogs are good for discussions — if someone writes something that someone else disagrees with, they can fix it by writing a comment on the blog. Someone else cannot change what the post says, but the writer of the post can. Sometimes only the blog owner can change or delete posts or comments, while visitors can simply read the blog and leave comments.
Often people create RSS feeds to their blogs. Programs called content aggregators or news readers can put postings from all the blogs that a person likes (that have RSS feeds) in one convenient place. Many people find it easier to read all the new posts from blogs in one place, through RSS, instead of going to each site one at a time. This saves a lot of time!
Blogs, like forums, are great communication tools for the exchange of ideas, for people teaching others about a specific area of interest, or even for just general social communication. The fact that they are usually so specialized helps get them high up in search results as well as contributing considerably to bodies of knowledge, although forums and news groups usually do this better than blogs, which are often more like diaries than information sources. True, there's a lot of misinformation and putdowns on both, but this invariably occurs when people communicate. One must learn to take what one learns with a grain of salt.
A forum, also know as a message board, is an online discussion site where people can converse, argue, inform, teach, or bitch in the form of posted messages. Chat rooms deliver messages in real-time but with forums and blogs, getting new messages requires the page to be reloaded (F5). Forums call a single conversation a thread. Blogs simply have posts and comments. Even though on either blogs or forums users have to login to post messages, usually they don't have to login to read the existing messages.
Download the files: cms-blog.zip but replace the cms-blog.php file with the code below (name it cms-blog.php), but only if you have decided you do not want the blog home page to initially display the 10 most recent complete topics, or to display complete topics if you click on a category in a sidebar, but instead to only display titles in a table, and each is a link to the topic and replies viewing page where replies can be added. This titles-only feature is available in both the regular blog code and the small blog code. Links to the code for these alternative blog home pages are below.
Content Management System: Blogs
- regular blog: home page
- small blog: home page
- tiny blog: home page
- blog: search
- blog: login
- blog: topic and replies viewing page
- blog: add topic to database
- blog: add reply to database
- blog: edit topic in database
- blog: create topic in database
- blog: delete topic in database
- blog: delete reply in database
- blog: create categories in database
- blog: edit categories in database
- blog: open or close topic
- blog: delete user account in members table
The titles display feature displays in a table each topic's Category, how many replies it's had, how many views it's had, and the date and time of its creation. Depending on which topic link is clicked, its corresponding id will be sent via query string (along with the username) to the blog topic and replies viewing page and only this topic and its replies will be visible on this viewing page. For other topics, you must return to the blog home page from a Return to Blog link on the blog topic and replies viewing page.
On to the PHP code. As usual, we start with config.php, since without it, the MySQL-based blog would not be viable. You cannot relate to a db without knowing the magic words. Next, the security of the page is dealt with by ensuring the page visitor has a username that's in the database. Note that the various pages on our blog app use both forms and URL query strings to transfer data between pages, so both POST and GET are checked for username, and if neither works, the visitor is sent to the login script. Not only is the db checked for a valid username, the username is checked to make sure it has only 6 to 20 letters, numbers or underscore in it and no other characters—otherwise, it's off to the login script. If a hacker has put something nasty in the query string, he'll end up at the login script. All our blog app scripts have this same (almost) username checker at the top of the PHP section—except for the login script. We say "almost" because most pages only allow the administrator access because most pages are about adding, deleting, or editing topics, replies, or categories. So, seeing if the username is the administrator's is in the user checker on most of these blog app pages.
Speaking of seeing if the username is the administrator's, we do that next because the page's links will differ depending on who the user is that's currently using the blog. Regular visitors will not be able to even see the links Create New Topic or Edit Categories, for instance, but the administrator will. The regular users will be able to see the titles links, which will also take them to the topic and replies viewing page. if($U<>"DIRTY_dog_DROPPINGS_"){$am_i_admin="cms-view-blog-topic.php";$thelinktext=""; is our way of coding "if you aren't the administrator we will make the links disappear." The administrator's username is a bit silly, as you see. Feel free to change it (to AfDqC_1f3_DkI3j5k9N_ for example) when you register the administrator username and password, but you must search and replace ALL blog app pages for our silly name and replace it with your not-as-silly name or you'll have more problems than a pregnant nun. Anyway, in that code you can see that the link is of no consequence because the link text that will be for clicking on is an empty string so the user will see nothing. Note that in the script below the admin person gets good links with good query strings in them so username and topic id will be passed to that blog app.
Next comes the blog tables. This script creates them if they do not exist. We'll need a topic table, blog_question, with fields: id, topic (which is the title), detail (which is the main content), name, email, datetime, category, open (the topics' open/closed flag), topics_username, view (which is the number of views of the page), and reply (which is the number of replies to the page). We'll also need a replies table, blog_answer, with fields: id, question_id, a_id, a_name, a_email, a_answer (which is the main content of the reply), and a_datetime.
Moving on, htmlentities() and stripslashes() are used to provides display security on this page—just in case the data has somehow gotten compromised.
You're allowed to have bold, underscore, italics, links, email links, pictures, videos, and audios in either topics or replies. The only limitation is that you must use our tag system when typing in new topics and/or replies, which isn't dealt with on this home page. But topics are displayed when you click on a topic link (because it sends the user to a topics and replies viewing page) and that is where you'll see the bold, underscore, italics, links, email links, pictures, videos, and audios in topics and/or replies. And here are the custom tags you will use, there, in writing topics or replies:
No single or double quotes or Enter/Return allowed in topic titles. Use letters, numbers, spaces and these: , . ) ? : ( ; _ - ! / in title and content. In content only, you may use single or double quotes or Enter/Return. Use Returns for new paragraphs. For italics, starting and ending tags are (i-) and (ii-). For bold, use (b-) and (bb-). Underline is (u-) and (uu-). For links, use (l-) then domain without http://, then (ll-) then link text, then (lll-). For emails, use (e-) then email address with (ee-) instead of @, then (eee-) then subject, then (eeee-) then link text, then (eeeee-).
For pictures, use (p-) as start tag, then the complete picture URL with .jpg or .png or .gif or .bmp extension, then (pp-) as end tag. If the picture somehow is on the blog server near the blog folder but is in a higher level folder than the subfolder your Content Management System is in, put 2 dots before the name and just make sure you use the picture name only, not a URL path, for this special case. If it's in a subfolder like "images", then move it to the higher level folder or the subfolder your Content Management System is in. If the image is wider than 580 pixels, resave it to 580.
For YouTube video, use (v-) as start tag, then the letter code after http://www.youtube.com/v/ and before &hl=en_US&fs=1& in your video's YouTube Embed code, then (vv-), then the same letter code again, then (vvv-) as end tag. If the video you want to use is not on YouTube, upload it there before proceeding.
For audio, use (a-) as start tag, then the sound's name, then (aa-), then the complete sound file URL ending in .mp3 or .wav, then (aaa-) as end tag. Make sure the site administrator has put the sound.js file in the folder your blog Content Management System is in, and do not try to put this JavaScript on the web page—it needs to be a separate file. Save the following code (exactly, without adding script tags or anything else!) as sound.js:
function sound(s,q) {document.getElementById(q).innerHTML="<embed src='"+s+"' hidden=true autostart=true loop=false>"}
The code now deals with the titles displaying script. Note that the displaying code is quite usual until we hit the title link. Here the link with the username and id is in the link's query string, but then the link text that you click on is the topic title field from the blog_question table. The db is supplying everything we need, in this example. At the bottom of this page are the links for Delete Account and Search Blog both of which are for anyone to use, but also 2 admin-only links: Create New Topic and Edit Categories whose strings are defined at the beginning of the first PHP code section.
SAVE THIS PAGE AS: cms-blog.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Blog—Content Management System (CMS)</TITLE>
<meta name="description" content="Blog—Content Management System (CMS)">
<meta name="keywords" content="Blogs,Blog,Content Management System,Content Management System Articles,php,CMS,javascript, dhtml, DHTML">
<style type="text/css">
BODY {margin-left:0; margin-right:0; margin-top:0;text-align:left;background-color:#ddd}
p, li {font:13px Verdana; color:black;text-align:left}
h1 {font:bold 28px Verdana; color:black;text-align:center}
h2 {font:bold 24px Verdana;text-align:center}
td {font:normal 13px Verdana;text-align:center;background-color:#ccc}
.topic {text-align:left;background-color:#fff}
.center {text-align:center;}
</style>
</head>
<body>
<?php
include_once"config.php";
$U=$_POST['username'];if (!isset($U)){$U=$_GET['username'];}
if (isset($U)&&preg_match("/[A-Za-z0-9_]{6,20}$/",$U)){$check_user_data = mysql_query("SELECT * FROM blogmembers WHERE username='$U'") or die(mysql_error());if(mysql_num_rows($check_user_data)==0){unset($U);}}else{unset($U);}
if (!isset($U)){echo '<script language="javascript">alert("Please login.");window.location="blog-login.php"; </script>';}
if($U<>"DIRTY_dog_DROPPINGS_"){$am_i_admin="cms-blog.php";$thelinktext="";$am_i_admin2="cms-blog.php";$thelinktext2="";}else{$thelinktext="Create New Topic";$am_i_admin="cms-create-blog-topic.php?username=".$U;$thelinktext2="Edit Categories";$am_i_admin2="cms-blog-edit-category.php?username=".$U;}
$sql = "CREATE TABLE IF NOT EXISTS blog_question (
id int(4) NOT NULL auto_increment,
topic varchar(255) NOT NULL default '',
detail text NOT NULL,
name varchar(65) NOT NULL default '',
email varchar(65) NOT NULL default '',
datetime varchar(25) NOT NULL default '',
category varchar(30) NOT NULL default '',
open tinyint(1) NOT NULL default '1',
topics_username varchar(20) NOT NULL,
view int(4) NOT NULL default '0',
reply int(4) NOT NULL default '0',
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1";
mysql_query($sql);
$sql = "CREATE TABLE IF NOT EXISTS blog_answer (
id int(4) NOT NULL auto_increment,
question_id int(4) NOT NULL default '0',
a_id int(4) NOT NULL default '0',
a_name varchar(65) NOT NULL default '',
a_email varchar(65) NOT NULL default '',
a_answer text NOT NULL,
a_datetime varchar(25) NOT NULL default '',
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1";
mysql_query($sql);
$tbl_name="blog_question";
$sql="SELECT id,topic,category,view,reply,datetime FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
?>
<center><div><h2>My Blog</h2></div></center>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCC">
<tr>
<td width="5%"><B>#</B></td>
<td width="51%"><B>Topic</B></td>
<td width="16%"><B>Category</B></td>
<td width="6%"><B>Views</B></td>
<td width="6%"><B>Replies</B></td>
<td width="16%"><B>Date/Time</B></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo htmlentities(stripslashes($rows['id']), ENT_QUOTES); ?></td>
<td class='topic'><a href="cms-view-blog-topic.php?id=<? echo htmlentities(stripslashes($rows['id']), ENT_QUOTES); ?>&username=<? echo stripslashes($U); ?>"><? echo htmlentities(stripslashes($rows['topic']), ENT_QUOTES); ?></a><BR></td>
<td><? echo htmlentities(stripslashes($rows['category']), ENT_QUOTES); ?></td>
<td><? echo htmlentities(stripslashes($rows['view']), ENT_QUOTES); ?></td>
<td><? echo htmlentities(stripslashes($rows['reply']), ENT_QUOTES); ?></td>
<td><? echo htmlentities(stripslashes($rows['datetime']), ENT_QUOTES); ?></td>
</tr>
<?php
}
mysql_close();
?>
<tr>
<td class='center' colspan="6"><a href="<? echo $am_i_admin2; ?>"><B><? echo $thelinktext2; ?></B></a> <a href="<? echo $am_i_admin; ?>"><B><? echo $thelinktext; ?></B></a> <a href="cms-search-blog.php?username=<? echo stripslashes($U); ?>";><B>Search Blog</B></a> <a href="cms-delete-blog-account.php?username=<? echo stripslashes($U); ?>";><B>Delete Account</B></a></td>
</tr>
</table>
</body>
</html>