Blog
Simple analytics
How to count the number of unique visitors per day? Just create an empty file analytics.log
and add this simple logging code at the end of index.php
:
<?php
$txt = date("Y-m-d") . "\t" . date("H:i:s") . "\t" . $_SERVER['REMOTE_ADDR'] . "\t"
. $_SERVER['REQUEST_URI'] . "\t" . $_SERVER['HTTP_USER_AGENT'] . PHP_EOL;
file_put_contents('./analytics.log', $txt, FILE_APPEND);
?>
Doing python analytics.py
with this Python code will display the number of unique visitors for each day, in a human readable way:
import csv
S = dict()
with open('analytics.log', 'rb') as f:
reader = csv.reader(f, delimiter='\t')
for row in reader:
if row[0] not in S: # row[0] is the date in year-month-day format
S[row[0]] = set() # let's use a set in order to count each IP only once a day
S[row[0]].add(row[2]) # row[2] is the IP
for k in sorted(S):
print k + ': ' + str(len(S[k]))
The result will look like this:
2015-04-19: 226
2015-04-20: 326
2015-04-21: 303
If the number of visitors per day is enough for you (it is for me), you don't need to use Google Analytics. Let's keep it simple and stupid!
One-liners vs. coding stories
Disclaimer: another useless technical article, for programmers only!
I was not very satisfied with this line of PHP code:
<?php
if (trim(dirname(parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH)), '/') === $requestedpage)
{ $requestedpage = ""; }
?>
Happy_the_Exceed, on the IRC channel #PHP
, turned it into a nice story, following his motto
Your PHP code should literally be like a story
Here it is:
<?php
# once upon a time, there were url chunks, derived from myself and the url path!!
$url_chunks = parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH);
# Which were used to get the parent's directory
$parent_directory = dirname($url_chunks);
# Ah, there may be a /, let's give it a jolly good trim!
$somePage = trim($parent_directory, '/');
# Then we attempt to figure out in some odd ball way whether this is a home page or not o_o
if ($somePage == $requestedpage) {
$requestedpage = '';
}
If you haven't understood anything about the previous discussion (or even if you have), here is something relaxing now:
Performance test
We have done some performance tests on a Void website filled with 50 static pages (10 of them displayed in topmenu) and 500 blog articles : the average PHP generation time, on our low-end 5€/month server, is:
- between 0.003 and 0.016 seconds for static pages or single article display
- between 0.020 and 0.045 seconds for the display of the blog page (showing 10 articles at a time)
For this actual website thisisvoid.org, I always get < 0.015 seconds generation time, both for blog and pages.
This is pretty decent, compared to other CMS performances.
Of course, if you have a high traffic site (several 100.000 pageviews per day), you can add caching with an exernal tool.
Why aren't there login/signup, built-in comment features, etc. ?
Short answer: because no one would bother create another account just to post comments anyway.
The kind of sign up forms we all love.
I did a little survey about features that are really needed in a CMS. To this end, I downloaded the full list of top-100K-Wordpress-powered-sites, and randomly visited 20 sites (ok, I should redo on a higher number of websites to have meaningful statistics).
The result is that 18 of them :
- didn't use comments at all (see this random site),
- or all their articles had 0 comments (another one),
- were written by one single user, usually the site's admin: most websites don't have a community of redactors (even for corporate websites, the work is often done by one single person)
That's why Void doesn't provide "login", "sign up", "comment" features out-of-the-box. You can always add it yourself (but it will become heavier and out of the 100 max lines of code philosophy), or easier : use Disqus, like we did here for this actual website.
Good coding practice vs. short code?
This is a technical article, about how this site is internally built-in, if you're not interested about that and just want to use Void, read something else.
I know. We should never have content mixed with layout generation code inside PHP. We should separate into multiple PHP files rather than a single file. We should use templates. And patterns. And controllers. And a framework, instead of flat PHP. etc.
I know that. But the goal here is a bit different: to make the lightest code possible. The result is a single PHP file of less than 100 lines of code. Huh? Is it possible? Yes.
So, well, I haven't followed the usual practice mentioned before, and have focused on short code.
Why? Because when I open an existing code project made by someone else, I prefer reading a single 100-lines file, instead of the same thing split into 17 files, using a package-management-system-that-I-don't-have-and-that-I-have-to-install, using a nice-framework-that-will-make-life-easier for which I don't anything, and have to spend 3 hours to understand the basics.
Moreover, very often when you use a CMS and want to customize things, you'll have to change the template and the PHP anyway, so it does not really matter if the two are separated or not (like here) in such a simple project.
How to write an article or a page?
It's very simple. Create a new .txt
file in /article/
or /page/
folder.
The pages will have a link in the top menu, unless you specify the contrary in the page's information (see below). The articles will be displayed in the blog, sorted by filename in order Z->A
, so I suggest you name them 01.txt
, 02.txt
, 03-blah.txt
, etc.
I know your next question:
How to write these text files now?
Here is a sample page:
TITLE:Home page
AUTHOR:Joseph
##Home
**Void** is a light CMS.
A text file is divided in two parts :
-
the part before the first blank line, which contains informations about the page/article with one of the following (optional) fields:
TITLE
,AUTHOR
,DATE
,URL
,NOMENU
, - the part after the first blank line, which contains the page/article content. It is written using the famous Markdown language.
Beginning of the project
What's the second website ever written with Void, after SamplerBox? The site of Void itself!
For the needs of my projects, I wanted to have a lightweight blogging platform. No more database installation, no more SQL, no more account creation, no more hassle. Here's the result: a CMS coded in a single PHP file, with less than 100 lines of code.
But still, it has all the features that 90% the the websites or blogs that I see everyday have:
-
static pages,
- a blog section with articles,
If you are the 10% who want a blogging platform with more advanced features, then there are a million of cool other platforms that you can use (Wordpress, Ghost, etc.)