我使用Codeigniter 3制作了注册和登录应用程序。
注册表单验证确保users表中的每个电子邮件都是唯一,如下图所示:
而不是&#34;电子邮件字段必须包含唯一值&#34;在电子邮件字段下方,我想要一条Flash消息让用户知道提供的电子邮件已经存在,并且他/她应签名而不是注册:< / p>
我在 if语句中使用了$email_exists
函数,但函数本身不存在尚未。
我想在is_unique
函数中使用CI $email_exists
。我怎么能这样做?
这是实现所需功能的好方法吗?
我的signup()
函数如下所示:
public function signup()
{
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$verification_key = md5($email);
$active = 0;
$this->load->model('Usermodel');
// Create account
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $date_created, $date_updated, $verification_key, $active)) {
$this->session->set_flashdata("signup_sucess", "Your account was created. Your have to activate it before you can signin. We have send you an activation email at $email.");
} else {
$this->session->set_flashdata("signup_failure", "We ware unable to create your account");
}
redirect('signup');
} else {
$this->load->view('signup');
if ($email_exists) {
$this->session->set_flashdata("email_exists", "The email address you provided already exists. Please signup.");
}
}
}
答案 0 :(得分:1)
如果您坚持使用验证功能,您可以轻松地调用它:
$this->form_validation->is_unique($email, 'users.email')
由于is_unique
函数需要第一个参数是匹配的字符串,第二个参数是table
。field
根据源文件文档。
完整代码:
public function signup()
{
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$email = $this->input->post('email');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$password = $this->input->post('password');
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$verification_key = md5($email);
$active = 0;
$this->load->model('Usermodel');
// Create account
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $date_created, $date_updated, $verification_key, $active)) {
$this->session->set_flashdata("signup_sucess", "Your account was created. Your have to activate it before you can signin. We have send you an activation email at $email.");
} else {
$this->session->set_flashdata("signup_failure", "We ware unable to create your account");
}
redirect('signup');
} else {
if (!$this->form_validation->is_unique($email, 'users.email')) {
$this->session->set_flashdata("email_exists", "The email address you provided already exists. Please signin.");
redirect('signin');
}
$this->load->view('signup');
}
}
答案 1 :(得分:0)
Ins {$this->load->view('signup');
使用redirect('controller/method')
并在重定向$this->session->set_flashdata("signup_failure", "Email alredy exist");
之前设置您的Flash消息
替换您的以下其他条件
else {
$this->load->view('signup');
}
使用
else {
$this->session->set_flashdata("signup_failure", "Email address <?php echo $this->input->post('email'); ?> alredy exist in our database");
redirect('signup');
}
答案 2 :(得分:0)
这就是我要找的:如果注册详情提供且有效,请注册新用户,除非电子邮件地址他/她提供已经存在于数据库中,在这种情况下,而不是显示验证错误(记住:所有数据,包含的电子邮件 有效),敦促用户登录和重定向到登录页面。
注册用户John Doe,已注册:
您被重定向到登录页面并“邀请”登录。
这是代码:
在Usermodel.php
文件中:
public function email_exists($email)
{
$query = $this->db->get_where('users', ['email' => $email]);
return $query->num_rows() > 0;
}
在Signup.php
文件中:
public function signup()
{
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$verification_key = md5($email);
$active = 0;
// Load user model
$this->load->model('Usermodel');
// If email does not already exist in the database
// signup new user
if (!$this->Usermodel->email_exists($email)) {
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $date_created, $date_updated, $verification_key, $active)) {
$this->session->set_flashdata("signup_sucess", "Your account was created. Your have to activate it before you can signin. We have send you an activation email at $email.");
} else {
// unless sigup does not fail for whatever reason
$this->session->set_flashdata("signup_failure", "We ware unable to create your account");
}
redirect('signup');
} else {
// If email is already in the database
// urge user to sign in (redirect to signup page too)
$this->session->set_flashdata("email_exists", "The email address $email already exists in the database. Please signin.");
redirect('signin');
}
} else {
$this->load->view('signup');
}
}
非常感谢所有有帮助的人!
答案 3 :(得分:-1)
public function signup()
{
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', );
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]',
array('required' => 'Email is Required.'),
array('is_unique' => 'Email Already exist.')
);
$this->form_validation->set_rules('password', 'Password', 'required|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run() != FALSE)
{
$email = $this->input->post('email');
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$password = $this->input->post('password');
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$verification_key = md5($email);
$active = 0;
$this->load->model('Usermodel');
// Create account
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $date_created, $date_updated ,$verification_key , $active))
{
// Create a view for Check Email in View.
$this->load->view('Check_Email_view');
}
else
{
$signup_failure="Something Went Wrong Please Sign Up again.";
$this->load->view('signup',$signup_failure, true);
// in view Use Isset($signup_failure)
}
}
else
{
$this->load->view('signup');
}
}