2012-01-25 22 views
2

私はcodeigniterでログインシステムを作成しています。私はcodeigniterで新しいです。テストするときに私がCodeigniter getinstance()エラー、ライブラリ、ヘルパーがロードされていませんか?

$CI =&get_instance(); 

$CI =get_instance(); 

$this->session->userdata 

すべての3つの方法が、エラーを取得を試してみました

<?php 
class Login extends CI_Controller { 

    function __construct() 
    { 
     if($this->is_logged_in() === true){ 
      redirect('welcome'); 
     } 
    } 

    function index() 
    { 
     $data['main_content'] = 'login_form'; 
     $data['class_name'] = get_class(); 
     $data['no_header'] = '1'; 
     $data['no_footer'] = '1'; 
     $this->load->view('includes/template', $data);  
    } 

    function validate_credentials() 
    {  
     $this->load->model('admin_model'); 
     $query = $this->admin_model->validate(); 
     print_r($query); 
     if($query) // if the user's credentials validated... 
     { 
      $data = array(
       'AdminID' => $query, 
       'is_logged_in' => true 
      ); 
      $this->session->set_userdata($data); 
      redirect('welcome'); 
     } 
     else // incorrect username or password 
     { 
      echo "here"; 
      exit; 
      $this->index(); 
     } 
    } 

    function signup() 
    { 
     $data['main_content'] = 'signup_form'; 
     $this->load->view('includes/template', $data); 
    } 

    function create_member() 
    { 
     $this->load->library('form_validation'); 

     // field name, error message, validation rules 
     $this->form_validation->set_rules('first_name', 'Name', 'trim|required'); 
     $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); 
     $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email'); 
     $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]'); 
     $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
     $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); 


     if($this->form_validation->run() == FALSE) 
     { 
      $this->load->view('signup_form'); 
     } 

     else 
     {   
      $this->load->model('membership_model'); 

      if($query = $this->membership_model->create_member()) 
      { 
       $data['main_content'] = 'signup_successful'; 
       $this->load->view('includes/template', $data); 
      } 
      else 
      { 
       $this->load->view('signup_form');   
      } 
     } 

    } 

    function logout() 
    { 
     $this->session->sess_destroy(); 
     $this->index(); 
    } 

    function is_logged_in() 
    { 
     $CI =&get_instance(); 
     $is_logged_in = $CI->session->userdata('is_logged_in'); 
     if(isset($is_logged_in) || $is_logged_in === true) 
     { 
      return true; 
     }else{ 
      return false; 
     } 
    } 
} 

A PHP Error was encountered 

Severity: Notice 

Message: Trying to get property of non-object 

Filename: controllers/login.php 

Line Number: 92 

Fatal error: Call to a member function userdata() on a non-object in P:\xampp\htdocs\xxxxx\xxxxx\application\controllers\login.php on line 92 

ここにある私のlogin.phpコントローラファイルの私のログインシステムこのエラーを取得しています。

答えて

3

あなたが最初に親のコンストラクタを呼び出すことなく、コントローラのコンストラクタでis_logged_in()関数を呼び出しています。多分それはおそらくいくつかのリソースがまだロードされていない理由です。

試しにこれを追加する:

function __construct() 
{ 
    parent::__construct(); //you always have to call parent's constructor before ANYTHING 

    if($this->is_logged_in() === true){ 
     redirect('welcome'); 
    } 
} 
0

の新しいライブラリのセッションライブラリは、get_instance()でロードしていません。の別のインスタンスを使用して、なぜ

function is_logged_in() 
{ 
    $is_logged_in = $this->session->userdata('is_logged_in'); 
    if(isset($is_logged_in) || $is_logged_in === true) 
    { 
     return true; 
    }else{ 
     return false; 
    } 
} 

は知ってはいけない:

は単に@jereで述べたように、あなたが親を呼び出し、セッションがすでにロードされていると仮定し、:: __構築物()あなたのコントローラで、やりますここのクラス。

あなたがそれを行うダメならば、同様に、再びセッションライブラリをロードしてみてください。

$CI->load->library('session') 
$is_logged_in = $CI->session->userdata('is_logged_in'); 
//rest of your old code 
関連する問題