我正在尝试创建一个博客站点,我有一个名为ci_test的数据库,它有两个表用户和文章。我有一个无法解决的会话问题。我设置 -
$config['encryption_key'] = '1234';
另外,设置属性
$autoload['libraries'] = array('database','session');
但它给出了错误:
严重性:注意
消息:未定义属性:Admin :: $ session
文件名:controllers / admin.php
行号:18
我的登录代码是:
<?php
class Login extends MY_Controller{
public function index()
{
$this->load->helper('form');
$this->load->view('public/admin_login');
}
public function admin_login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username','user name','required|alpha|trim');
$this->form_validation->set_rules('password','password','required');
$this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');
if($this->form_validation->run())
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('loginmodel');
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id)
{
//$this->load->library('session');
$this->session->set_userdata('user_id', $login_id);
redirect('admin/dashboard');
}
else{
echo 'password dont';
}
}
else
{
$this->load->view('public/admin_login');
}
}
}
从评论登录模式更新
class Loginmodel extends CI_Model{
public function login_valid($username, $password) {
$q = $this->db->where(['uname'=>$username,'pword'=>$password])->get('users');
if($q->num_rows()) {
return $q->row()->id; //return TRUE; } else{ return FALSE;
}
}
管理员控制器:
<?php
class Admin extends CI_Controller{
public function dashboard()
{
$this->load->model('articlesmodel','article');
$articles = $this->article->articles_list();
$this->load->view('admin/dashboard',['articles'=> $articles]);
}
public function __construct()
{
if(! $this->session->userdata('user_id'))
{
redirect('login');
}
}
}
型号:
<?php
class Articlesmodel extends CI_Model{
public function articles_list()
{
$user_id = $this->session->userdata('user_id');
$query = $this->db->select('title')->where('user_data',$user_id)->get('articles');
return $query->result();
}
}
视图:
<div class="container">
<table class="table">
<thead>
<tr>
<td>Serial no</td>
<td>Article List</td>
<td>#</td>
</tr>
</thead>
<tbody>
<?php if(count($articles)): ?>
<?php foreach($articles as $article): ?>
<tr>
<td>
1
</td>
<td>
<?= $article->title; ?>
</td>
<td>
<a class="btn btn-primary">Edit</a>
<a class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endforeach;?>
<?php else:?>
<tr>
<td colspan="3">Record not found</td>
</tr>
<?php endif;?>
</tbody>
</table>
</div>
答案 0 :(得分:0)
确保您的文件名是controllers/Admin.php
而不是controllers/admin.php
首字母总是大写只适用于类
并且你的construct()应该在所有方面都在class下面并且你缺少parent :: __ construct()
<?php
class Admin extends CI_Controller {
public function __construct() {
parent::__construct(); // Missing
if(! $this->session->userdata('user_id'))
{
redirect('login');
}
}
public function dashboard() {
}
}
我认为你已将它设置在一个组中。其中user_id是组名
$data = array(
'id' => $login_id->column_name
);
$this->session->set_userdata('user_id', $data);
访问数据
$id = $this->session->userdata('user_id');
$id['id'];
或尝试
$this->session->set_userdata(array('user_id' => $login_id->column_name)); //
http://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data
确保您在Ci3中使用文件设置了路径
$config['sess_cookie_name'] = 'ci_sessions';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = FCPATH . 'application/cache/session/';
Codeigniter加密密钥http://randomkeygen.com/
$config['encryption_key'] = 'YyO29izmGbTj84kQPAkxkjI1Cz4Whe71';
public function login_valid($username, $password) {
$this->db->where('uname', $username);
$this->db->where('pword', $password);
$query = $this->db->get('users');
if ($query->num_rows() == 1) {
return $query->row();
} else {
return false;
}
}
然后在控制器上登录正确
$login = $this->some_model->login($this->input->post('username'), $this->input->post('password'));
if ($login) {
$this->session->set_userdata(array('user_id' => $login->column_name));
redirect('successcontroller');
} else {
// Error
}
答案 1 :(得分:0)
$config['encryption_key'] = '1234';
这不会起作用,因为CodeIgniter对它的encryption_key有要求。请参阅此处的文档。
https://codeigniter.com/userguide3/libraries/encryption.html
试试这个
$key = $this->encryption->create_key(16);
然后使用下面的那个键
$config['encryption_key'] = 'YOUR KEY';
或者,甚至更好,这里有两个网站将为您生成CodeIgniter加密密钥。
http://jeffreybarke.net/tools/codeigniter-encryption-key-generator/