In this chapter, we will cover:
Downloading and installing CodeIgniter
Basic configuration options
Managing CodeIgniter on different environments
Managing database settings in different environments
Securing system files
Removing index.php from the address bar using .htaccess
Installing and using Sparks
CodeIgniter is an easy to use, easy to set up PHP-based framework which you can use to build pretty much any web-based application you can think of. There is a little bit of configuration needed before we can start to use CodeIgniter; however, this chapter will walk you through downloading, installing, and understanding the basic configuration of CodeIgniter to help you quickly get up and running.
First things first, you will need a copy of CodeIgniter to be getting on with. There are several choices: you can download a nightly build, an older version, or the current stable release. However, it's recommended that you go for the latest stable version.
You can get your hands on the latest stable version of CodeIgniter through the following link:
http://codeigniter.com/downloads/
CodeIgniter will be offered as a compressed archive file. Once CodeIgniter has been downloaded, copy the package to your web folder, and unpack it as you would normally unpack an archive on your system. Once you've done this, you'll need to set some configuration options, which we'll look at next.
Configuring CodeIgniter is a lot easier than many other web frameworks available and does not require you to resort to using the command line. All you need to quickly get up and running is access to several files in the application/config/
folder. These are a few of the suggested settings which will get your CodeIgniter installation ready without too much fuss.
Open the file in your localhost of development environment: /path/to/codeigniter/application/config/config.php
and find the following lines:
$config["base_url"]:
The value should be the full web address (the address that is written in your browser address bar) to the CodeIgniter installation. So if you are working in your localhost, the value should be: http://localhost/path/to/codeigniter/
.
If you've amended your host's file to use a domain name rather than localhost, then be sure to replace localhost with that domain name:
$config["encryption_key"]
If you wish to use either the Session
or Encryption
classes in your application, the encryption key must be set. The encryption key is simply a string of characters used by CodeIgniter to encrypt various types of communication:
$config["global_xss_filtering"]
The preceding code line specifies whether cross-site script filtering should be applied to the Get
, Post
, or Cookie
variables. For the sake of security, this should be set to TRUE
, especially in a live environment:
$config["csrf_protection"]
The preceding code line specifies whether a cookie token is set, which if TRUE
will be checked every time a form is submitted from the client side. In a live environment, it should be set to TRUE
:
$config["log_threshold"]
The preceding code line specifies whether you want to write to logs, and if so, the type of information you wish to write to those logs. For example:
The following code line is the path to the folder in which you wish to save log files:
$config["log_path"] = "/path/to/log/file"
In some cases, it may be useful to adapt your configuration files so that they can function on several servers or environments without having to edit or maintain each time they are moved. For example, the configuration settings you may have on your localhost are very likely to be different than those on a live or production server. Setting the configuration files correctly will save you a lot of time rather than manually switching between the two.
Open the /path/to/codeigniter/application/config/config.php
file and replace the $config["base_url"]
line with the following:
switch($_SERVER["SERVER_NAME"]) { case "localhost": $config["base_url"] = "http://localhost/path/to/codeigniter/"; break; case "mydomain.com": $config["base_url"] = "http://www.mydomain.com/"; break; }
If you plan to use a database for your CodeIgniter application, then you'll need to maintain the correct connection settings. CodeIgniter keeps these settings in the database.php
config file.
Open the
/path/to/codeigniter/application/config/database.php
file. Chances are that the only values that need to change are the standard hostname, username, password of your database server, and the database name.Find the line that defines
$active_group
, which specifies the specific database settings to use for a particular hosting environment. You can switch between settings by acase
/switch
test similar to that used previously, for example, the following code tests for a particular server and loads the appropriate settings:switch($_SERVER["SERVER_NAME"]) { case "localhost": $active_group = "testing"; break; case "mydomain.com": $active_group = "default" break; } $db["default"]["hostname"] = "localhost"; $db["default"]["username"] = "root"; $db["default"]["password"] = ""; $db["default"]["database"] = "database_name"; $db["default"]["dbdriver"] = "mysql"; $db["default"]["dbprefix"] = ""; $db["default"]["pconnect"] = TRUE; $db["default"]["db_debug"] = FALSE; $db["default"]["cache_on"] = FALSE; $db["default"]["cachedir"] = ""; $db["default"]["char_set"] = "utf8"; $db["default"]["dbcollat"] = "utf8_general_ci"; $db["default"]["swap_pre"] = ""; $db["default"]["autoinit"] = TRUE; $db["default"]["stricton"] = FALSE; $db["testing"]["hostname"] = "localhost"; $db["testing"]["username"] = "root"; $db["testing"]["password"] = ""; $db["testing"]["database"] = "database_name"; $db["testing"]["dbdriver"] = "mysql"; $db["testing"]["dbprefix"] = ""; $db["testing"]["pconnect"] = TRUE; $db["testing"]["db_debug"] = TRUE; $db["testing"]["cache_on"] = FALSE; $db["testing"]["cachedir"] = ""; $db["testing"]["char_set"] = "utf8"; $db["testing"]["dbcollat"] = "utf8_general_ci"; $db["testing"]["swap_pre"] = ""; $db["testing"]["autoinit"] = TRUE; $db["testing"]["stricton"] = FALSE; $active_record – Specifies if you require active record support. By default it is set to TRUE.
All we're doing is defining the environment that the site is running on. In the preceding example, we specify two environments: either default
or testing
, and apply specific settings for them. So, let's look at some variable definitions.
The following table shows the options that normally remain unchanged from the default setting but are here just incase you wish to change them:
We will look at accessing data from a database in more detail in Chapter 6, Working with Databases.
On live environments, it is strongly recommended that you move your system folder out of the web root to prevent malicious access.
Move the system folder either by the command line or using your computer's GUI to a folder outside the publicly accessible web folder. The method to do this will be different depending on which system you are using, but I'm sure you know how to move a folder, so we will not discuss that here.
After you have moved the system folder, you will need to update the
$system_path
variable in thepath/to/codeigniter/index.php
file. Look for and find the following line:$system_path = "path/to/system/folder";
Amend the line to reflect the new location of the system folder. So if, for example, you moved the system folder up one level out of the web root, you should write the following line:
$system_path = "../system";
It is possible to remove the index.php
file from the web browser address bar when CodeIgniter is running.
Create or open a .htaccess
file. If a .htaccess
file does not already exist, you can create one as follows:
Linux/Mac
Open a terminal window and type:
touch/path/to/CodeIgniter/.htaccess
.
Windows
Create a text file in your CodeIgniter root, naming it
file.htaccess
.Press Windows + R to open the run dialogue.
Enter the following command and click on OK:
ren "C:\path\to\CodeIgniter\file.htaccess" .htaccess
Once your
.htaccess
file is opened, write the following lines at the top of the file:<IfModule mod_rewrite.c> RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
It has been the case for a long time now that to find and use extensions, libraries, and other useful snippets of code for CodeIgniter, you have to search the Internet and download code from various places such as blogs, code repositories, and so on. Useful installations for CodeIgniter were spread across the Internet and as such, may have been hard to locate. Sparks acts as a single point of reference for extensions for CodeIgniter. It's simple to install and use, and contains thousands of useful add-ons for CodeIgniter.
If you are using a MAC or Linux, then the command line interface is open to you.
Using the terminal application on your system, navigate to the root of your CodeIgniter application and enter the following line:
php -r "$(curl -fsSL http://getsparks.org/go-sparks)"
If your installation was successful, you should see something similar to:
user@server:/path/to/codeigniter$ php -r "$(curl -fsSL http://getsparks.org/go-sparks)" Pulling down spark manager from http://getsparks.org/static/install/spark-manager-0.0.9.zip ... Pulling down Loader class core extension from http://getsparks.org/static/install/MY_Loader.php.txt ... Extracting zip package ... Cleaning up ... Spark Manager has been installed successfully! Try: `php tools/spark help`
If you are using Windows, then you will need to download Sparks and unpack it manually. To do that, perform the following instructions or check out the instructions on the GetSparks website for the latest version:
Create a folder named
tools
in the top level (root) of your CodeIgniter directory.Visit the following URL: http://getsparks.org/install.
Go to the Normal Installation section and download the Sparks package.
Unpack the download into the
tools
folder you created in step 1.Download the
Loader
class extension from: http://getsparks.org/static/install/MY_Loader.php.txt.Rename the
MY_Loader.php.txt
file toMY_Loader.php
and move it to theapplication/core/MY_Loader.php
directory in your CodeIgniter instance.Now that Sparks is installed in your CodeIgniter instance, you can begin to install extensions and packages.
To install a package from Sparks, type the following in the command line:
php tools/spark install [Package Version] Spark Name
Here,
Package Version
is the specific version of the Spark you wish to install. You are not required to state the version, and by leaving it out, Sparks will download the latest version by default.Spark Name
is the name of the Spark you wish to install, so for example, to install theexample-spark
(Version 1.0.0) that comes with the default installation, type in the command line:php tools/spark install -v1.0.0 example-spark
If the installation was successful, you should see something similar to:
user@server:/path/to/codeigniter$ php tools/spark install -v1.0.0 example-spark [ SPARK ] Retrieving spark detail from getsparks.org [ SPARK ] From Downtown! Retrieving spark from Mercurial repository at https://url/of/the/spark/repo [ SPARK ] Spark installed to ./sparks/example-spark/1.0.0 - You're on fire!