用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2590|回复: 11
收起左侧

[URL] 构造函数中的$this->load->没有执行,一定要实例化吗?

[复制链接]
发表于 2017-6-19 14:13:42 | 显示全部楼层 |阅读模式
本帖最后由 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 encountered
Severity: 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 encountered
Severity: Error
Message: Call to a member function encrypt() on null
Filename: controllers/Login.php
Line Number: 76
Backtrace:

莫非一定要在独立实例化?


 楼主| 发表于 2017-6-19 15:17:30 | 显示全部楼层
Hex 发表于 2017-6-19 15:14
PHP 版本没问题。你看你贴的代码,有两个 function index()。。。。 你仔细看看哈 ...

最顶上那个是被注释的,完整的是这样的。
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');

  3. class Login extends CI_Controller {

  4.         /**
  5.          * Index Page for this controller.
  6.          *
  7.          * Maps to the following URL
  8.          *                 http://example.com/index.php/welcome
  9.          *        - or -
  10.          *                 http://example.com/index.php/welcome/index
  11.          *        - or -
  12.          * Since this controller is set as the default controller in
  13.          * config/routes.php, it's displayed at http://example.com/
  14.          *
  15.          * So any other public methods not prefixed with an underscore will
  16.          * map to /index.php/welcome/<method_name>
  17.          * @see https://codeigniter.com/user_guide/general/urls.html
  18.          */

  19.        
  20.          public function __construct()
  21.     {
  22.         parent::__construct();
  23.                 $this->load->library('Session/Session');
  24.         $this->load->model('login_model');
  25.                 $this->load->library('encryption');
  26.                
  27.     }
  28.        
  29.         public function index()
  30.         {
  31.                 $this->load->helper(array('form', 'url'));
  32.                 $this->load->library('form_validation');
  33.                
  34.                
  35.                 $this->form_validation->set_rules('username', 'username', 'trim|required');
  36.                 $this->form_validation->set_rules('password', 'password', 'trim|required');
  37.                
  38.                 $post_username = $this->input->post('username', TRUE);
  39.                 $post_password = $this->input->post('password', TRUE);

  40.                
  41.                 if ($this->form_validation->run() == FALSE)
  42.         {
  43.             $this->load->view('index');
  44.         }
  45.         else
  46.         {
  47.                        
  48.                         $userinfo = $this->login_model->login($post_username,$post_password);
  49.                        
  50.                         $username = $userinfo['username'];
  51.                         $password = $userinfo['password'];
  52.                         $md5password=md5($post_password);
  53.                        
  54.                         if(empty($username))
  55.                         {
  56.                                 show_error('用户名不存在');
  57.                                
  58.                         }elseif($md5password != $password)
  59.                         {
  60.                                 show_error('密码错误');
  61.                                
  62.                         }elseif($status = 0){
  63.                                 show_error('账号已过期,请联系管理员');
  64.                         }
  65.                        
  66.                        
  67.                        
  68.                
  69.                         $admin_info = $this->encryption->encrypt($userinfo['id'].'|'.$userinfo['username'].'|'.$userinfo['status'].'|'.$userinfo['group']);
  70.                        
  71.                         $time = time();
  72.                         $lifetime = 0;
  73.                         $this->load->helper('cookie');
  74.                        
  75.                         if($lifetime)
  76.                         {
  77.                                 setcookie('admin_info',$admin_info,$lifetime*86400);
  78.                         }else{
  79.                                 delete_cookie('admin_info'); //清除COOKIE中登录信息
  80.                                
  81.                                 $this->session->set_userdata('admin_info', $admin_info);       
  82.                         }
  83.                        
  84.                         $this->load->view('main');
  85.         }
  86.                
  87.                
  88.                
  89.         }
  90.        
  91.        
  92.        
  93.         public function logout(){
  94.                
  95.                
  96.         }
  97. }
复制代码
 楼主| 发表于 2017-6-19 14:33:58 | 显示全部楼层
感谢回复,有 $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 encountered
Severity: Error
Message: Call to a member function set_rules() on null
Filename: controllers/Login.php
Line Number: 42
Backtrace:

 楼主| 发表于 2017-6-19 15:34:11 | 显示全部楼层
Hex 发表于 2017-6-19 15:26
$this->load->library('Session/Session'); 这句错了,应该是 $this->load->library('session'); 看手册 ...

改了,还是一直提示
  1. A PHP Error was encountered

  2. Severity: Notice

  3. Message: Undefined property: Login::$form_validation

  4. Filename: controllers/Login.php

  5. Line Number: 38

  6. Backtrace:

  7. File: E:\upupw\mitang\application\admin\controllers\Login.php
  8. Line: 38
  9. Function: _error_handler

  10. File: E:\upupw\mitang\admin\index.php
  11. Line: 315
  12. Function: require_once

  13. A PHP Error was encountered

  14. Severity: Error

  15. Message: Call to a member function set_rules() on null

  16. Filename: controllers/Login.php

  17. Line Number: 38

  18. Backtrace:
复制代码
发表于 2017-6-19 14:31:22 | 显示全部楼层
不要这样写: $this->form_validation=new CI_Form_validation(); 这是错误的,不知道你在哪里看到的,完全错误。
应该写成 $this->load->library('form_validation');
encryption 一样也需要 $this->load->library('encryption');

先按正确的方式写,然后再看出什么错误,再去解决,不要出错了就乱写一通。
发表于 2017-6-19 14:38:54 | 显示全部楼层
eyouy 发表于 2017-6-19 14:33
感谢回复,有 $this->load->library('form_validation');这一行,就在new CI_Form的上面,如果去掉了$this- ...

$this->load->library('form_validation'); 这样写不可能出错,你是 CI 3.x 吗?
 楼主| 发表于 2017-6-19 14:43:16 | 显示全部楼层
Hex 发表于 2017-6-19 14:38
$this->load->library('form_validation'); 这样写不可能出错,你是 CI 3.x 吗?

是的。3.1.4,一楼的就是目前的代码
发表于 2017-6-19 14:58:15 | 显示全部楼层
eyouy 发表于 2017-6-19 14:43
是的。3.1.4,一楼的就是目前的代码

你写了两个  index 函数,这肯定不行啊。另外,你的 PHP 版本是 5.3.7 以上吗?
 楼主| 发表于 2017-6-19 15:13:28 | 显示全部楼层
Hex 发表于 2017-6-19 14:58
你写了两个  index 函数,这肯定不行啊。另外,你的 PHP 版本是 5.3.7 以上吗? ...

哪里有两个index函数?php版本是5.6.15
发表于 2017-6-19 15:14:42 | 显示全部楼层
eyouy 发表于 2017-6-19 15:13
哪里有两个index函数?php版本是5.6.15

PHP 版本没问题。你看你贴的代码,有两个 function index()。。。。 你仔细看看哈
发表于 2017-6-19 15:26:38 | 显示全部楼层
eyouy 发表于 2017-6-19 15:17
最顶上那个是被注释的,完整的是这样的。


$this->load->library('Session/Session'); 这句错了,应该是 $this->load->library('session'); 看手册里的例子,不要自己猜测。

其他代码我看没问题

本版积分规则