php – 未定义属性:CI :: $session

前端之家收集整理的这篇文章主要介绍了php – 未定义属性:CI :: $session前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在登录页面上加载模型时遇到问题.这里的会话有问题.我有一个安全类帮助器,其中$key是其中的一部分.但我认为这与会话有关,因为错误说.

我想我已经设置了num行正确,不确定是否也可能是它的原因.

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$session
Filename: core/Model.PHP
Line Number: 51

模型

<?PHP  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {

    private $user_id;
    private $username;
    private $permission = array();

    public function __construct() {

        if (isset($this->session->userdata['user_id'])) {
            $user_query = $this->db->query("SELECT * FROM " . $this->input->post('dbprefix') . "user WHERE user_id = '" . (int)$this->session->userdata['user_id'] . "' AND status = '1'");

            if ($user_query->num_rows) {
                $this->user_id = $user_query->row['user_id'];
                $this->username = $user_query->row['username'];

                $this->db->query("UPDATE " . $this->input->post('dbprefix') . "user SET ip = '" . $this->db->escape($this->input->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->userdata['user_id'] . "'");

                $user_group_query = $this->db->query("SELECT permission FROM " . $this->input->post('dbprefix'). "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

                $permissions = unserialize($user_group_query->row['permission']);

                if (is_array($permissions)) {
                    foreach ($permissions as $key => $value) {
                        $this->permission[$key] = $value;
                    }
                }
            } else {
                $this->logout();
            }
        }
    }

    public function login($username,$password) {
        $user_query = $this->db->query("SELECT * FROM " . $this->input->post('dbprefix') . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = SHA1(CONCAT(salt,SHA1(CONCAT(salt,SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'");

        if ($user_query->num_rows) {
            $this->session->userdata['user_id'] = $user_query->row['user_id'];

            $this->user_id = $user_query->row['user_id'];
            $this->username = $user_query->row['username'];         

            $user_group_query = $this->db->query("SELECT permission FROM " . $this->input->post('dbprefix') . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

            $permissions = unserialize($user_group_query->row['permission']);

            if (is_array($permissions)) {
                foreach ($permissions as $key => $value) {
                    $this->permission[$key] = $value;
                }
            }

            return true;
        } else {
            return false;
        }
    }
如果您在代码中使用会话,请确保通过设置加密密钥在config.PHP文件自动加载.如果您不自动加载它,则可以使用$this-> load-> library(‘session’)加载它.
但您必须设置加密密钥才能在控制器功能自动加载或加载会话.

详细信息在这里. Working with sessions in codeigniter

原文链接:https://www.f2er.com/php/133505.html

猜你在找的PHP相关文章