在代码点火器3中使用form_validation库时,$ _POST超级全局变量不会加载

时间:2015-07-31 08:33:22

标签: php forms codeigniter

Login.php(显示输入用户名和密码字段的登录表单)

<?php echo validation_errors(); ?>
<form action="http://www.parthfolder.com/admin/login" method="post">
<h5>Username</h5>
<input type="text" name="adminuser" value="" size="50" />
<!--
<h5>Password</h5>
<input type="password" name="password" value="" size="50" />

<h5>Confirm Password </h5>
<input type="password" name="passconf" value="" size="50" />-->

<h5>Password </h5>
<input type="password" name="password" value="" size="50" />

<div><input type="submit" value="submit" /></div>
</form> 

Admin.php(控制器)

<?php 
    class Admin extends CI_Controller{
        public function __construct(){
            parent::__construct();
            $this->load->model('admin_model');
            $this->load->helper(array('form','url'));
            $this->load->library('session');
        }
        public function login(){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('adminuser','Username','required');
            $this->form_validation->set_rules('password','Password','required');

            if($this->form_validation->run()==FALSE){           
                $this->load->view('admin/login');
            }
            else{
                redirect('admin/success');
            }

        }
        public function success(){
            echo $_POST['adminuser'];
            echo $_POST['password'];
        }
        public function home(){
            if(isset($_SESSION['adminuser'])){
                redirect('admin/login');
            }
            else{
                echo "set";
            }
        }
    }
?>

我的问题是当我注释掉与form_validation库相关的所有语法时,就像这样

的login.php

//<?php echo validation_errors(); ?>
<form action="http://www.parthfolder.com/admin/success" method="post">
<h5>Username</h5>
<input type="text" name="adminuser" value="" size="50" />
<!--
<h5>Password</h5>
<input type="password" name="password" value="" size="50" />

<h5>Confirm Password </h5>
<input type="password" name="passconf" value="" size="50" />-->

<h5>Password </h5>
<input type="password" name="password" value="" size="50" />

<div><input type="submit" value="submit" /></div>
</form> 

注意: - 我更改了Login.php中的操作链接

admin.php的

public function login(){
            //$this->load->library('form_validation');
            //$this->form_validation->set_rules('adminuser','Username','required');
            //$this->form_validation->set_rules('password','Password','required');

            //if($this->form_validation->run()==FALSE){         
                $this->load->view('admin/login');
            //}
            //else{
                //redirect('admin/success');
            //}

        } 

我得到了理想的行为。表单已提交,控制器管理员的功能成功使用$ _POST []超全局回显用户名和密码。

但是,当我使用form_validation库(原始的未注释代码)时,我收到警告,指出需要用户名和密码字段。但是当我提供正确的输入时,我会收到错误消息

A PHP Error was encountered

Severity: Notice

Message: Undefined index: adminuser

Filename: controllers/Admin.php

Line Number: 23

Backtrace:

File: /usr/local/apache2/htdocs/parth/application/controllers/Admin.php
Line: 23
Function: _error_handler

File: /usr/local/apache2/htdocs/parth/index.php
Line: 292
Function: require_once


A PHP Error was encountered

Severity: Notice

Message: Undefined index: password

Filename: controllers/Admin.php

Line Number: 24

Backtrace:

File: /usr/local/apache2/htdocs/parth/application/controllers/Admin.php
Line: 24
Function: _error_handler

File: /usr/local/apache2/htdocs/parth/index.php
Line: 292
Function: require_once

这种行为背后的原因是什么?感谢..

1 个答案:

答案 0 :(得分:0)

问题在于重定向。 $ _POST仅在帖子后可用。重定向后删除var。

要访问用户名和密码,您应将这些值保存在会话var中。 但不建议在任何地方保存密码=&gt;不安全!!

http://www.codeigniter.com/userguide3/libraries/sessions.html

所以看起来应该是这样的:

<?php 
    class Admin extends CI_Controller{
        public function __construct(){
            parent::__construct();
            $this->load->model('admin_model');
            $this->load->helper(array('form','url'));
            $this->load->library('session');
        }
        public function login(){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('adminuser','Username','required');
            $this->form_validation->set_rules('password','Password','required');

            if($this->form_validation->run()==FALSE){           
                $this->load->view('admin/login');
            }
            else{
                $this->session->set_userdata('adminuser', $this->input->post('adminuser'));
                $this->session->set_userdata('password', $this->input->post('password'));  // not recommended! => insecure
                redirect('admin/success');
            }

        }
        public function success(){
             echo $this->session->userdata('adminuser');
             echo $this->session->userdata('password');
        }
        public function home(){
            if(isset($_SESSION['adminuser'])){
                redirect('admin/login');
            }
            else{
                echo "set";
            }
        }
    }
?>