2011-08-13 8 views
2

세션을 확인하기 위해 MY_Controller 클래스를 만들었습니다.Codeigniter redirect() 문제

내 LoginController가 사용자 정보를 확인하고 있다면, 사용자를 PainelController로 리디렉션합니다. 리디렉션을 사용하면 내 URL이/localhost/session/login 대신/localhost/painel로 새로 고쳐집니다.

리디렉션을 사용할 때로드 ->보기 만 사용하여 세션에 액세스 할 수없는 문제가 있습니다.

해결 방법이 있습니까?

미리 도움을 청하십시오.

PS : 나는 htaccess로를 사용 하나 CI Wiki

편집 로그인의

class MY_Controller extends CI_Controller { 

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

} 

조각에서 발견이 : -

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

CI_Controller를 확장

내 Painel 내 PainelController (확장 MY_Controller)가 작동하지 않습니다에서이 세션 값에 액세스하려고해도보기

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

, 말할 것이다 :

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 
+0

이 점에서 한 가지 문제가 발생하지 않았습니까? 리디렉션 후에 세션이 자체적으로 지워지나요? – Aniket

+0

만약 그렇다면 왜, 어떻게 사용해야합니까? 왜로드 ->보기를 사용할 때 제대로 작동하기 때문에 리다이렉션만으로는이 문제가 발생합니다. – Gerep

+0

일부 세션 변수가 반향되는지 여부를 확인할 수 있습니다. – Aniket

답변

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'); 
+1

CodeIgniter의 세션 라이브러리는'session_start'를 수행합니다. –

+0

그게 내가 그의 도서관을 요청한 이유지만, 그는 그렇지 않은 것 같다. 일반적으로 모든 컨트롤러 또는 메소드에서 중복 세션 인증 대신에 제한 또는 권한을 처리하기위한'auth' 라이브러리가 있어야합니다. – ace

+0

그 문제는 모든 컨트롤러에 인증 라이브러리를로드해야한다는 것입니다. – Gerep