Category Archives: server scripting

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 ]
 elif [ ${#php5_version} -gt 0 ]
  echo "Error: Unexpected php version"
# 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.

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 , 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:

$myFile = "resources/visitors_count.txt";
 $fh = fopen($myFile, 'r');
 $size = filesize($myFile);
 $count = fread($fh, $size);
 else {
 $count = 0;
 $fh = fopen($myFile, 'w') or die("can't open file");
 fwrite($fh, $count);
else {
 $fh = fopen($myFile, 'w') or die("can't open file");
 fwrite($fh, $count);
$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.