本帖最后由 eyouy 于 2017-6-19 14:31 编辑
做的登录模块,controllers\login.php,需要用到加密和session
PHP复制代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index ()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->view('index');
}
*/
public function __construct ()
{
parent ::__construct ();
$this->load->model('login_model');
[color =#ff0000]$this->load->library('Encryption');
[color =#ff0000] $this->load->library('Session');
}
public function index ()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
[color =#ff0000]$this->form_validation=new CI_Form_validation();
$this->form_validation->set_rules('username', 'username', 'trim|required');
$this->form_validation->set_rules('password', 'password', 'trim|required');
$post_username = $this->input->post('username', TRUE);
$post_password = $this->input->post('password', TRUE);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('index');
}
else
{
$userinfo = $this->login_model->login($post_username,$post_password);
$username = $userinfo['username'];
$password = $userinfo['password'];
$md5password=md5($post_password);
if(empty($username))
{
show_error ('用户名不存在');
}elseif($md5password != $password)
{
show_error ('密码错误');
}elseif($status = 0){
show_error ('账号已过期,请联系管理员');
}
$admin_info = $this->encryption->encrypt($userinfo['id'].'|'.$userinfo['username'].'|'.$userinfo['status'].'|'.$userinfo['group']);
$time = time();
$lifetime = 0;
$this->load->helper('cookie');
if($lifetime)
{
setcookie('admin_info',$admin_info,$lifetime*86400);
}else{
delete_cookie ('admin_info'); //清除COOKIE中登录信息
$this->session->set_userdata('admin_info', $admin_info);
}
$this->load->view('main');
}
}
public function logout (){
}
}
复制代码
加了form_validation,就老提示出错,后来看论坛里面有人加了$this->form_validation=new CI_Form_validation();这个就好了,现在把session和Encryption的放到构造函数里面,还是提示
A PHP Error was encounteredSeverity: Notice Message: Undefined property: Login:encryption Filename: controllers/Login.php Line Number: 76 Backtrace: File: E:\upupw\mitang\application\admin\controllers\Login.php
Line: 76
Function: _error_handler File: E:\upupw\mitang\admin\index.php
Line: 315
Function: require_once
Fatal error: Call to a member function encrypt() on null in E:\upupw\mitang\application\admin\controllers\Login.php on line 76 A PHP Error was encounteredSeverity: Error Message: Call to a member function encrypt() on null Filename: controllers/Login.php Line Number: 76 Backtrace:
莫非一定要在独立实例化?
|