php 未定义的属性:CI::$session
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23978239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined property: CI::$session
提问by Hammad
I am having trouble loading my model on my login page. It has a problem with sessions on here. I have a security class helper which $key is part of it. But I think it is to do with session as error says.
我在登录页面上加载模型时遇到问题。这里的会话有问题。我有一个安全类助手, $key 是其中的一部分。但我认为这与错误所说的会话有关。
I think I have set up num rows correct not sure if also that may be cause of it.
我想我已经正确设置了 num 行,不确定这是否也可能是它的原因。
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$session
Filename: core/Model.php
Line Number: 51
model
模型
<?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;
}
}
回答by Hammad
If you are using session in your code then make sure to autoload in config.php file by setting an encryption key. If you don't autoload it then you can load it using $this->load->library('session')
.
But you must set encryption key in order to autoload or load session in controller functions.
如果您在代码中使用会话,请确保通过设置加密密钥在 config.php 文件中自动加载。如果您不自动加载它,那么您可以使用$this->load->library('session')
. 但是您必须设置加密密钥才能在控制器功能中自动加载或加载会话。
Detailed information here. Working with sessions in codeigniter
详细信息在这里。在 codeigniter 中使用会话
回答by alivojdan
plus of the started session, you need to check parent in constructor function or write this code in your constructor: **parent::__construct();**
加上已启动的会话,您需要在构造函数中检查 parent 或在构造函数中编写此代码: **parent::__construct();**