Permission denied: /home/username/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable

I usually dump any html doc or any website in my public_html folder.  For example I have ExtJS dumped in public_html/extjs so I can see the docs in http://localhost/~shiplu/extjs/

For some days when I tried to access that url I was getting

[Thu Nov 07 21:50:39 2013] [crit] [client 127.0.0.1] (13)Permission denied: /home/shiplu/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable

Replace ‘shiplu’ with your username to mach an error for your own.  I have .htaccess file in public_html folder. I was wondering why apache was looking for it inside my home directory?

Then I discovered that I have 700 mode on my home directory (/home/shiplu). So apache maps the path /extjs to /home/shiplu/public_html/extjs and look for .htaccess file on every folder in the path way.  While looking for /home/.htaccess it found the file doesn’t exists. In the next step while reading /home/shiplu/.htaccess,  it couldn’t even determine files existence  due to lack of permission.

The easy solve was to give read access to my home directory.  But I want to give read access only to apache. Not to anything else.  So I add apache in my group and give my group users to read access it. Why my group? because home directory of a user is usually owned by the users own group and the user himself.

To find your group name run `id` in the console.


$ id
uid=1000(shiplu) gid=1000(shiplu) groups=1000(shiplu),4(adm),20(dialout),24(cdrom),46(plugdev),105(lpadmin),112(netdev),115(admin),116(sambashare),129(scanner),143(kvm),144(libvirtd)

Here gid indicates my group name which is shiplu.  Also to make sure you own your home directory  run ls /home -l


total 20
drwxr-xr-x  50 hacker  hacker   4096 Nov  7 20:20 hacker
drwxr-x--- 183 shiplu  shiplu  16384 Nov  7 21:55 shiplu

See the two ‘shiplu’? The second one is the group name that owns the /home/shiplu directory.

So give the owner group of /home/shiplu read+execute access.


# chmod  750 /home/shiplu

 

And assign apache user to the shiplu group.

usermod -a -G shiplu www-data

www-data is the username of apache user. Now restart apache. and it’ll be able to access.

Javascript timed array iteration

Sometimes you need to iterate on an array with a time delay between each iteration. For example, you have a list of UI elements. And you want to perform animation or process each components in a way to make it visible to user. If you dont use any delay there will be no UI feedback. User will think everything happened at once. Its better to provide an UI feedback on the work in progress.

Timed array iteration means you use a delay on each iteration. JavaScript has no sleep method. So its not possible to add delay like other languages. I used `window.setTimeout ` to achieve this. This is only available on browser based JavaScript. In server side JavaScript like nodejs, v8, rhino its not available. Here is the function.

function arrayWalkTimed(arr, dur, func){
    var p = arr[0]? arr[0]: null;
    if(p){
        func(p);
        window.setTimeout(function(){
            timed_array_walk(arr.slice(1), dur, func);
        }, dur);
    }
}

Check if a php method is called statically or as an instance method

PHP allows to call you any method of a class both as static and as instance method. Recent version of PHP is more strict though. But still you can call non-static function as static. Calling a non-static function that uses $this statically will cause a fatal error. You’d  want to catch this and throw exception with meaningful message.

If you can determine how this method is called whether statically or as instance method you can handle this situation.

See this example class.

class C {
    private $value = "value";
    public function method () { 
        echo __METHOD__. "\n"; 
        echo "Value:". $this->value. "\n"; 
    }
}

This method will raise a fatal error you if call it as C::method().

To determine if its this is called statically or as an instance method there are two techniques.

  1. Using debug_backtrace
    1. class A { 
       public function m(){ 
       $bt = debug_backtrace(); 
       if($bt[0]['type']=='::')
       throw new Exception(__METHOD__." is called statically");
       }
      }
      
      
  2. Using isset on $this.
    1. class A { 
       public function m(){ 
       if(!isset($this))
       throw new Exception(__METHOD__." is not called from class instance");
       }
      }

 

The last one is shorter. But first one is more explicit.