Add PDO to Codeigniter using all of the regular PHP PDO functionality => TRUE PDO
Based off of Codeigniter 2.1-stable version
The files contained in the develop branch are based of Codeigniter 2.1-stable branch, in effect making this a 3rd party standalone.
Files different from Ellis Labs Codeigniter
- README.txt
- PDOschema.sql
- /application/config/config.php
- /application/config/database.php
- /application/libraries/Session.php
- /system/database/DB.php
For a complete step by step installation tutorial, visit
http://christopherickes.com/web-app-development/pdo-for-codeigniter-2/
Brief Synopsis
- Replace your current system/database/DB.php file
- Add application/libraries/Session.php file
- Edit application/config/database.php file
-
Set the following values
$active_record = FALSE; $PDO_conn = TRUE;
-
Fill the remaining values based on your unique configuration.
-
- If storing sessions in the database (recommended)
-
Create a session table as instructed by Codeigniter
CREATE TABLE IF NOT EXISTS `ci_sessions` ( session_id varchar(40) DEFAULT '0' NOT NULL, ip_address varchar(45) DEFAULT '0' NOT NULL, user_agent varchar(120) NOT NULL, last_activity int(10) unsigned DEFAULT 0 NOT NULL, user_data text NOT NULL, PRIMARY KEY (session_id), KEY `last_activity_idx` (`last_activity`) );
-
Edit application/config/config.php
$config['sess_use_database'] = TRUE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_cookie_Name'] = 'somethingwithoutunderscores' // or whatever you named your created table // If using encryption (recommended) $config['sess_encrypt_cookie'] = TRUE; $config['encryption_key'] = ''; // 32 upper & lower case plus numbers
-
-
Rename the application directory to new-app-dir
mv application *new-app-dir*
-
Create a public_html directory
mkdir public_html
-
Move index.php to the public_html directory
mv index.php public_html/
-
Change the server root directory to the public_html directory
This step is web server specific. Use Google, if needed.
HINT for Apache: Edit Virtual Host configuration. -
Update index.php
define('ENVIRONMENT', 'production'); $system_path = '/full/path/to/system_dir/system' $application_path = '/full/path/to/app_dir/*new-app-dir*'
6. Update *new-app-dir*/config/config.php
```php
$config['base_url'] = 'www.myappdomain.com';
# removing index.php requires rewrite rules
$config['index_page'] = '';
$config['global_xss_filtering'] = TRUE;
$config['csrf_token_name'] = 'sometokenname';
$config['csrf_cookie_name'] = 'somecookiename';
```
7. Update rewrite rules
ADVANCED: If possible, disable .htaccess and make updates in Virtual Host configurations
```linux
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^new-app-dir.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
```
8. Update *new-app-dir*/config/database.php
```php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
Update: As of v2.2.0, database credentials are moved to an external file. The external file allows credentials to be placed above the document root. It also makes it easier to remove database credenitals from version control. In all, it is more secure if executed correctly. Example: In $_SERVER['DOCUMENT_ROOT'].'/config/database.php'
<?php
// Database connection config
return array(
'db_master' => array(
'username' => 'user_matrix_0120',
'hostname' => 'localhost',
'password' => 'hKZ4!9f3<?MU)6!',
'database' => 'matrix_app',
),
);
- Update new-app-dir/config/autoload.php appropriately.