我正在使用表单验证功能——它目前不允许用户在创建帐户时输入错误的用户名和密码参数。但这意味着将用户弹回注册屏幕。相反,它们被带到主屏幕,如果我使用在进入数据库之前已经使用过的用户名。
注册控制器:
class Signup extends CI_Controller {
function Signup() {
parent::__construct();
$this->load->model('membership');
}
function index() {
$this->load->view('shared/header');
$this->load->view('account/signuptitle');
$this->load->view('account/signupview');
$this->load->view('shared/footer');
}
function register() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
$this->form_validation->set_rules('username', 'Username', 'callback_usernameTaken');
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->form_validation->run()) {
$this->membership->newUser($username, $password);
$this->session->set_userdata('status', 'OK');
$this->session->set_userdata('username', $username);
redirect('home');
}
if ($this->membership->usernameTaken($username)) {
$this->load->view('shared/header');
$this->load->view('account/signuptitle');
$this->load->view('account/signupview');
$this->load->view('shared/footer');
} else {
$this->load->view('shared/header');
$this->load->view('account/signuptitle');
$this->load->view('account/signupview');
$this->load->view('shared/footer');
}
}
}
我认为该if ($this->membership->usernameTaken($username))
语句需要以某种方式安排它可能是一个 else if 语句吗?
会员模式:
class Membership extends CI_Model {
function Membership() {
parent::__construct();
}
function newUser($username, $password) {
$newMember = array('username' => $username,
'password' => $password);
$insert = $this->db->insert('membership', $newMember);
}
function usernameTaken($username) {
$this->db->select('*')->from('membership')->where('username', $username);
$query = $this->db->get();
if ($query->num_rows > 0) {
return true;
} else {
return false;
}
}
再次感谢大家的帮助——我一直在这个网站上查看类似的问题,但我发现它们太混乱了