PHP : check whether script running from cli or with apache

June 13th, 2015 § 0 comments § permalink

Recently I was in need of some way to identity the script is running from command line or using apache server. Then I came to know about php_sapi_name() method.

The php_sapi_name method will return the type of interface. for command line it will return cli.

If the script is running with apache, it will return different values according to its Server API. when I tried to run my script with apache it returned apache2handler.

Yii : route config for subdomains

April 29th, 2015 § 0 comments § permalink

Recently I was setting up a subdomain for a existing project, I needed to configure a different root route for the it. First I tried to do it with htaccess but I failed. Then I found a solution after some googling.

// protected/config/main.php
    'urlManager' => array(
      'urlFormat' => 'path',
      'showScriptName' => false,
      'caseSensitive' => true,
      'rules' => array(
        'home' => 'site/index',
        '' => 'about/index',

This will result in two different root route for and I you know a better solution please let me know.

PHP : Add element in the beginning of an array

August 6th, 2013 § 0 comments § permalink

We can add an element in the beginning of an array using array_unshift method. Its quite easy and handy, But did you know that array_unshift will reindex you array? Thats what I like to discuss over this blog post.

Lets take the above php array as sample. I intentionally missed the indexes 3 and 4 to show array_unshift method will reindex your array.

See the Fifth is moved to 3rd index? This will happen only when the indexes are numeric.

So the easy way to append an element in the beginning of an array without reindexing is using “+” operator on array.

Now the “Fifth” element is still on index 5.
Hope you will be careful when using array_unshift next time.

How to execute codeigniter migration

March 7th, 2012 § 0 comments § permalink

The Official Codeigniter userguide have a well written document about how to write a migration in codeigniter. But It missed  one, “How run/execute the migration?”. I am not sure whether I missed or codeigniter did. But at last I found my own way to execute the migration and it works fine for me. I am not sure whether this is right way.

I create a controller named “migration” and added a index action to it. The index action will accept a parameter, version number.

Then I invoke the migration via browser.

For Eg: If I need to migrate to version number 10,
I use http://localhost/index.php/migrate/index/10 in browser.

I appreciate, if you can share a better way to execute migration in codeigniter.
Thank you.

Install phpunit on ubuntu

February 21st, 2012 § 0 comments § permalink

A few months before I wrote an article on how to install phpunit on windows. Now when I tried to install phpunit on ubuntu I faced an error phpunit/PHPUnit requires PEAR Installer (version >= 1.9.4), installed version is 1.9.2 .

Here is the steps which I used to get rid of this error.

  1. Install php-pear
     sudo apt-get install php-pear
  2. Update its own channel
     sudo pear channel-update
  3. Upgrade pear
    sudo pear upgrade-all
  4.  Now install PHPunit
    sudo pear install –alldeps 
Now we can check whether installation is success by typing phpunit in terminal.

Yes. It worked.

Happy Testing.

PHP : How to avoid isset on array index

February 3rd, 2012 § 0 comments § permalink

I think most of you are fed up with checking each and every array index with isset function. Here is the simple way to avoid isset on array index. When I was a PHP newbie, I used to write code like this to avoid Notice : undefined index “blahblah”

In this case, we are helpless to predict which all index will be missing but we can use a better way which we can make sure that no array indexes will be missing.

Now we can assure that in $data all the index are present. So we can avoid isset checking.

Happy coding.

PHP : Check if extension is loaded

January 18th, 2012 § 0 comments § permalink

           The simplest way to check whether the php extension is loaded or not, is using extension_loaded function. This function will return TRUE if it is loaded else FALSE.

Symfony – configure database using command

September 30th, 2011 § 0 comments § permalink

Symfony command line helps us a lot to do the task easier and faster. One of the best example is configuring database in symfony. There are two ways you can configure database.

First method is give direct entry to database.yml. But I find the command line  as  easy and quick way to configure database in symfonny 1.4.

You can even easily specify the environment in which the database configuration belongs to. The command will write the the database configuration into database.yml.

Symfony – using isset on sfOutputEscaperObjectDecorator

August 18th, 2011 § 0 comments § permalink

Have you tried isset function on doctine object from  template? Something like this?

   If you tried this you will know that isset will return only false. Because the Doctrine Object is wrapped with sfOutputEscaperObjectDecorator. So if you like to check the presence of a property then you should use something like this.

Happy coding

Send sms from php using way2sms updated

July 16th, 2011 § 14 comments § permalink

Update 2012-01-17 : I just updated the code to send sms. Now you guys don’t worry abt the action param, since the class itself handle it for everyone. I recommend you to disable the warnings, else you will receive lots due to imperfect html provided by way2sms.


Update 2012-01-16 : If you are still not receiving SMS while using the updated code from github, you should probably need to do one more change in code. As I mention in this blog the the parameter Action=abfghst5654g is unique for each user. So you need to find the value of action for your login. You can easily find it by following these steps

  1. Go to and login with your credentials.
  2. GoTo send SMS page.
  3. Inspect webpage with firebug(Firefox) or Developer tools (Chrome).

I will update here, once I update the code to find the value of action for each user.

Thanks Anooj, for notifying this.


1 year before I updated the Ashwin anand’s php code for sending sms from php. later many of them reported that my code too is not work. So now i again updating it and hosting it in Github. You are free to review, fork and edit it. You can download it from github repo.

The change I made from older version is

in classSms.php, line no. 94.
There I updated Action=abfghst5654g after checking in with firebug.Its a hidden field in the home page.
Happy Messaging.