Block directory browsing in PHP

It is very important part for any web application. At one side, when we are taking care a lot to prevent our code to be copied, why we forget to implement Directory Browsing. At least you can save your client side scripts and images. By implementing this mechanism your application will deny to display js, jquery, stylesheet, image directories or even any directory of application.

Best thing about this mechanism is it’s implementation, it is very easy. Just one line of code, need to be changed or written. It is worth to write.

As much as I know, there are 2 ways you can implement this.

  1. Update apache server configuration file i.e. httpd.conf
  2. Update application’s .htaccess file

Update httpd.conf file:


[sourcecode language=”plain”]Options Indexes FollowSymLinks[/sourcecode]


[sourcecode language=”plain”]Options FollowSymLinks[/sourcecode]

Update .htaccess file:

Add below line:

[sourcecode language=”plain”]Options -Indexes[/sourcecode]

That’s all, You are done :)

Create a Custom config file for Codeigniter

By default, CodeIgniter has one primary config file, located at “application/config/config.php
What if we want to have a custom configuration file? What if we want to create custom config array with desired name?
Codeigniter allows a developer to create custom file but developer has to define values with $config array developer cannot define a custom variable or array inside this new file and use it


By doing this developer will get an error – “application/config/custom.php file does not appear to contain a valid configuration array.

Well to over come with this, I have created a customized message configuration file that will store all messages at one place. This article will help you to create your own custom config files.

At the end of this article downloadable packages are available for complete codeigniter v2.1.4 with customized files and only customized files.

Step 1: Create your config file.

I have created a file called message.php in “application/config/” directory. This file contains a config array named “message”. Define as much as data in $message array, that you would like to access.

$message[‘msg_cn_welcome’] = “Welcome to CodeIgniter!”;

Step 2: Create a Class

Create a Message Class in “/system/core/ “. This is going to be a core class as it will be auto loaded. Define a Method inside this class to perform a desire action. With respect to Message class, I have define a method calleditem. Item function will get the value from $message array and will return it to the caller function.
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
* CodeIgniter
class CI_Message { var $message = array();
var $is_loaded = array();function CI_Message()
$this->message =& get_message();
log_message(‘debug’, “Message Class Initialized”);

function item($item, $index = ”)
if ( ! isset($this->message[$item]))
return FALSE;

$pref = $this->message[$item];

if ( isset($index) OR is_array($index))
for($i=1; $i <= sizeof($index); $i++)
$pref = str_replace(‘%V’.$i,$index[$i-1],$pref);
$pref = str_replace(‘%V’,$index,$pref);
return $pref;

Step 3 : Create a reference function to load this new config file.

Now create a reference function get_message in “Common.php” file located at “/system/core/ “.
This function will be responsible to load message.php config file and read $message array and its values. Here is the code block.
* Loads the main message.php file
* Added By : Pinal Desai
* @access private
* @return array
function &get_message()
static $main_msg; if ( ! isset($main_msg))
if ( ! file_exists(APPPATH.’config/message’.EXT))
exit(‘The configuration file message’.EXT.’ does not exist.’);

if ( ! isset($message) OR ! is_array($message))
exit(‘Your Message file does not appear to be formatted correctly.’);

$main_msg[0] =& $message;
return $main_msg[0];

Step 4 : Load the Class

Now one thing is remaining, which is; to load a Message class. To load this customized class; add following code block to “CodeIgniter.php” file located at “/system/core/ “.

* ——————————————————
* Load the Message class
* ——————————————————

$EMSG =& load_class(‘Message’, ‘core’);

Step 5: Use the custom cofig file

Ready to use newly created config file and array. Following code will show the simple usage.

echo $this->message->item(‘msg_cn_welcome’);

Download DEMO Package:

Complete Codeigniter v2.1.4: Download Now
Only Customized Code and Files : Download Now

Article by: Pinal Desai