如何在codeigniter中查看视图上的错误数据

时间:2014-05-07 06:30:51

标签: php flash codeigniter authentication login

如何在codeigniter中查看视图中的错误....我正在创建登录 codeigniter但是如果用户登录值不等同于数据库表 值。然后我需要显示错误....无效的用户名或 密码..因为我使用set_flashdata但....在我查看时 通过error_message()拨打电话。然后错误显示:

  

调用未定义的函数error_message().....

mylogin auth controller

public function auth() {

        $data['title'] = 'Heart Surgeon';
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_login');

        if ($this->form_validation->run() == TRUE) {

            $Value['admin_username'] = $this->input->post('username');
            $Value['admin_password'] =md5($this->input->post('password'));

            if ($Value != null) {

                $result = $this->user_model->login($Value);
                if($this->session->set_userdata($result) != null){
                    redirect('dashboard');
                }

            }
        } else {
                $this->index();

        }
    }

我的模特

public function login($value) {

        $query = $this->db->get_where('tbl_admin', $value, 1);


        if ($query->num_rows() > 0) {

            $row = $query->row_array();

            $sess_arr = array(
                'admin_user' => $row['admin_username'],
                'adm_key' => $row['admin_key'],
                'admin_type' => $row['admin_type'],
                'admin_id' => $row['admin_id'],
                'admin_logged_in' => TRUE
            );

             $this->session->set_userdata($sess_arr);
        }
        else{

             $this->session->set_userdata(array('msg_type'=>'error'));      
            $this->session->set_flashdata('error', 'Invalid username/password');
       redirect('home');    

        }
    }

我的观点

<?php if (!defined('BASEPATH')) exit('No direct script access
allowed'); ?> <?php $this->load->view('layout/header'); ?> 


<div class="container">

    <div class="row">
        <div class="col-md-4 col-md-offset-4 text-center logo-margin ">
            <img src="<?php echo base_url(); ?>assets/img/logo.png" alt=""/>
        </div>
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-default">                  
                <div class="panel-heading">
                    <h3 class="panel-title">Please Sign In</h3>
                </div>
                <div class="panel-body">
                    <?php if (validation_errors()) {
                        ?>
                        <div class="warning" style="padding: 0px;">
                            <?php echo validation_errors(); ?>
                        </div>
                    <?php
                    }

                    $atts = array(
                        'width' => '650',
                        'height' => '400',
                        'scrollbars' => 'yes',
                        'status' => 'yes',
                        'resizable' => 'yes',
                        'screenx' => '0',
                        'screeny' => '0'
                    );
                    ?>
                    <div><?php  echo error_message();?></div>
                    <?php echo form_open('home/auth'); ?>
                    <fieldset>
                        <div class="form-group">
                            <input class="form-control" placeholder="E-mail" name="username" type="email" autofocus>
                        </div>
                        <div class="form-group">
                            <input class="form-control" placeholder="Password" name="password" type="password" value="">
                        </div>
                        <div class="checkbox">
                            <label>
                                <input name="remember" type="checkbox" value="Remember Me">Remember Me
                            </label>
                        </div>


                        <!-- Change this to a button or input when using this as a form -->
                        <input type="submit" value="login" class="btn btn-lg btn-success btn-block">
                    </fieldset>
                    <?php echo form_close(); ?>
                </div>
            </div>
        </div>
    </div> </div> <?php $this->load->view('layout/footer'); ?>    

2 个答案:

答案 0 :(得分:0)

你调用错误的函数来获取flash错误

尝试在控制器上设置闪存错误: -

$this->session->set_flashdata('error', 'Invalid username/password');

在视图上获取错误flashdata: -

<?php echo $this->session->flashdata('error');?>

更多信息: - http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

答案 1 :(得分:0)

基本上,当我们使用set_flashdata method()设置任何名称值对时,

$this->session->set_flashdata('error', 'Invalid username/password');

一旦使用它将被自动丢弃/从会话中删除 $this->session->flashdata();方法。

我100%同意拉克什。