如何阻止重复条目进入我的会员数据库?

时间:2012-11-22 14:34:37

标签: php codeigniter

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

注册控制器:

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 个答案:

答案 0 :(得分: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;


    }
}

您的模型没问题,请尝试将func更改为此

 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;

}

刚刚在这里写了这个,所以你必须测试它,但我相信它应该没问题。对不起,我不能正确地缩进它,在这里做这件事很痛苦。)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为你每次都在插入。

if (! $this->membership->usernameTaken($username)) 
{
  $this->membership->newUser($username, $password);
}

答案 3 :(得分: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-&gt; form_validation-&gt; run()&amp;&amp;!$ this-&gt; membership&gt; usernameTaken($ username)){

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

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