显示无效的电子邮件和密码错误消息

时间:2018-06-29 09:28:44

标签: php codeigniter

视图中: login.php

我在两个位置使用此视图文件,一个用于用户登录,另一个用于显示无效的用户名和密码消息。下面我提到了一些代码,但它不起作用。给我有关这个问题的任何建议。

 <div class="login-content">
/*
message to be displayed here:
 i try this one:<?php if(!(authentication($email,$password) == TRUE)){                          
echo "Invalid email address or password";
               }?>
  */
    <form action="<?php echo site_url('auth/check');?>" method="POST" class="margin-bottom-0" data-parsley-validate="true" onsubmit = "return Validate();" >
     <div class="form-group m-b-15">
    <input type="text" name="txtemail" id="txtemail" class="form-control input-lg" placeholder="<?php echo $language['Email'];?>" data-parsley-required='true' maxlength="95"/>
                            </div>
     <div class="form-group m-b-15">
    <input type="password" name="txtpassword" id="txtpassword" class="form-control input-lg" placeholder="<?php echo $language['Password'];?>" data-parsley-required='true' maxlength="95"/>
     </div>

在控制器中: Auth.php

这是function,用于检查用户验证。我想在function条件下使用此else显示一条消息。

public function check(){

    $email      = $this->input->post('txtemail');
    $password   = $this->encrypt($this->input->post('txtpassword'));
    $this->load->model('cmsuser_model');
    $user = $this->cmsuser_model->authentication($email,$password);

    if(!is_null($user))
    {
        $this->session->set_userdata('user', $user);
        $this->session->set_userdata('profile_image',$user->image);
        $this->session->set_userdata('is_manager','0');


        $this->load->model('association_model');
        $table = '';
        $limit = '';
        $order_by = '';
        $group_by = '';
        $start = '';
        $fields = '';
        $where = array();
        $where['created_by'] = $this->session->userdata['user']->cmsuid;
        $dt = $this->association_model->get_all($fields, $where, $table, $limit, $order_by, $group_by, $start);
        $this->session->set_userdata('ass_id', $dt[0]['ass_id']); 

        redirect(base_url('index.php/welcome'), 'refresh');
    }
    else{
  //Browser redirect to this else condition but i am not able to display message in `view(login.php)` file.
    redirect(base_url('index.php/auth/login'), 'refresh');
        }
    exit;
}

1 个答案:

答案 0 :(得分:1)

public function check(){

    $email      = $this->input->post('txtemail');
    $password   = $this->encrypt($this->input->post('txtpassword'));
    $this->load->model('cmsuser_model');
    $user = $this->cmsuser_model->authentication($email,$password);

    if(!is_null($user))
    {
        $this->session->set_userdata('user', $user);
        $this->session->set_userdata('profile_image',$user->image);
        $this->session->set_userdata('is_manager','0');


        $this->load->model('association_model');
        $table = '';
        $limit = '';
        $order_by = '';
        $group_by = '';
        $start = '';
        $fields = '';
        $where = array();
        $where['created_by'] = $this->session->userdata['user']->cmsuid;
        $dt = $this->association_model->get_all($fields, $where, $table, $limit, $order_by, $group_by, $start);
        $this->session->set_userdata('ass_id', $dt[0]['ass_id']); 

        redirect(base_url('index.php/welcome'), 'refresh');
    }
    else{
      $url=echo base_url('login.php').'?status=err';
      redirect($url, 'refresh');
//this will redirect you to the login page with the parameter status in the url
        }
    exit;
}

login.php中的查看文件

<?php
   $satatus = $_GET['status'];
   if($status == 'err'){
?>
//add this div right above the login section
<div>Wrong Username/Password !!</div>
    <div class="login-content">
/*
message to be displayed here:
 i try this one:<?php if(!(authentication($email,$password) == TRUE)){                          
echo "Invalid email address or password";
               }?>
  */
    <form action="<?php echo site_url('auth/check');?>" method="POST" class="margin-bottom-0" data-parsley-validate="true" onsubmit = "return Validate();" >
     <div class="form-group m-b-15">
    <input type="text" name="txtemail" id="txtemail" class="form-control input-lg" placeholder="<?php echo $language['Email'];?>" data-parsley-required='true' maxlength="95"/>
                            </div>
     <div class="form-group m-b-15">
    <input type="password" name="txtpassword" id="txtpassword" class="form-control input-lg" placeholder="<?php echo $language['Password'];?>" data-parsley-required='true' maxlength="95"/>
     </div>
<?php
   }
?>