2

セッションをチェックするためにMY_Controllerクラスを作成しました。

LoginControllerがユーザー情報をチェックしていて、問題がなければ、ユーザーをPainelControllerにリダイレクトします。リダイレクトを使用しているので、URLは/ localhost / session/loginではなく/localhost/painelで更新されます

問題は、リダイレクトを使用すると、load-> viewを使用するだけで、セッションにアクセスできないことです。

回避策はありますか?

助けてくれてありがとう。

PS: CIWikiにある.htaccessを使用しています

編集

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.'));
            }

-

私のPainelビュー

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これを処理する既存のライブラリを使用して、これをすべてのコンストラクターcontrollerまたはに含めることができます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 に答える