Monday, August 20, 2012

Codeigniter Initial Configuration


In this section, you learn the ins and outs of the files inside the application/config/ folder. Four of them are important for your project and they are :
  •  config.php
  •  database.php 
  • autoload.php 
  •  routes.php  

 Config.php

 The config.php file contains a series of configuration options (all of them stored in a PHP array called,appropriately enough, $config ) that CodeIgniter uses to keep track of your application ’ s information and settings. The first configuration option you need to set inside config.php is the base URL of your application. You do that by setting the absolute URL (including the http:// part) for $config[ ‘ base_url ’ ] , like so:

$config[‘base_url’] = “http://www.example.com/test/”;

 Once you ’ ve set this configuration option, you can recall it whenever you want using the CodeIgniter base_url() function, which can be a very handy thing to know. This one feature keeps you from having to  rewrite hard - coded URLs in your application, when you migrate from development to test or from test to production.
The second thing you need to do is set a value for your home page by editing the $config[ ‘ index_page ’ ] configuration option. CodeIgniter ships with a value of “ index.php ” for this option, which means that index.php will appear in all of your URLs. Many CodeIgniter developers prefer to keep this value blank, like so:

$config[‘index_page’] = ‘’;


To make this work, you need to include an .htaccess file to the CodeIgniter root directory, After you ’ ve set this option value, there ’ s very little to do. For now, leave all the other values at their default settings:


$config[‘uri_protocol’] = “AUTO”;                            
$config[‘url_suffix’] = “”;                                           
$config[‘language’] = “english”;                                
$config[‘charset’] = “UTF-8”;                                    
$config[‘enable_hooks’] = FALSE;
$config[‘subclass_prefix’] = ‘MY_’;
$config[‘permitted_uri_chars’] = ‘a-z 0-9~%.:_-’;     
$config[‘enable_query_strings’] = FALSE;
$config[‘controller_trigger’] = ‘c’;
$config[‘function_trigger’] = ‘m’;
$config[‘log_threshold’] = 0;                                       
$config[‘log_path’] = ‘’;                                              
$config[‘log_date_format’] = ‘Y-m-d H:i:s’;                

$config[‘cache_path’] = ‘’;                                            
$config[‘encryption_key’] = “enter_a_32_character_string_here”;
$config[‘sess_cookie_name’] = ‘ci_session’;
$config[‘sess_expiration’] = 7200;
$config[‘sess_encrypt_cookie’] = TRUE;
$config[‘sess_use_database’] = FALSE;
$config[‘sess_table_name’] = ‘ci_sessions’;
$config[‘sess_match_ip’] = FALSE;
$config[‘sess_match_useragent’] = TRUE;
$config[‘cookie_prefix’] = “”;
$config[‘cookie_domain’] = “”;
$config[‘cookie_path’] = “/”;
$config[‘global_xss_filtering’] = TRUE;
$config[‘compress_output’] = FALSE;
$config[‘time_reference’] = ‘local’;
$config[‘rewrite_short_tags’] = FALSE

 For more details on each of these configuration options, simply read the comments embedded in /system/application/config/config.php. You will also get more detail on certain settings as you work through the sections of the book and tweak the configuration as needed. For example, at some point, you will want to use encryption for security purposes or set your logging threshold for debugging, and they both require making changes to this file.


 database.php

 The database.php file contains all the information required to connect to a database. Currently, CodeIgniter supports mysql, mysqli, postgres, odbc, and mssql connections. To connect to your database, simply enter valid information for your hostname, username, password, database name, and database driver. Each of these is stored in the $db array under the “ default ” group, which means you could have numerous connection groups, each with their own unique name. For example, you could have one set of connection variables for your development environment and another for your production environment. As long as you ’ ve set the $active_group variable correctly, your application will keep connected.
$active_record = TRUE;
$active_group = “default”;
$db[‘default’][‘hostname’] = “localhost”;
$db[‘default’][‘username’] = “db_username”;
$db[‘default’][‘password’] = “db_password”;
$db[‘default’][‘database’] = “db_name”;
$db[‘default’][‘dbdriver’] = “mysql”;
$db[‘default’][‘dbprefix’] = “”;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = “”;
$db[‘default’][‘char_set’] = “utf8”;
$db[‘default’][‘dbcollat’] = “utf8_general_ci”;

autoload.php

The autoload.php file specifies which systems are automatically loaded by CodeIgniter. There ’ s one school of thought that maintains that the autoloader should be kept as minimal as possible to keep the framework lightweight and nimble, but another school of thought maintains that certain frequently used systems should be autoloaded instead of called at the local level repeatedly. You ’ ll find your own balance as experience and local project needs warrant, but for now, set your options like this:

$autoload[‘libraries’] = array(‘database’,’session’,’email’,’validation’);
$autoload[‘helper’] = array(‘url’,’form’,’text’,’date’,’security’);
$autoload[‘plugin’] = array(‘captcha’);
$autoload[‘model’] = array();
$autoload[‘config’] = array();
The first $autoload option, libraries , is a list of libraries that should be loaded. You ’ re going to be using the database, session, e - mail, and form validation libraries a lot in this (and many other) CodeIgniter project(s), thus loading them now makes a good deal of sense. You learn more about these libraries later. The second $autoload option, helper , is a list of helper collections that you will need to get work done. Almost every CodeIgniter project uses the URL, form, and text helpers frequently; it ’ s not a bad idea to have date and security autoloaded as well, particularly as you ’ ll need them to help you parse dates and handle security features. For the time being, keep the third $autoload option, plugin , either blank or filled in with the CAPTCHA plugin, as you may have a need to use CAPTCHA devices in forms for your application for extra security. The fourth $autoload option, model , allows you to autoload models. This is a new feature ofCodeIgniter 1.6. You are going to create two basic models for Claudia ’ s Kids below, one that handles product data and the other that handles category data. When you create those two models, you ’ ll have the opportunity to come back to this file and autoload them. Finally, the fifth $autoload option, config , is where you would list any custom configuration files. As it ’ s unlikely that you ’ ve created any of those at this point, leave it blank.


routes.php 

The routes.php file lets you remap URI requests to specific controller functions. For example, you may have  a controller named site with a function named index . The URI for this controller/function combination might be :
http://www.example.com/site/index
Furthermore, if your site controller had a pages function that accepted a numeric ID for database lookup, the URI might look like this:
http://www.example.com/site/pages/4
In some cases, you might want to remap one or more of these default routes. For example, the second
example might be better displayed as this:
http://www.example.com/about_us/
In that case, your routes.php file would contain a rule like this:
$route[‘about_us’] = “site/pages/4”;
For right now, though, this kind of manipulation falls under “ advanced usage, ” so don ’ t worry too much
about it. However, please do note that this kind of thing is possible. Also, be aware that two “ reserved
routes ” exist: default_controller and scaffolding_trigger.
$route[‘default_controller’] = “welcome”;
The default_controller route tells CodeIgniter which controller should be loaded if no controller is
identified. For simplicity ’ s sake, keep this setting at welcome, because you ’ re going to create a welcome
controller for the project associated with this book.


For more information visit the official website of codeigniter see the userguide.
 

Sunday, August 19, 2012

Codeigniter Directory Structure

Now that you have CodeIgniter downloaded and unzipped, take a minute to look at the file structure. Below illustrates the initial folder structure you ’ ll see.

You can see three folders and two files in the codeigniter folder. The three folders are application, system and Codeigniter user guide and the files are index.php and license.txt.Below is the detailed description about the directories.

The application Folder— The application folder contains the application you ’ re building. Basically, this
folder contains your models, views, controllers, and other code (like helpers and class
extensions). In other words, this folder is where you will work for the project development.

cache — The cache folder contains all cached pages for your application. In Chapter 9 , you learn
more about caching and how to turn your super - speedy development application into a
blazingly fast live application.
Config— The Know more about config folderfolder is the area where you set the configuration for your application.
Controllers— In this folder you will place your class files developed for your application
core— In this folder you will place your base class files of your application
errors— In this folder you will place your application specific error logs
helpers— In this folder you will place the include files use full for your application
hooks— In this folder you will place the support files use full for your application
language— In this folder you will place language  macros/ define constants.
libraries— In this folder you will place your own developed libraries useful for your application
logs — The logs folder is the folder CodeIgniter uses to write error and other logs to.
models— In this folder you will place your data base fetching logic in
thirdparty— In this folder you can place any plugins used for your application
views— Most of your work will be in this folder, you will place your html template files. 

The system/ Folder
The system/ folder is where all the action happens. This folder contains all the CodeIgniter code of
consequence, organized into various folders:

core — The core folder is where CodeIgniter ’ s core classes live. You have almost no
reason to go in here. All of your work will occur in the application folder. Even if your intent is
to extend the CodeIgniter core, you would do it with hooks, and hooks live in the application
folder.
database — The database folder contains core database drivers and other database utilities. Again,
there ’ s no good reason for you to be in this folder.
fonts — The fonts folder contains font - related information and utilities. Again, there ’ s no reason
to spend any time here.
helpers — The helpers folder contains standard CodeIgniter helpers (such as date, cookie, and
URL helpers). You ’ ll make frequent use of helpers in your CodeIgniter career and can even
extend helpers thanks to improvements introduced in CodeIgniter version 1.6.
language — The language folder contains language files. You can ignore it for now.
libraries — The libraries folder contains standard CodeIgniter libraries (to help you with e - mail,
calendars, file uploads, and more). You can create your own libraries or extend (and even
replace) standard ones, but those will be saved in the application/libraries directory to keep
them separate from the standard CodeIgniter libraries saved in this particular folder.

For more info visit codeigniter site at www.codeigniter.com

Thursday, August 16, 2012

Downloading CodeIgniter

You can download the latest version of CodeIgniter by going to www.codeigniter.com and clicking the Download CodeIgniter button on the home page as shown in the below figure.



When you click the button, you ' ll get a dialog for downloading a ZIP archive. Either open it in your unzip utility or save it to your hard drive and then open it.

Sunday, August 12, 2012

URI routing in Codeigniter

The routes.php file lets you remap URI requests to specific controller functions. For example, you may
have a controller named site with a function named index . The URI for this controller/function
combination might be :

http://www.example.com/site/index

Furthermore, if your site controller had a pages function that accepted a numeric ID for database
lookup, the URI might look like this:

http://www.example.com/site/pages/4

In some cases, you might want to remap one or more of these default routes. For example, the second
example might be better displayed as this:

http://www.example.com/about_us/

In that case, your routes.php file would contain a rule like this:

$route[‘about_us’] = “site/pages/4”;

For right now, though, this kind of manipulation falls under “ advanced usage, ” so don ’ t worry too much
about it. However, please do note that this kind of thing is possible. Also, be aware that two “ reserved
routes ” exist: default_controller and scaffolding_trigger.

$route[‘default_controller’] = “welcome”;

The default_controller route tells CodeIgniter which controller should be loaded if no controller is
identified. For simplicity ’ s sake, keep this setting at welcome, because you ’ re going to create a welcome
controller for the project associated with this book.

To know more about URI routing Please visit the below link
http://codeigniter.com/user_guide/general/routing.html

Sunday, November 6, 2011

What is the difference between urlencode and urldecode ?

The difference between urlencode and urldecode is
URLencode is used to encode a string to be passed through URL to a web page. URLencode replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.

Use this link to get more stuff on Urlencode http://www.w3schools.com/tags/ref_urlencode.asp


URLdecode is used to decode the encoded URL string . Decodes any %## encoding in the given string

What is the difference between array_slice() and array_splice()?

The difference between array_slice() and array_splice()

array_splice() is used to remove some elements from an array and replace with specified elements.
Below are some code examples:
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>

 array_slice() is used to extract a slice of the array. Below are some example codes:

<?php
$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
The above example will output:


Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)






What is the difference between unlink and unset ?

The difference between unlink and unset is given below:

Unlink is used to delete a file for a given path.
where as
Unset is used to delete a value of a variable.