日期:2014-05-16 浏览次数:20394 次
A few days ago i had to build a simple comment form. First i thought about MYSQL etc, but this all seems to be too complicated. So i came up with a simple solution based on jSon and a TXT file.
So that’s how it cooks:
1. Load the text file with the comments and convert it to an array with json_decode
1 2 3 4 5 |
/* get comments from file */ $commentsText = file_get_contents ( 'comments.txt' ) ; ? /* create array list from comments */ $commentsList = json_decode ( $commentsText , true ) ; |
2. Check if a new comment was posted and save to file when valid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* check if new comment is posted and minimum 3 characters are set */ if ( ! empty ( $_POST [ 'comment' ] ) && strlen ( $sComment ) > 3 ) { ? /* get posted comment and remove all HTML */ $sComment = strip_tags ( $_POST [ 'comment' ] ) ; ? /* add comment, client IP and date to array */ $commentsList [ 'comments' ] [ ] = array ( 'text' => $sComment , 'ip' |