我这里有这个代码,应该验证会员登录信息
由于某些原因,即使未设置输入的用户名或密码,$this->error['warning']
也会同时显示。
问题如何确保我的密码和用户名未设置为不运行第二次检查,直到两者都完成。
public function validate() {
/* First Checks */
if (!$this->input->post('username')) {
$this->error['username'] = 'You have missed the username input!';
}
if (!$this->input->post('password')) {
$this->error['password'] = 'You have missed password input!';
}
/* Second Checks */
if (($this->input->post('password') != '') && ($this->input->post('username') != '')) {
if (!$this->member->login($this->input->post('password'), $this->input->post('username'))) {
$this->error['warning'] = 'In correct username or password!';
}
if (!$this->member->approved($this->input->post('username'))) {
$this->error['warning'] = 'Your account not approved yet!';
}
}
return !$this->error;
}
完整控制器
<?php
class Login extends CI_Controller {
private $error = array();
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['title'] = 'Members Login';
if ($this->member->getmemberid()) {
redirect(base_url('admin/members/dashboard'));
}
if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
redirect(base_url('admin/members/dashboard'));
}
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
if (isset($this->error['username'])) {
$this->data['error_username'] = $this->error['username'];
} else {
$this->data['error_username'] = '';
}
if (isset($this->error['password'])) {
$this->data['error_password'] = $this->error['password'];
} else {
$this->data['error_password'] = '';
}
if ($this->input->post('username')) {
$this->data['username'] = $this->input->post('username');
} else {
$this->data['username'] = '';
}
if ($this->input->post('password')) {
$this->data['password'] = $this->input->post('password');
} else {
$this->data['password'] = '';
}
$this->load->view('admin/template/common/header', $this->data);
$this->load->view('admin/template/member/login', $this->data);
$this->load->view('admin/template/common/footer');
}
public function validate() {
if (!$this->input->post('username')) {
$this->error['username'] = 'You have missed the username input!';
}
if (!$this->input->post('password')) {
$this->error['password'] = 'You have missed password input!';
}
if (($this->input->post('password') != '') && ($this->input->post('username') != '')) {
if (!$this->member->login($this->input->post('password'), $this->input->post('username'))) {
$this->error['warning'] = 'In correct username or password!';
}
if (!$this->member->approved($this->input->post('username'))) {
$this->error['warning'] = 'Your account not approved yet!';
}
}
return !$this->error;
}
}
答案 0 :(得分:0)
这是一个简单的错误导致买不到
public $data = array();
到目前为止,在上面构造的顶部