在codeigniter中使用外部库类

时间:2014-06-25 16:57:52

标签: php codeigniter-2

我已经为登录验证创建了一个自定义库文件。如果我在$ this-> load-> library('form_validation')之前调用自定义库;

class VerifyLogin extends CI_Controller {
     function __construct()
     {
       parent::__construct();
       $this->load->helper('url'); 
       $this->load->model('loginuser');
       $this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
     }
     function index()
     {
       //my code
       $this->load->library('form_validation');
     }

    }

这是我得到$ this-> load-> library('form_validation')的错误;它完美无缺。 我只想知道为什么这段代码有效?我是否覆盖了默认库?

  

消息:未定义的属性:VerifyLogin :: $ form_validation

如果我在

之后加载库
class VerifyLogin extends CI_Controller {
     function __construct()
     {
       parent::__construct();
       $this->load->helper('url'); 
       $this->load->model('loginuser');
       $this->load->library('form_validation');
       $this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
     }
     function index()
     {
       //my code
     }

    }

2 个答案:

答案 0 :(得分:0)

您似乎错误地加载了库。尝试其中之一;

多个

$this->load->library(array('library1', 'library2'));

<强>单

$this->load->library('library1');
$this->load->library('library2');

答案 1 :(得分:0)

@sobiaholic

这是validate_login

class Validate_login extends CI_Controller {
  function __construct()
   {
     parent::__construct();
     $this->load->model('loginuser');
     $this->load->helper('url'); 
   }
   function is_logged()
   {
       if(isset($this->session->userdata['my_session_id']))
       {
           if(strlen($this->session->userdata['my_session_id']))
           {
               return TRUE;
           }
           else
           {
               return FALSE;
           }
       }
       else
       {
           return FALSE;
       }
   }
   function validate_login()
   {
      $session_id=$this->session->userdata['my_session_id'];
      $this->db->select('last_activity,user_data');
      $this -> db -> from('sessions');
      $this->db->where('session_id',$session_id);
      $this -> db -> limit(1);
      $query = $this -> db -> get();
      if($query -> num_rows() == 1)
      {
        $results=$query->result();
        $active_session=$this->session_alive($results[0]->last_activity,$session_id);
        if($active_session==TRUE)
        {
            return TRUE;
        }
        else
        {
            $this->verifylogin->logout();
        }
      }
      else
      {
        return false;
      }
   }
   function session_alive($valid_till,$session_id)
   {
       $time_limit=$this->config->item('sess_expiration');
       if (time() - $valid_till > $time_limit) 
        {
            return FALSE;
        }
        else
        {
            if($this->update_session($session_id)==TRUE)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
   }
   function update_session($session_id)
   {
       $new_time=time();
       $data=array('last_activity'=>$new_time);
       $this->db->where('session_id',$session_id);
       $this->db->update('sessions',$data);
       if($this->db->affected_rows())
       {
           return TRUE;
       }
   }
}