未定义的属性:Index :: $ session

时间:2017-07-01 10:53:34

标签: php codeigniter

如何解决问题当我运行此代码时,我收到以下错误我已经加载了会话库仍然是我收到此错误
这是我的控制器

public function __construct(){
    parent::__construct();        
}
public function index() {
    $this->load->database();
    $this->load->helper('form', 'url');
    $this->load->library('session');
}
public function process() {     
    $this->load->model('login_model');
    $result = $this->login_model->validate();
    if (!$result) {
        $msg = '<font color=red>Invalid username and/or password.</font><br />';
        $this->load->view(base_url().'index',$msg);
    } else {
        if ($_SESSION['utype'] == 2 || $_SESSION['utype'] == 1) {
            redirect(base_url() . 'admin/dashboard');
        }
        if ($_SESSION['utype'] == 3 && !empty($_POST['urlValue'])) {
            redirect($_POST['urlValue']);
        }
        if (empty($_POST['urlValue'])) {
            redirect('/');
        }
    }
}

这是我的模特

   public function validate() { 
    $username = $this->security->xss_clean($this->input->post('email'));
    $password = $this->security->xss_clean($this->input->post('password'));            
    $pwd = base64_encode($password); 
    $this->db->where('email', $username);
    $this->db->where('password', $pwd);      
    $query = $this->db->get('adduser');
    $row = $query->row();

    if (count($row) > 0) {           
        $row = $query->row();
        $data = array(
            'uid' => $row->id,
            'uname' => $row->firstname,
            'utype' => $row->usertype,
            'uemail' => $row->email,
            'validated' => true
        );
        $this->session->set_userdata($data);
        return $row;
    }      
    return $row;
}

我遇到了这个问题:

 Severity: Notice
 Message: Undefined property: Index::$session
 Filename: core/Model.php
 Line Number: 77
 Backtrace:
 File: D:\xampp\htdocs\savepaise\application\models\Login_model.php
 Line: 42
 Function: __get
 File: D:\xampp\htdocs\savepaise\application\controllers\Index.php
 Line: 31
 Function: validate
 File: D:\xampp\htdocs\savepaise\index.php
 Line: 315
 Function: require_once
 Fatal error: Call to a member function set_userdata() on a non-object in 
 D:\xampp\htdocs\savepaise\application\models\Login_model.php on line 42
   A PHP Error was encountered
 Severity: Error
 Message: Call to a member function set_userdata() on a non-object
 Filename: models/Login_model.php
 Line Number: 42

1 个答案:

答案 0 :(得分:1)

我认为您需要在import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.stream.IntStream; import java.util.stream.Stream; public class DateTimeFormattingStackOverflow { static LocalDateTime dateTime = LocalDateTime.now(); static Stream<String> substrings(String str) { return IntStream.range(1, str.length() + 1) .mapToObj(i -> str.substring(0, i)); } static void printDateTime(String pattern) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern); System.out.println(pattern + ", " + dtf.format(dateTime)); } public static void main(String[] args) { Stream<String> patterns = substrings("EEEE") .flatMap(e -> substrings("MMMM").map(m -> e + " " + m)) .map(em -> em + " d"); patterns.forEach(DateTimeFormattingStackOverflow::printDateTime); } }

中的E M d, Sat 7 1 E MM d, Sat 07 1 E MMM d, Sat Jul 1 E MMMM d, Sat July 1 EE M d, Sat 7 1 EE MM d, Sat 07 1 EE MMM d, Sat Jul 1 EE MMMM d, Sat July 1 ... all library函数中加载controller
constructor

更新1:您可以像这样使用自动加载

转到index function并在那里,您可以编辑您需要的内容。

它们是数组,由包,库,助手,配置,语言和模型分隔。

实施例

public function __construct(){
    parent::__construct();
    $this->load->database();
    $this->load->helper('form', 'url');
    $this->load->library('session');        
}