views:

708

answers:

4

What would be the easiest way to include a CI file? Let's say I want to include http://example.com/ci/index.php/mycontroller/ on example.com

example.com doesn't run CI and I can't do include('ci/index.php/mycontroller').

A: 

Assuming you have PHP configured to allow url includes, just...

include('http://example.com/ci/index.php/mycontroller/');

(Requires PHP 4.3.0+)

Amber
I tried it, but it didn't work. Guess I have to e-mail my hosting provider.If I include like this, will the page CI be able to read cookies or sessions - does it have full functionality as if I loaded it directly from my browser?
Indrek
Should be noted that this is quite the potential security risk.
Tim Lytle
Oh, checked phpinfo()... allow_url_fopen is on, but for some reason it doesn't load.Is it a security risk if I hardcode the URL into include?
Indrek
Note that including the page is not as if the user browsed to it, but as if your server browsed to the page. Thus if your user has a session, CI won't be able to read that when you're `include()` ing because the session isn't for the server, it's for the user. If you need to include it in such a way that it uses the user's session, you're probably going to have to use an `<iframe>` and include it client-side.
Amber
Ok, so loading the content throught iframe/javascript should be the safest and easiest...
Indrek
A: 

Depending on what you want to do the output, you should be able to set the REQUEST_URI or whatever CI uses for determining the request and then just include the bootstrap file...

EDIT: Actually, I think it would be better to go the other way around, if that's possible (again, that depends on what exactly you want to do - so please tell us). What I mean is create a CodeIgniter controller that simply includes your script...

Franz
http://example.com/index.php is a fully functional index page. I want to add a small snippet to it that I wrote in CI. The problem is, that I can't include it direclty, as CI URLs are dynamic - usr/local/public_html/.../ci/index.php/mycontroller doesn't exist as a file..
Indrek
Well, if you access the controller file directly, CodeIgniter will give you the file... at least with the default rewrite rules...
Franz
Will it give me full functionality?I probably am not expressing clearly enough, but I want the output of the view file that the controller loads.Mycontroller extends Controller{function test(){$this->load->view('test_view');}}This would be located at http://example.com/ci/index.php/mycontroller/test - but I can't include it directly like this: include('ci/index.php/mycontroller/test');
Indrek
You could create the view object from outside of CodeIgniter...
Franz
I still don't follow you :(
Indrek
Rethinking it, the best way is to include your script from a CI controller, sorry. Do you know what I mean now?
Franz
A: 

If you want to simply display the output of the CI script, you could open the URL, read the contents and output them - or use readfile(). From the stand point of the CI app, the request would be coming from the including server (not the end user), and to pass cookies/session vars along would require using something like cURL.

If you actually want to include the source code, that is possible, but a security risk. There are a few SO questions about this topic:

They note (among other things) that to include the source you'll need to make sure the CI site does not execute the page, but simply outputs the contents of the php file, and that actually including code from a URL is amazingly dangerous.

You're essentially trusting the included site (and all the servers the request goes through) to provide (good) code that the including server will then execute without question.

Tim Lytle
+1  A: 

As I couldn't seem to invoke the CI controller's functions, I decided it's easiest to simply load the page with jQuery:

$('#myDiv').load('ci/index.php/mycontroller', {}, function(){
        $('#myDiv #loading').hide();
        $('#myDiv #data').slideDown(500);
});
Indrek