Skip to content
Derek Jones edited this page Jul 5, 2012 · 13 revisions

Category:Core | Category:Core::Integration

Learn how to use CI code in other PHP applications.

Warning

Please note that this mechanism may lead to unstable software. If you want a forum or a wiki for a production environment, have a look at Expression Engine. Take a look at the related forum thread.

Purpose

CI allows us to create custom libraries easily. This way we can use some smart classes in our CI applications. Now let's use CI in other PHP applications. We will call 'external app' the application in which you want to use some CI code. This is useful if you want to integrate a forum in your website, and use your authentication process. I'm sure you'll find other ideas.

Installation

No file to download here, you'll have to change the files manually.

hacked ci files

In your system directory (or whatever, if somebody has a better idea...), create cidip directory. Copy index.php to cidip/cidip_index.php At the top of the new file, add those lines:

// WARNING: This is not an original CI file
// those first lines are a hack to Code Igniter

// backuping query string
$query_string = urlencode($_SERVER['QUERY_STRING']);
// destroying the $_GET array (but reconstructed when CI leaves)
$_GET = array();
// let's use CI a bit to recover our $_GET array
$_SERVER['PATH_INFO'] = '/cidip/recover_get_array/'.$query_string;

Change this line

$system_folder = "..";

One could comment out ```php error_reporting(E_ALL);


Change the last line to ```php
require_once BASEPATH.'cidip/cidip_CodeIgniter'.EXT;

Copy codeigniter/CodeIgniter.php to cidip/cidip_CodeIgniter.php In section Send the final rendered output to the browser (around line 238), comment out ```php $OUT->_display();


#### ci_dip controller

Add the CI_dip controller to your controllers directory. It will basically reconstruct $_GET or $_REQUEST arrays. The controller's file name should be "cidip.php"
```php
<?php
class CIdip extends Controller {

    function CIdip()
    {
        parent::Controller();
    }

    function recover_get_array($query_string = '')
    {
        $query_string = urldecode($query_string);
        
        $_SERVER['QUERY_STRING'] = $query_string;
        $get_array = array();
        parse_str($query_string,$get_array);
        foreach($get_array as $key => $val) {
            $_GET[$key] = $this->input->xss_clean($val);
            $_REQUEST[$key] = $this->input->xss_clean($val);
        }
    }
}
?>

Usage

Simply 'require' cidip_index.php where you need CI in your external app. You may have to do that with an absolute path, to avoid bad surprises. You can use a constant that your external app defines to point to the base path of the app, then navigates with ../ to CI's directory.

POTENTIAL ISSUES

Well I'm not good enough to be sure of what happens here. If you know, please contribute. The main issue is variables or functions conflicts. You'll have to search for conflicting global vars ($OBJ, $CI, $global, $OUT, $BM, $CFG). If error_reporting is set correctly, PHP will complain about conflicting functions, so there is no problem here.

POTENTIAL BENEFITS

  • CI code available !
  • CI log process receives PHP messages generated by the external app too.

EXAMPLES

FreakAuth integration

Some applications that work with a database of members and authentication can now be integrated to your CI installation. It's even more easy if their authentication process is modular, like in Vanilla or DokuWiki (I'll upload the auth classes later). I personnaly disabled the login process of the external app (in the config file or removing it from the template...) and use only FreakAuth login.

[u]THE[/u] little trick

Most of the external apps you will integrate uses the auth system very early, so you will often have CI functions available everywhere in the external code (useful in templates to integrate our header or footer views with ```php <?php $obj =& get_instance();echo $obj->load->view('footer',null,true); ?>


NOTE: Short tags are not processed by CI here.
Clone this wiki locally