构造函数中的$this->load->没有执行,一定要实例化吗?
本帖最后由 eyouy 于 2017-6-19 14:31 编辑做的登录模块,controllers\login.php,需要用到加密和session
<?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');
$this->load->library('Encryption');
$this->load->library('Session');
}
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$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: NoticeMessage: Undefined property: Login::$encryptionFilename: controllers/Login.phpLine Number: 76Backtrace:File: E:\upupw\mitang\application\admin\controllers\Login.php
Line: 76
Function: _error_handlerFile: 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 76A PHP Error was encounteredSeverity: ErrorMessage: Call to a member function encrypt() on nullFilename: controllers/Login.phpLine Number: 76Backtrace:
莫非一定要在独立实例化?
Hex 发表于 2017-6-19 15:14
PHP 版本没问题。你看你贴的代码,有两个 function index()。。。。 你仔细看看哈 ...
最顶上那个是被注释的,完整的是这样的。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->library('Session/Session');
$this->load->model('login_model');
$this->load->library('encryption');
}
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('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(){
}
}
感谢回复,有 $this->load->library('form_validation');这一行,就在new CI_Form的上面,如果去掉了$this->form_validation=new CI_Form_validation();就显示下面的错误,encryption 和session也一样。
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$form_validation
Filename: controllers/Login.php
Line Number: 42
Backtrace:
File: E:\upupw\mitang\application\admin\controllers\Login.php
Line: 42
Function: _error_handler
File: E:\upupw\mitang\admin\index.php
Line: 315
Function: require_once
A PHP Error was encounteredSeverity: ErrorMessage: Call to a member function set_rules() on nullFilename: controllers/Login.phpLine Number: 42Backtrace:
Hex 发表于 2017-6-19 15:26
$this->load->library('Session/Session'); 这句错了,应该是 $this->load->library('session'); 看手册 ...
改了,还是一直提示A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$form_validation
Filename: controllers/Login.php
Line Number: 38
Backtrace:
File: E:\upupw\mitang\application\admin\controllers\Login.php
Line: 38
Function: _error_handler
File: E:\upupw\mitang\admin\index.php
Line: 315
Function: require_once
A PHP Error was encountered
Severity: Error
Message: Call to a member function set_rules() on null
Filename: controllers/Login.php
Line Number: 38
Backtrace: 不要这样写: $this->form_validation=new CI_Form_validation(); 这是错误的,不知道你在哪里看到的,完全错误。
应该写成 $this->load->library('form_validation');
encryption 一样也需要 $this->load->library('encryption');
先按正确的方式写,然后再看出什么错误,再去解决,不要出错了就乱写一通。 eyouy 发表于 2017-6-19 14:33
感谢回复,有 $this->load->library('form_validation');这一行,就在new CI_Form的上面,如果去掉了$this- ...
$this->load->library('form_validation'); 这样写不可能出错,你是 CI 3.x 吗? Hex 发表于 2017-6-19 14:38
$this->load->library('form_validation'); 这样写不可能出错,你是 CI 3.x 吗?
是的。3.1.4,一楼的就是目前的代码 eyouy 发表于 2017-6-19 14:43
是的。3.1.4,一楼的就是目前的代码
你写了两个index 函数,这肯定不行啊。另外,你的 PHP 版本是 5.3.7 以上吗? Hex 发表于 2017-6-19 14:58
你写了两个index 函数,这肯定不行啊。另外,你的 PHP 版本是 5.3.7 以上吗? ...
哪里有两个index函数?php版本是5.6.15 eyouy 发表于 2017-6-19 15:13
哪里有两个index函数?php版本是5.6.15
PHP 版本没问题。你看你贴的代码,有两个 function index()。。。。 你仔细看看哈 eyouy 发表于 2017-6-19 15:17
最顶上那个是被注释的,完整的是这样的。
$this->load->library('Session/Session'); 这句错了,应该是 $this->load->library('session'); 看手册里的例子,不要自己猜测。
其他代码我看没问题
页:
[1]
2