Personal Home Page
It’s been a while since I looked back into my blog, holding good with how I thought it would turn out to be. And it’s sooner than I anticipated, which is bad. But then I’ve come to a milestone here that’s encouraging me to get back to writing. 1000 hits to my site. Talking about blogs that get such a response each day, this isn’t anything to brag about. But it sure feels great.
A counter plugin that’s been installed is doing the job. And then there’s my own script that’s running everytime a page on this site is loaded. A simple PHP script. Implementing a counter is possibly one of the simplest things you could achieve with it, but its significance is not to be played down for the reason that the very existence of this language arised merely from the need for keeping visitor count. A 25 year old Rasmus Lerdorf, back in 1994, wanted to track the visitor hits to his personal website, and put together a set of Perl scripts and formed what he called as Personal Home Page Tools. And over the following years, the language was open-sourced which led to the rapid development and evolution of the scripting language what we now know as the PHP: Hypertext Processor.
I do not have much of an idea about Perl, but talking PHP, the implementation of a counter can get as simple as this.
<?php
$file=fopen('counter.txt','r');
$count=fgets($file);
fclose($file);
$count++;
$file=fopen('counter.txt','w');
fwrite($file,$count);
fclose($file);
?>
Pretty easy to catch, but however if there’s a need to explain, the code works this way. The counter is text file which holds the number of hits. The counter file is opened first for reading, and its contents are fetched through the fgets() function and stored in the variable count, following which the file is closed. The variable count is incremented and then the counter file is opened again, for writing this time. The value of count is written onto the file using the fwrite() function and then the file is closed. I know that this doesn’t comply with good coding standards. I should rather be dealing with databases. But this does the job for me very well. And the probablity of losing any hits due to concurrent access anomalies must be next to nil. At least for my site. And I can see by comparing that it’s been consistent so far.
But I think it is time for me to change my code now. Time to rework it to a better code.
<?php
mysql_connect('hostname','db_username','db_password');
@mysql_select_db('database_name');
$query='UPDATE counter SET count=count+1';
mysql_query($query);
?>
Oh well, this is shorter too. And this way, the table counter in the database has a field count which is incremented every time a page is run. Alright. This is good.
Bearing a close resemblance to C++, anybody who’s learnt programming at school should be able to pick it up in no time. And the best of all is what you could actually do with it being a web-scripting object-oriented language. For how I began using it to just scrape data off the web, things in it turned out to be so elegant that it drew my interest and now I’ve picked it up quite enough to be doing all of what I do on it.
This language now, means a lot to me.
Edit: Sep 2012 This post is embarrassing to look at now. But I’m still just going to leave it as it is. :|

Recent Comments