0

我正在使用表单验证功能——它目前不允许用户在创建帐户时输入错误的用户名和密码参数。但这意味着将用户弹回注册屏幕。相反,它们被带到主屏幕,如果我使用在进入数据库之前已经使用过的用户名。

注册控制器:

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;
        }
    }

再次感谢大家的帮助——我一直在这个网站上查看类似的问题,但我发现它们太混乱了

4

4 回答 4

2

我会说将这些视图放在模板中,然后您可以加载该模板。它使事情看起来更整洁。您的索引函数应如下所示

 function index() {
    $data['page_title'] = 'Sign up page';
    $data['central_content'] = "sign_up";
    $this->load->view('template');  
}

在您的模板中不要忘记加载内容,就像这样;

<?php $this->load->view($central_content);  ?>

重新排列您的注册功能,如下所示;

function register() {
    #you should be autoloading these helpers tbh

    $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');




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

    $username = $this->input->post('username');
    $password = $this->input->post('password');
    #Learn to use flashdata, it helps.

    if($this->membership->newUser($username, $password)){
       $this->session->set_userdata('status', 'OK');
       $this->session->set_userdata('username', $username);
       redirect('home');
    }

    }else{

     $this->index();
     }

}

#You specified a callback but there's no callback function in your class? Here's how you go about it.
Public function usernameTaken(){
 #callbacks should return true or false
    if ($this->membership->usernameTaken($username)) {
    return true;
    } else {
#You can set your own validation message in the case it's false.
$this->form_validation->set_message('usernameTaken', 'The selected username already exists');
return false;


    }
}

你的模型没问题,试试把函数改成这个

 function newUser($username, $password) {
      $newMember = array('username' => $username,
      'password' => $password);
      return ($this->db->insert('membership', $newMember)) ? true : false;
}
function usernameTaken($username) {
   #your query, requires a select * in a case where it's clearly not needed. Little things like this slow down your query.Try,
    $query = $this->db->where('username', $username)->get('membership');
    #Also num_rows()
    return ($query->num_rows() > 0) ? false : true;

}

刚写在这里,所以你必须测试一下,但我相信它应该没问题。抱歉,我无法正确缩进,在这里做这件事很痛苦:)

于 2012-11-22T15:33:12.650 回答
0

我相信当注册函数运行时,只有这部分代码在重定向之前被执行。

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->form_validation->run() && !$this->membership->usernameTaken($username)) {

    $this->membership->newUser($username, $password);
    $this->session->set_userdata('status', 'OK');
    $this->session->set_userdata('username', $username);
    redirect('home');
    }

这样它就不会重定向并继续执行它下面的行。

于 2012-11-22T14:51:34.010 回答
0

表单验证可能无法访问模型。尝试移动usernameTaken($username)到控制器。

于 2012-11-22T14:42:15.273 回答
0

我想你每次都在插入。

if (! $this->membership->usernameTaken($username)) 
{
  $this->membership->newUser($username, $password);
}
于 2012-11-22T14:49:55.290 回答