1

我正在尝试使用 CodeIgniter 构建注册系统。我有一个名为 User 的控制器,代码如下:

class User extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
    }
    public function index()
    {
        if(($this->session->userdata('user_name')!=""))
        {
            $this->welcome();
        }
        else{
            $data['title']= 'Home';
            $this->load->view('header_view',$data);
            $this->load->view("registration_view.php", $data);
            $this->load->view('footer_view',$data);
        }
    }
    public function welcome()
    {
        $data['title']= 'Welcome';
        $this->load->view('header_view',$data);
        $this->load->view('welcome_view.php', $data);
        $this->load->view('footer_view',$data);
    }
    public function login()
    {
        $email=$this->input->post('email');
        $password=md5($this->input->post('pass'));

        $result=$this->user_model->login($email,$password);
        if($result) $this->welcome();
        else        $this->index();
    }
    public function thank()
    {
        $data['title']= 'Thank';
        $this->load->view('header_view',$data);
        $this->load->view('thank_view.php', $data);
        $this->load->view('footer_view',$data);
    }
    public function registration()
    {
        $this->load->library('form_validation');
        // field name, error message, validation rules
        $this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
        $this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');

        if($this->form_validation->run() == FALSE)
        {
            $this->index();
        }
        else
        {
            $this->user_model->add_user();
            $this->thank();
        }
    }
    public function logout()
    {
        $newdata = array(
        'user_id'   =>'',
        'user_name'  =>'',
        'user_email'     => '',
        'logged_in' => FALSE,
        );
        $this->session->unset_userdata($newdata );
        $this->session->sess_destroy();
        $this->index();
    }
}

到目前为止,一切都很好。如果我进入注册页面,我会显示注册表。如果我发送表单并且它通过了表单验证检查,我会得到成功页面,如果表单有错误,我会返回带有一些错误消息的表单。

现在我想做的是数据库的东西。我对如何将注册表单中的 POST 值获取到我的数据库有一些想法,但不知道如何检查用户名或电子邮件是否已经存在,如果存在,则在注册表单上显示该错误。当我在表单中注册时,如何将激活发送到用户电子邮件到我网站中的活动帐户。

这是我的注册表单视图:

<div id="content">
<div class="signup_wrap">
<div class="signin_form">
    <?php echo form_open("user/login"); ?>
        <label for="email">Email:</label>
        <input type="text" id="email" name="email" value="" />
        <label for="pass">Password:</label>
        <input type="password" id="pass" name="pass" value="" />
        <input type="submit" class="" value="Sign in" />
    <?php echo form_close(); ?>
</div><!--<div class="signin_form">-->
</div><!--<div class="signup_wrap">-->
<div class="reg_form">
<div class="form_title">Sign Up</div>
<div class="form_sub_title">It's free and anyone can join</div>
<?php echo validation_errors('<p class="error">'); ?>
    <?php echo form_open("user/registration"); ?>
        <p>
            <label for="user_name">User Name:</label>
            <input type="text" id="user_name" name="user_name" value="<?php echo set_value('user_name'); ?>" />
        </p>        
        <p>
            <label for="email_address">Your Email:</label>
            <input type="text" id="email_address" name="email_address" value="<?php echo set_value('email_address'); ?>" />
        </p>
        <p>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" value="<?php echo set_value('password'); ?>" />
        </p>
        <p>
            <label for="con_password">Confirm Password:</label>
            <input type="password" id="con_password" name="con_password" value="<?php echo set_value('con_password'); ?>" />
        </p>        
        <p>
            <input type="submit" class="greenButton" value="Submit" />
        </p>
    <?php echo form_close(); ?>
</div><!--<div class="reg_form">-->    
</div><!--<div id="content">-->

和模块是: -

class User_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }
    function login($email,$password)
    {
        $this->db->where("email",$email);
        $this->db->where("password",$password);

        $query=$this->db->get("user");
        if($query->num_rows()>0)
        {
            foreach($query->result() as $rows)
            {
                //add all data to session
                $newdata = array(
                        'user_id'       => $rows->id,
                        'user_name'     => $rows->username,
                        'user_email'    => $rows->email,
                        'logged_in'     => TRUE,
                   );
            }
                $this->session->set_userdata($newdata);
                return true;            
        }
        return false;
    }
    public function add_user()
    {
        $data=array(
            'username'=>$this->input->post('user_name'),
            'email'=>$this->input->post('email_address'),
            'password'=>md5($this->input->post('password'))
            );
        $this->db->insert('user',$data);
    }
}
4

1 回答 1

1

改变:

$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');

$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|is_unique[users.user_name]');

$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|min_length[4]|valid_email|is_unique[users.email_address]');

如果您Form_validation.php没有is_unique()功能,请添加:

public function is_unique($str, $field)
{
    list($table, $field) = explode('.', $field);

    if (isset($this->CI->db))
    {
        $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
        return $query->num_rows() === 0;
    }

    return FALSE;
}
于 2013-08-16T08:12:17.020 回答