Accessing the CodeIgniter super object from external PHP script outside of codeigniter installation

I tried for so long but couldn't find a solution. For some reason, I need to access the super_ get_instance () codeigniter object from an external php script that is outside the codeigniter installation.

So, for example, I have my own php script called my_script.php inside public_html. And the codeigniter is set inside public_html / codeigniter.

The following discussion is here: http://ellislab.com/forums/viewthread/101620/ I created a file called external.php and put it in the public_html / codeigniter folder, it contains the following code:

<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
ob_start();
require('./new/index.php');
ob_end_clean();
?>

      

Then I created a file called my_script.php and put it in the public_html folder (outside of the codeigniter installation), it contains the following code:

<?php
require('new/external.php');
$ci =& get_instance();
echo $ci->somemodel->somemethod();
?>

      

Now when I load my_script.php file from browser it throws the following error:

The path to your system folder is not displayed correctly. Open the following file and fix this: index.php

If I put my_script.php file in codeigniter folder with corrected file path in require () function then it works. But I really need it to work from outside the codeigniter installation.

Any idea how to get rid of this problem?

Thanks in advance.

+2


source to share


2 answers


CI main index.php

sets paths to system and application folders. If you include index.php

from another directory, these paths are set relative to your include directory.

Try changing the following lines to index.php



$system_path = 'system';
// change to...
$system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';

// and...
$application_folder = 'application';
// change to...
$application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';

      

dirname(__FILE__)

will give you the absolute path index.php

, even if you include it somewhere else.

+2


source


The problem of loading codeigniter from an external file outside of the codeigniter installation was solved using the first answer, which I marked as accepted. And the problem with calling the default controller / method was solved using the constant (define). Here is the updated code for the external.php file:

<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
define("REQUEST", "external");
ob_start();
require('./new/index.php');
ob_end_clean();
?>

      

And here is the default controller method:



public function index($flag = NULL) 
 {
  if (constant("REQUEST") != "external")
  {
    // some code here
  }
 }

      

Many thanks to contributor hyubs. @hyubs

+1


source







All Articles