Jul
23

Htpasswd protection library for Code Igniter

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 and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Furl
  • Ma.gnolia
  • Technorati
  • YahooMyWeb
  • BlinkList
  • NewsVine
  • Reddit
  • Simpy
  • Spurl
Jun
09

Code Igniter Rocks !

Code Igniter

A few days back I discovered CakePHP and was very impressed with it. I started to code one of my apps with it but my impression decreased when i wanted to deploy it. After several methods and even communicating in their IRC, I wasnt able to deploy the app - total wastage of my time !!!

My search for a framework thats as easy (or easier ?) as Cake but having smooth deploy feature started again. I heard about Code Igniter before, but didnt give it much attention. Now I decided to visit their site. The one thing that forced me to give attention to it this time was this text on their site:

“If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks that require rocket science to understand, Code Igniter might just be the right tool for you.”

I decided to test it out and to my surprise, they are not lying a bit ! I was able to create a site for one client in super-cool way in a lot lesser time than it would actually take without it. While coding the app, I loved CI’s simplicity, its non-geeky style framework functions and their simple usages, the top-notch documentation and a good user base for knowledge searching.

Now I know which framework I should use for my projects. Certainly Cake is more robust framework than CI, but CI beats it by its simplicity. And I also love that !

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Furl
  • Ma.gnolia
  • Technorati
  • YahooMyWeb
  • BlinkList
  • NewsVine
  • Reddit
  • Simpy
  • Spurl
top