Running a php script with compatibility to php4/php5 installed on host machine

I had to write a php script to do some processing on xml input. I executed the code using this line:

php <script_name>.php

The code was working fine on Mac OSX operating system. So, I checked in the code and later I checked out the code on a linux machine using RHEL 4.6 operating system. When I gave the same command on my linux machine, it showed errors like unknown function. So, I checked the php versions installed on Mac OSX  machine and RHEL machine using the command below.

php -v

The output on Mac OSX was:

PHP 5.3.2 (cli) (built: Aug  7 2010 00:04:41)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

The output on RHEL machine was:

PHP 4.3.9 (cgi) (built: Sep 12 2007 11:09:31)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

So, the difference in the PHP version was the reason why the code written on Mac OSX which was actually a PHP5 script didn't run on the RHEL machine which had PHP4 installed. So, I had to do some modifications to the original PHP5 script to create a new PHP4 script which was now running as desired on RHEL machine.

I wanted to run the php script oblivious of the php version installed on the machine on which I am running it.  So, I had to write a shell script which extracted the php version installed on the machine and then select the php script which was compatible with the php version installed on this machine. The script I wrote is below.

# Variables to read php version
php4_version=`php -v | grep "PHP 4"`
php5_version=`php -v | grep "PHP 5"`

# One of the variables length should be exclusively greater than 0
if [ ${#php4_version} -gt 0 ]
then
 file="<php4_compatible_script>.php"
 elif [ ${#php5_version} -gt 0 ]
then
 file="<php5_compatible_script>.php"
else
  echo "Error: Unexpected php version"
fi
# Executing the php script
php $file

The script works by extracting the version from 'php -v' using 'grep' command. On a php4 version machine, php5_version variable would be empty and on a php5 version machine, php4_version variable would be empty. Using this notion, we check on the length of the variable to select the php script which would be compatible with the machine. Then in the last step we execute that php script.

Do let me know of better ways of achieving this result by leaving behind a comment.

Facebook Share button and Like button stats do not reflect what we may perceive

We may think, that counter shown near the Facebook share button at some blog post represents the count of unique Facebook users who have actually shared the link to that post on their wall. Similarly, we think that the counter near the Like button represents the count of unique Facebook users who have liked that post(link). But that is not true, the stats do not exactly represent what we may generally perceive them to be.

I became suspicious about the facebook share button count, when the count on one of my blog posts, had reached 6 yesterday. For a moment, I was pleasantly surprised that my post was getting shared across the web by a few facebook users. But after a moment, I realized that there was something wrong, because firstly, the post was not something great and secondly, I do not have a large (or actually even small) fan following that my post would be shared 6 times within a day. Investigating the reason behind it on the web, I discovered what that count actually represents.

Facebook share count = No. of times it has been shared +
                       No. of comments on that shared wall post +
                       No. of likes on that shared wall post.

At the time of writing this blog, the Facebook share count on that post was 7, which was because I had shared this link on my profile page and on my facebook page for this blog, and 4 comments were made on that shared post at my profile page and 1 like for that post at my profile page.

So, the question is how can we actually get to know the number of unique users who actually shared a blog post or link. To get the exact count, you can copy paste the line below into your browser's address bar, replacing the URL_YOUR_LINK by url of the blog post or any other link that has been shared on facebook.

http://api.facebook.com/restserver.php?method=links.getStats&urls=URL_YOUR_LINK  

<share_count> tag value represents actually how many times your link has been uniquely shared by distinct facebook users. But the count we see near the facebook share button is value of <total_count>.

For example, for viewing the actual stats of my blog post, click on this link or copy this link  http://api.facebook.com/restserver.php?method=links.getStats&urls=http://technoflirt.com/noflirt/2010/12/12/sticky-note-part-2/ into your browser's address bar.

Similarly, the stats for Facebook like button counter is actually the value of <total_count> tag value. For example, the number of likes on Drupal: Facebook-style Micropublisher Preview is shown as 5, but if you click this link http://api.facebook.com/restserver.php?method=links.getStats&urls=http://publicmind.in/blog/fbsmp_preview/ , you will notice that <like_count> value is actually 4.

Thus, the bottom line is that facebook share counts  and facebook like counts are actually deceiving. However, the comments or likes made by a facebook user xyz on a post abc shared by another facebook user abcd, are in some sense helping share the link as these activities appear in news feed of that user xyz as "user xyz likes some abc link" or "user xyz has commented on user abcd's link" . Such activities are now visible to the user xyz's connections. So, comments and likes are related to sharing a link. But even if the same user comments 5 times on a shared post, the facebook share button count increases by 5, because this count is related to how many times the link appears in the facebook activity feed of all the facebook users. So, I guess the count should actually represent the unique number of users who share, comment or like some link/post i.e. keep count of the unique association of users with some link.

Designing a basic php counter for your website

I think you may have noticed on several websites, a line in the footer saying "You are visitor no. ..." or something on similar lines or maybe something humorous like "You are jobless no. ...". The number displayed is the value of a dynamic counter which keeps increasing each time that page is being fetched from the server.

You can easily install such counters using the code provided by several websites like http://www.simplehitcounter.com/ , http://www.website-hit-counters.com/ etc. which give you an option to specify the design, font and initial value of the counter. However, if you like building code from scratch, you can design your own basic php counter.  All you need is that your website hosting server should support php scripts.

The logic behind this counter implementation can be explained in 3 steps.

  1. Read the current value of counter from a file on server side.
  2. Increment the current value and store it back in the same file
  3. Display this value using the formatting as you want.

Here's the php code for this counter:

<?php
$myFile = "resources/visitors_count.txt";
if(file_exists($myFile))
{
 $fh = fopen($myFile, 'r');
 $size = filesize($myFile);
 if($size>0)
 {
 $count = fread($fh, $size);
 }
 else {
 $count = 0;
 }
 fclose($fh);
 $fh = fopen($myFile, 'w') or die("can't open file");
 $count++;
 fwrite($fh, $count);
 fclose($fh);
}
else {
 $fh = fopen($myFile, 'w') or die("can't open file");
 $count=1;
 fwrite($fh, $count);
 fclose($fh);
}
$display_string = "You are visitor no. ". $count;
echo "<em> <span style='color:blue'> $display_string </span></em>";
?>

The point to be taken care of is that you must have the appropriate permissions to access the file containing counter value on server side. Change the permissions of that file to 644 or 755, if it's not already that.

Now, installing this counter in your website is simple. You just need to add this line <?php include "count.php" ?> to the place on your website where you want to display the count. Another thing to note is that this code will work only if the file where you are pasting this code has .php extension or another equivalent extension which makes the server realize that it is a php script. For example, I am using this counter to keep track of visitors on my website's main page. If you don't want to display the visitors count on your website but would like to keep track of visitors coming to your website, you can comment out the echo statement in the code above.

You can download this php script from here.

Flirting in a different sense …