【问题标题】:Fatal error: Call to a member function set_userdata() on a non-object致命错误:在非对象上调用成员函数 set_userdata()
【发布时间】:2016-01-06 01:30:43
【问题描述】:

我试图在 CodeIgniter 中登录,但在我的模型和 admin_home.php 中出现此错误。我已经尝试修复它,但仍然没有得到答案。如果可以,请提供帮助。

错误

遇到了 PHP 错误

严重性:通知

消息:未定义的属性:Admin_login::$session

文件名:core/Model.php

行号:52

致命错误:在第 31 行的 /.../admin_login_model.php 中的非对象上调用成员函数 set_userdata()

这是我的代码。

admin_login.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_login extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        /* Load the model*/
        $this->load->model('admin_login_model');
    }

    public function index($msg = NULL){
        // Load our view to be displayed
        // to the user
        $data['msg'] = $msg;
        $this->load->view('admin_login_view', $data);
    }

    public function process(){
        // Validate the user can login
        $result = $this->admin_login_model->validate();
        // Now we verify the result
        if(! $result){
            // If user did not validate, then show them login page again
            $msg = '<font color=red>Invalid username and/or password.</font><br />';
            $this->index($msg);
        }else{
            // If user did validate, 
            // Send them to members area
            redirect('admin_home'); 
        }       
    }
}
?>

admin_login_view.php

<!doctype html>
<html>
<head>
    <link   href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
      rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Libre+Baskerville' rel='stylesheet' type='text/css'>
<style>
    body {
        padding-top: 70px; /* needed to position navbar properly */
        font-family: 'Libre Baskerville', serif;
        } 
</style>
<title>Admin Login!</title>
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
  <div class="container">
    <div class="container-fluid">
        <div class="navbar-header">
          <p class="navbar-brand">
            <a href="">Admin Login!</a>
          </p>
        </div>
      </div>
  </div>
</nav>
<div class="container" id="login_form">
    <form action='https://w1439345.users.ecs.westminster.ac.uk/ECWM604/ci/index.php/admin_login/process' method='post' name='process'>
        <h2>Login</h2>
        <br />          
        <?php if(! is_null($msg)) echo $msg;?>  
        <label for='username'>Username</label>
        <input type='text' name='username' id='username' size='25' /><br />

        <label for='password'>Password</label>
        <input type='password' name='password' id='password' size='25' /><br />                         

        <input type='Submit' value='Login' />           
    </form>     
</div> <!-- container -->

</body>
</html>

admin_login_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_login_model extends CI_Model {

function __construct()
{
    parent::__construct();
    $this->load->database();
}

public function validate(){
    /* grab admin input*/
    $username = $this->security->xss_clean($this->input->post('username'));
    $password = $this->security->xss_clean($this->input->post('password'));

    /* Prep the query*/
    $this->db->where('username', $username);
    $this->db->where('password', $password);

    /* Run the query*/
    $query = $this->db->get('admin');
    /* check if there are any results*/
    if($query->num_rows() == 1)
    {
        // If there is a user,create session data
        $row = $query->row();
        $data = array(
                'username' => $row->username,
                'validated' => true
                );
        $this->session->set_userdata($data);
        return true;
    }
    /* If the previous process did not validate
     then return false.*/
    return false;
}
}
?>

和 admin_home.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Admin_home extends CI_Controller{

function __construct(){

    parent::__construct();
    $this->check_isvalidated();
}

public function index(){
    /* If the admin is validated, then this function will run*/
    echo 'Congratulations, you are logged in.';
}

private function check_isvalidated(){
    if(! $this->session->userdata('validated')){
        redirect('admin_login');
    }
}

public function do_logout(){
    $this->session->sess_destroy();
    redirect('admin_login');
}
}
?>

感谢您的帮助。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您需要先加载库。所以也许在你的__construct()Admin_login_model

    $this->load->library('session');
    

    之后,您可以像现在一样玩弄它:

    $this->session->set_userdata($data);
    

    更多信息请参见their website

    【讨论】:

    • 在模型中添加加载会话后出现此错误:“为了使用 Session 类,您需要在配置文件中设置加密密钥。”
    • 行得通,谢谢;顺便说一句,你知道如何从一个控制器重定向到另一个控制器吗?命令:redirect('admin_home/index') 无法正常工作
    • @Nynaeve:将控制器问题作为一个新问题发布,并考虑接受/支持您给出的答案,如果它们对您有用。
    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2013-11-25
    • 2012-05-30
    • 2016-08-30
    • 2015-02-06
    • 2012-05-14
    相关资源
    最近更新 更多