Welcome Guest, Not a member yet? Register   Sign In
probem with session library when using CLI
#1

[eluser]Unknown[/eluser]
I put a fresh install of CI 2.1.3 in /var/www/ci/

I add edit application/controllers/welcome.php so it's contents are the following:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function index()
{
  echo "test\n";
}
}

At this point, if I run
Code:
$ php /var/www/ci/index.php
I get the expected output:
Code:
test

Now I edit config/autoload.php line 55 so that it reads
Code:
$autoload['libraries'] = array('session');
and config/config.php line 227 so that it reads
Code:
$config['encryption_key'] = '348rc9834rc9834nc9834nc';

Now if I run
Code:
$ php /var/www/ci/index.php
I get the following output:
Code:
<div  solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index: REMOTE_ADDR</p>
<p>Filename: core/Input.php</p>
<p>Line Number: 351</p>

</div><div  solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /var/www/ci/system/core/Exceptions.php:185)</p>
<p>Filename: libraries/Session.php</p>
<p>Line Number: 675</p>

However, if I point my browser to
Code:
http://localhost/ci/
I get the expected output
Code:
test

I would like to run controllers from my command line while still autoloading the session library. Is this possible without editing anything in the system directory?
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

It depends. Do you need sessions available from the CLI? The problem is that a lot of the $_SERVER variables don't get set. Since the CLI doesn't support cookies (and probably never will), sessions won't work.

I doubt that editing the system files (or extending the Session class) will make any difference. I would suggest that you make a design choice. Some of your app can be tailored to work specifically with the CLI, and the rest can be geared towards working via a Web server. I doubt you'll ever need a page that's available via both. Usually you'll want your app to output text (for the CLI), or output HTML (for a browser).

You can also use multiple environments, so within your config directory, you'll have a directory named CLI. Within your index.php, you can have some logic that intelligently guesses and sets the environment. In your autoload.php file for the CLI environment, you'd make sure that you don't load the Session class.

I actually have a CLI controller I extend to prevent access to certain controllers via HTTP.

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* This is the base for controllers that can only be access via the CLI */
class Cli_controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        
        if ( ! $this->input->is_cli_request())
        {
            show_404();
        }
        
    }
}

// END Cli_controller class

/* End of file cli_controller.php */
/* Location: ./application/libraries/cli_controller.php */

Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB