Mohammad Emran Hasan
Mohammad Emran Hasan

Follow

Mohammad Emran Hasan

Follow

Htpasswd protection library for Code Igniter

Mohammad Emran Hasan's photo
Mohammad Emran Hasan
·Jul 23, 2007·

2 min read

This is a library designed to run with Code Igniter that allows the application to protect any folder in the webserver using the htpasswd method of Apache. It can also group the users and create group based permission on the folders. The use is very simple: you need to define an array of the users (and array of groups if you need that), tell it where to store the password files and tell which folder to protect. It will do the rest for you. Here goes the function:

_/\*\*_  
 _\* Protect a given folder with htpasswd protection method_  
 _\*_  
 _\*_ **_@author_**  _Md Emran Hasan <emran@rightbrainsolution.com>_  
 _\*_ **_@param_**   _string  location of the folder to protect_  
 _\*_ **_@param_**   _string  location of the folder to write the group and password file_  
 _\*_ **_@param_**   _array   an array with the users info (username and password)_  
 _\*_ **_@param_**   _array   an array with the groups along with their user list_  
 _\*_ **_@return_**  _boolean_  
 _\*_ **_@access_**  _public_  
 _\*/_  
**function** protect($protectFolder, $passFolder, $userData, $groupData \= **array**())

It is a small part of the large protection library I have been developing for a membership management application called MemberSmart . This application allows someone to protect their PHP and non-PHP based files residing in a server in a number of ways. Among the methods, the htpasswd is the most easy and convenient one. This application will be released soon by the client I am working for.

Download the library with the example from htpasswd.zip. Here is an example controller highlighting the use of the library.

<?php

class Protect extends Controller {

**function** Protect()  
{  
    parent::Controller();  
}  

**function** index()  
{  
    _// Load the library_  
    $this\->load->library(‘AuthHtpasswd’);  

    _// Create the user array (this might come from your db)_  
    $userData \= **array**(  
                        **array**(  
                                ‘username’ \=> ‘admin’,  
                                ‘password’ \=> ‘root’  
                             ),  
                        **array**(  
                                ‘username’ \=> ‘test’,  
                                ‘password’ \=> ‘test123’  
                             ),  
                        **array**(  
                                ‘username’ \=> ’emran’,  
                                ‘password’ \=> ‘hasan’  
                             )  
                     );  

    _// Create the group array (this might also come from your db)_  
    $groupData \= **array**(  
                        **array**(  
                                ‘name’ \=> ‘ADMIN’,  
                                ‘users’ \=> **array**(‘admin’, ’emran’)  
                             ),  
                        **array**(  
                                ‘name’ \=> ‘QA’,  
                                ‘users’ \=> **array**(‘test’)  
                             )  
                     );  

    _// Define the root to the folders_  
    $rootDir \= $\_SERVER\[‘DOCUMENT\_ROOT’\] . DIRECTORY\_SEPARATOR;

    _// Protect them!_  
    $action \= $this\->authhtpasswd->protect($rootDir . ‘cs’,  
                                     $rootDir . ‘data’,  
                                     $userData,  
                                     $groupData  
                                    );  

    _// Is there any error? If yes, then show them!_  
    **if** (!$action)  
    {  
        $this\->authhtpasswd->printErrors();  
    }  
}  

}
?>

Md Emran Hasan
Co-founder & CTO
Right Brain Solution Ltd.

 
Share this