vince's blog

This is my blog. I post pieces here about programming, systems, hardware, IT, music, or whatever else I want to talk about.

<- return to all posts

All posts tagged 'blog':

[#1] Creating a blog

Welcome to my new blog! I spent about a week working on this whenever I had some spare time. I hope to add commenting and image functionality soon, but for the time being this blog will just be rambling articles by yours truly.

I set up a MySQL database with four tables: entries, users, comments, and img. Then I did some bare-bones HTML/CSS to lay out the beautiful masterpiece you're looking at now. I used PHP prepared statements to store and retrieve blog posts, added register and login forms and some PHP functions to handle those, and password + salt SHA-1 encryption. Upon registration, I check that the username/email is unique before handling the password and encrypting. Both login and register forms also have a captcha, provided by Securimage. It is a very simple script that I easily implemented in my error checking section. Bonus points for not being provided by Google! The captcha error will print before and instead of any username/email/password errors, so bots cannot phish for valid usernames or emails through those error messages. After validating all information, the site then stores the session in a $_SESSION['user'] associative array, with all of the information retrievable using, for example, $_SESSION['user']['username'] to display the logged in user's username. I obviously only store relevant and non-confidential information in the session.

Pagination was a bit tricky, since I wanted posts to display in descending order. Of course, an SQL select statement can be appended by DESC to retrieve values in descending order, but I needed to calculate $first_index and $last_index to only retrieve the posts needed for that page. I figured this would be faster than retrieving all posts and only displaying the quantity $posts_per_page. After some scratch work here's what I came up with:

$post_count is the result of SELECT COUNT(id)
$posts_per_page is set to 5 currently.
$page is $_GET['page'], default 1

$first_index = $post_count - ($page * $posts_per_page) + 1
$last_index = $first_index + $posts_per_page

In the query, the first index is inclusive while the last index is not.

Lastly, I wanted to have a functional and helpful tagging system, to make topics easier to read about and to show similar posts on a certain topic. I did this by making five tag columns, tag1...tag5. They can be NULL, and if $_GET['tag'] is set, a different select query is used. It does not use $first_index and $last_index and instead has a condition `WHERE (tag1=$_GET['tag']) OR ... OR (tag5=$_GET['tag'])`. Unfortunately this means that tag filter results are not paginated, but I hope to incorporate this soon!

I hope this quick piece was informative to anyone looking for the basic steps to setting up a blog like this. This will be the general format of most posts to come, and as mentioned before I will hopefully have commenting and image (upload) support soon to build a more detailed and interactive platform to talk about tech on! Thanks for reading :)
blog php mysql captcha encryption
watercolour | 09 May 2018 07:48:33