[英]CodeIgniter Validation Rules for Email and Password
I was used to coding application with CodeIgniter and I'm a total newbie. 我习惯用CodeIgniter编写应用程序,我是一个新手。 I just started to learn CodeIgniter 3.0, and and reached to validation rules. 我刚刚开始学习CodeIgniter 3.0,并且达到了验证规则。
I notice that xss_clean
is now gone from the validation class, so what rules should I use in the validation of email and password? 我注意到xss_clean
现在已经从验证类中消失了,那么我应该在验证电子邮件和密码时使用哪些规则? Using just trim
, valid_email
, and required is enough for security? 只使用trim
, valid_email
和required是否足够安全?
Sorry if that question has been asked, but I searched around and I see old topics where people is using xss_clean
. 很抱歉,如果问过这个问题,但我搜索了一下,我看到人们正在使用xss_clean
旧主题。
Include Email Helper: 包括电子邮件助手:
$this->load->helper('email');
For Email: 电子邮件:
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
Or you can also use PHP Filter for email validation as 或者您也可以使用PHP过滤器进行电子邮件验证
filter_var($email, FILTER_VALIDATE_EMAIL);
For Password Expression 用于密码表达
public function chk_password_expression($str)
{
if (1 !== preg_match("/^.*(?=.{6,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $str))
{
$this->form_validation->set_message('chk_password_expression', '%s must be at least 6 characters and must contain at least one lower case letter, one upper case letter and one digit');
return FALSE;
}
else
{
return TRUE;
}
}
To call the function you should use: 要调用该函数,您应该使用:
$this->form_validation->set_rules( 'password', 'Password', 'trim|required|min_length[6]|max_length[15]|callback_chk_password_expression');
Note: chk_password_expression should be in same class controller or in parent class. 注意:chk_password_expression应该位于同一个类控制器或父类中。 Email helper should be included as $this->load->helper('email'); 电子邮件助手应该包含在$ this-> load-> helper('email');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.