Open the file in your localhost of development environment: /path/to/codeigniter/application/config/config.php and find the following lines:
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/.
Tip
Remember to begin with a http:// and always put the trailing / slash.
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:
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:
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:
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:
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:
0: No errors are written to logs as logging is deactivated
1: Error messages only (this will include PHP errors also)
2: Debugging messages only
3: Informational messages only
4: All types of messages
The following code line is the path to the folder in which you wish to save log files:
CodeIgniter will now respond and function according to the settings provided.
Managing CodeIgniter on different environments
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:
This is simply a case/switch statement with a SERVER_NAME check. The base_url value is set according to the server that the CodeIgniter application or project is running on.
Managing database settings on different environments
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 a case/switch test similar to that used previously, for example, the following code tests for a particular server and loads the appropriate settings:
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 standard database access options are shown in the following table:
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.
Securing the system files
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 the path/to/codeigniter/index.php file. Look for and find the following line:
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:
By moving the system folder out of the web root, you are protecting it against access via the Internet (as much as possible). The system folder is much more unlikely to be accessed in a location outside of the web root than inside.
Removing index.php from the address bar using .htaccess
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:
Once your .htaccess file is opened, write the following lines at the top of the file:
This rule in the .htaccess file will remove the index.php file from the browser's address bar. CodeIgniter's index.php file is still called but it is not shown to the user in the address bar of the browser.
Installing and using Sparks
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:
If your installation was successful, you should see something similar to:
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 to MY_Loader.php and move it to the application/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:
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 the example-spark (Version 1.0.0) that comes with the default installation, type in the command line:
If the installation was successful, you should see something similar to:
You should now be ready to begin making use of your Spark. Be sure to read the Readme file or documentation that is included with your Spark for its correct usage.