问题
Is there a way of preventing session class to set cookie and adding a record in database when calling it from the command line?
回答1:
The simplest way I think would be to extend the Session class like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Session extends CI_Session
{
public function __construct()
{
$CI = get_instance();
if ($CI->input->is_cli_request())
{
return;
}
parent::__construct();
}
}
Just place it in the application/libraries
folder. And with a controller like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function no_cli_session()
{
$this->load->library('session');
$this->session->set_userdata('logged', 'yes');
}
}
When you call the function from your browser the session is set and when you call it from cli it is not.
来源:https://stackoverflow.com/questions/8394225/prevent-cookie-when-running-script-from-cli-in-codeigniter