Wednesday, November 5, 2008

URL checker works using PHP 5 and CURL API for PHP. It checks for URL's existence in four scenarios.

URL can be available with a web site in it properly, URL can be available but the site is under Quarantine, URL available but the site has been moved or the virtual directory is not there or domain not available or the URL is working but some Server error occured on the site (HTTP Error Code 500)

First Version of the URL Checker used PHP Socket API for checking. Since PHP Socket uses DNS Resolving before making a Socket Connection, it takes large amount of time when it comes to large set of URL's. The second version uses CURL with Asynchronous Ajax calls to the server. Since the weight of an Ajax call is very little this script runs faster than the previous one. According to my observation it only take 10 to 15 seconds to check 600 URL's. Therefore CURL is preferred as a more rapid way to check URL existence.

Monday, September 1, 2008

Remote Debugging with xDebug


Debugging is an important fact in programming as it is used to find errors and malfunctions in programming. xDebug is a powerful tool to enable server side remote debugging. XDebug latest version can be downloaded from
http://www.xdebug.org/

Windows users can directly download the dll file and Linux users can download the source and compile.

By putting the following two lines in the php.ini will enable debugging

zend_extension_ts="/path_to_the_module/php_xdebug-2.0.3-5.2.5.so"
xdebug.remote_enable=1

Have fun!

Chain-of-Command pattern


Chain of Command handler is very important when it comes to loose coupling systems where change of a one module should not effect others if not necessary. As the name implies the Chain of Command pattern will accepts commands and arguments and is chained through the class until the command is executed. If the process can execute the command it will execute the command and stops. Like wise each handler class will decide whether it can handle the request or not.
interface ICommand
{
function executeCommand($command_name, $params);
}

class ChainOfCommands
{
private $mycommands = array();

public function addCommands($cmd)
{
$this->mycommands[]= $cmd;
}

public function runCommand($command_name, $params)
{
foreach( $this->mycommands as $cmd )
{
if ( $cmd->executeCommand($command_name, $params) )
return;
}
}
}
each handler class should implement the ICommand interface and override its onCommand method
class HelloCommand implements ICommand
{
public function executeCommand($command_name, $params)
{
//Check the command name
if ( $command_name!= 'Hello_Command' )
return ;

echo( "Inside Hello Command" );
// Put all the code to be executed and error handling
return true;
}
}
now create instances of ChainOfCommands class , add required commands and call the executeCommand method
$obj = new ChainOfCommands();
$obj->addCommand( new HelloCommand() );
$obj->runCommand( 'Hello_Command', "param 1" );

Implement Singleton pattern with PHP 5


We always create multiple instances of a class when we do programming but sometimes it is necessary to restrict people to create more than one instance of a class and this is where singleton comes into play.


Singleton classes use a static function to return its instance so that no matter how many object created in the class it uses the same object reference i.e all the instances share the same reference (because of the behavior of static)

ex -

public static function getObject()
{
if(!isset(self::$instance))
{
$object= __CLASS__;
self::$instance=new $object;
}

return self::$instance;
}

we can call this method MyClass::getObject(); if the class name is MyClass

Friday, August 22, 2008

Configure Memcached server with Linux


Memcached is a distributed caching tool developed by Danga. It can be used to speed up web by caching heavy objects in the memory and by reducing the database round trips. Memcached configuration is very straight forward.


It needs an additional component to be installed on your linux box, libevent. You can install libevent from source or from yum. Installing through yum is easy. All you need to type is yum install libevent. If you want to install libevent from the source follow these steps.

1) Download the latest version of libevent from their web site and unzip the tar ball(tar.gz) to a folder (ex-/home/your-username/

2) Go to the terminal and login as root (it is not necessary to login as root but on some distributions permission issues might arise. Better safe than sorry ;) ) by typing su or sudo depending on your distribution

3) Install libevent
# ./configure –prefix=/usr/local
# make install
When we configure libevent we tell the compiler to configure it to /usr/local where all the library files are kept

4) Download memcached from the site and unzip to a folder. Now we are ready to install memcached.
# ./configure –with-lib-event=/usr/local/
# make install
We tell the compiler to compile memcached with libevent which is located at usr/local directory. If all goes well memcached is ready to go

5) Type memcached on the terminal then you see the service is running. Or else type memcached -vv to rjavascript:void(0)un memcached in the very verbose mode which is quite helpful at the beginning to debug.

6) If you want to use memcached with php then you need to install PECL extension for PHP. This can be done by typing yum install php-pecl-memcached in the command line.

7) Have a php info page and you will see a section called Memcached if all goes well. :)