2

我创建了一个 MY_Controller 类来检查会话。

我的 LoginController 正在检查用户信息,如果没问题,我将用户重定向到 PainelController。我使用重定向,所以我的 url 将使用 /localhost/painel 而不是 /localhost/session/login 刷新

问题是当我使用重定向时,我无法访问我的会话,只能使用 load->view。

有解决方法吗?

提前感谢您的帮助。

PS:我使用 .htaccess,在CI Wiki上找到的

编辑

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if(!$this->session->userdata('usuario')) {
            redirect('login');
        }
    }

}

登录片段:扩展 CI_Controller

if( $rs )
            {

                $this->session->set_userdata('usuario', $usuario);//usuario is a object
                //$this->load->view('painel');//it works
                redirect('painel', 'location');//it doesn't
            }
            else
            {
                $this->load->view('login', $data = array('mensagem'=>'Usuário ou senha inválidos.'));
            }

--

我的面板视图

echo $this->session->userdata('usuario')->usuario_nome; //it works
only if I load->view('painel')

即使我尝试在我的 PainelController(扩展 MY_Controller)上访问此会话值,它也不起作用,会说:

Message: main() [function.main]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Usuario" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition
4

1 回答 1

0

我尝试在我的 CI 沙箱上重定向几次,但我没有丢失我的会话。
您在使用哪些库/助手PainelController
看来你没有@session_start();那个特定的控制器。

通常,您可以使用现有的Auth库来为您处理此问题,然后将其包含在每个controllerthat 的构造函数中,甚至包含在MY_Controller.
这是一个例子:

class Events extends CI_Controller {
// Constructor function
public function __construct()
{
    //load initial models and libraries that are needed for the controller
    parent::__construct();
    $this->load->library('auth');
            ...
    }
    ...
}

然后我的auth库就会有这样的构造函数。

class Auth {
  var $CI       = NULL;

  function Auth($props = array())
  {
    $this->CI =& get_instance();

    // Load additional libraries, helpers, etc.
    $this->CI->load->library('session');
    $this->CI->load->database();
    $this->CI->load->helper('url');
    @session_start();
  }
  ...
}

编辑:

您也可以将其包含在您autoload通常位于的config/autoload.php.
http://codeigniter.com/user_guide/general/autoloader.html

这是一个例子。

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
|   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('database','session', 'encrypt');
于 2011-08-15T01:19:21.810 回答