dongsilu2237 2018-09-05 19:05
浏览 44
已采纳

无法访问错误登录表单codeigniter

I am creating a login form, I wrote everything from documentation and from other resources. When I try to login I am getting an error of Unable to access an error message corresponding to your field name Username.(xss_clean) and Unable to access an error message corresponding to your field name Password.(xss_clean)

I checked my database from phpMyAdmin, database is already there. I checked my connection of databse, it's also connected. I checked then table admin it's also there and coded in model.

Can you guys help me, what is wrong I did here?

Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class LoginCheck extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->database();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->model('admin');
    }

    function index() {
        //This method will have the credentials validation
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

        $this->form_validation->set_error_delimiters('<br /><div class="form-error">', '</span>');

    if($this->form_validation->run() == FALSE) {
        //Field validation failed.  User redirected to login page
        $this->load->view('index');
    }

    else {
        //Go to private area
        echo "Private area";
        //redirect('listing', 'refresh');
    }
}

    function check_database($password) {
        //Field validation succeeded.  Validate against database
        $username = $this->input->post('username');
        //query the database
        $result = $this->admin->login($username, $password);
        //print_r($result);

    if($result) {
        $sess_array = array();
        foreach($result as $row) {
            $sess_array = array(
            'id' => $row->id,
            'username' => $row->username
            );
            $this->session->set_userdata('logged_in', $sess_array);
        }
        return TRUE;
    }
        else {
            $this->form_validation->set_message('check_database', 'Invalid username or password');
            return false;
        }
    }
}

?>

View

<?php
$attributes = array('class' => 'login100-form validate-form flex-sb flex-w');
echo form_open('index.php/logincheck', $attributes);
?>
<span class="login100-form-title p-b-51">
<img src="<?php echo site_url('login_assets/img/logo.png')?>">
</span>
<div class="wrap-input100 validate-input m-b-16" data-validate = "Username is required">
<?php echo form_error('username'); ?>
<input class="input100" type="text" name="username" value="<?php echo set_value('username'); ?>" placeholder="Username">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input m-b-16" data-validate = "Password is required">
<?php echo form_error('password'); ?>
<input class="input100" type="password" name="password" value="<?php echo set_value('password'); ?>" placeholder="Password">
<span class="focus-input100"></span>
</div>
<div class="container-login100-form-btn m-t-17">
<?php 
$attributes = array('class' => 'login100-form-btn');
echo form_submit('submit', 'Login', $attributes);
?>
</div>
<?php echo form_close(); ?>
  • 写回答

1条回答 默认 最新

  • dongzai0020 2018-09-05 19:29
    关注

    It appears that your original validation rules had a tiny mistake. In CI, the final validation rule shouldn't be followed by a pipe |. The pipe should only be used as a delimiter in this case.

    Try changing your validation rules to remove the trailing pipe:

    $this->form_validation->set_rules('username', 'Username', 'rule1|rule2|rule3');
    $this->form_validation->set_rules('password', 'Password', 'rule1|rule2');
    

    Also, note that your form is pointed to the LoginCheck class, but it's not pointed to any specific function/method. Even though it may default to index() the form should point to the specific class/method so you should use

    echo form_open('index.php/logincheck/index', $attributes);

    Finally, your updated rules include xss_clean, which was removed from CI's form validation library and required the security helper to be loaded. You may get around the error by loading the security helper in the construct, but don't forget that XSS filtering is supposed to be done on output, not input (which is why it was removed from the form validation lib)

    You may read a bit about this last part HERE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题