ds342222 2011-05-14 20:01
浏览 25

注册时无法正确使用用户名验证

I'm following http://www.youtube.com/watch?v=ua1pWx8WjFM when creating registration. For a really long time I tried to find the reason why I can register any number of the same username while code and everything else looks the same and works correctly in video.

My model "user_model":

    function register_user($username, $password, $name, $email)
{
    $sha1_password = sha1($password);

    $query_str = "INSERT INTO registration (username, password, name, email) VALUES (?, ?, ?, ?)";

    $this->db->query($query_str, array($username, $sha1_password, $name, $email));

}


function username_check($username)
{

    $query_str = "SELECT username from registration where username = ?";

    $result = $this->db->query($query_str, $username);

    if ($result->num_rows() > 0)
    {
        //username exists
        return true;
    }
    else
    {
        //username doesn't exist
        return false;
    }
}

}

controller "user.php":

function User()
{
    parent::__construct();  

    $this->load->model('User_model');
}

function index()
{   
    //$this->load->model('User_model');
    $this->register();
}

function register()
{   
    //$this->load->model('User_model');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean|callback_username_not_exists');
    $this->form_validation->set_rules('name', 'Name', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|min_length[6]|xss_clean|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
    $this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|alpha_numeric|min_length[6]|matches[password]|xss_clean');


    if ($this->form_validation->run() == FALSE)
    {
        //$this->load->model('User_model');
        $this->view_data['page_title'] = 'Welcome to a Tutorial';
        $this->view_data['page_content'] = 'view_login_form';
        $this->load->view('view_register', $this->view_data);       //errors
    }
    else
    {
        $username = $this->input->post('username');
        $name = $this->input->post('name');
        $password = $this->input->post('password');
        $email = $this->input->post('email');

    //  $this->load->model('User_model');
        $this->User_model->register_user($username, $password, $name, $email);
    }

    function username_check($username)
    {

        $this->form_validation->set_message('username_check', 'That %s already exists');

        if ($this->User_model->check_username($username))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

} }

So, what's wrong here?

  • 写回答

1条回答 默认 最新

  • dsf22567 2011-05-14 20:48
    关注

    It's pretty obvious really; probably an oversight in the video.

    You've set this validation rule for your username field:

    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean|callback_username_not_exists');

    The callback rule is callback_username_not_exists

    Your callback function is called username_check. It's never being called as the name does not match.

    评论

报告相同问题?

悬赏问题

  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 为什么我安装了Homebrew之后,无法像官网的那样通过执行命令安装Watchman
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表