728x90
등록 양식을 만들려면 사용자가 강력한 암호를 가지고 있는지 확인해야 합니다.
Form Validation Library 및 REGEX를 사용하여 CodeIgniter로 강력한 암호 유효성 검사를 쉽게 만들 수 있습니다.
예를 들면, 비밀번호의 최소 및 최대 길이를 설정합니다.
소문자, 대문자, 숫자 및 특수 문자를 포함해야 합니다.
비밀번호 필드에 입력한 데이터가 비밀번호 확인 필드와 같아야 하는지도 쉽게 확인할 수 있습니다.
비밀번호에서 중요한 사항은 다음과 같습니다.
- 필수
- 소문자
- 대문자
- 숫자
- 특수문자
Controller
application/controllers/PasswordController.php 파일을 만들고 아래 코드를 입력합니다.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class PasswordController extends CI_Controller
{
public function __construct()
{
// CodeIgniter 기본 생성자
parent::__construct();
// session 라이브러리를 호출합니다.
$this->load->library('session');
}
public function index()
{
// 배열을 이용한 검증 규칙 설정 : 여러 검사 규칙을 한번에 처리할 수 있습니다.(배열의 키 이름은 반드시 아래와 같아야 합니다.)
$rules = array(
[
'field' => 'email',
'label' => 'Email',
'rules' => 'required' // 필수
],
[
'field' => 'new_password',
'label' => 'New Password',
'rules' => 'callback_valid_password' // 콜백함수 기능
],
[
'field' => 'confirm_password',
'label' => 'Confirm Password',
'rules' => 'matches[new_password]' // new_password랑 일치하는지 확인
]
);
// 검사 규칙을 설정하기 위해서 set_rules 함수를 사용합니다.
$this->form_validation->set_rules($rules);
// run() 함수를 호출하면 검증규칙파일을 자동으로 로드합니다.
if($this->form_validation->run() == FALSE)
{
// 폼 데이터를 전송받지 않은 경우
$this->load->view('PasswordValidation');
}
else
{
// 폼 데이터를 전송받은 경우
$this->session->set_flashdata('success','축하합니다.');
// PasswordController/index 페이지로 리다이렉트합니다.
redirect(base_url('PasswordController/index'));
}
}
// Create strong password
// 위에서 new_password로 콜백 지정한 함수
public function valid_password($password = "")
{
// 좌우 여백 제거
$password = trim($password);
// 정규표현식
$regex_lowercase = '/[a-z]/';
$regex_uppercase = '/[A-Z]/';
$regex_number = '/[0-9]/';
$regex_special = '/[!@#$%^&*()\-_=+{};:,<.>ยง~]/';
// 비밀번호가 빈 값일때
if(empty($password))
{
// 에러메시지 설정('rule', 'Error Message')
$this->form_validation->set_message('valid_password', '{field} 필드는 필수입니다.');
return FALSE;
}
// 비밀번호에 대문자가 하나도 없는 경우
if(preg_match_all($regex_lowercase, $password) < 1)
{
$this->form_validation->set_message('valid_password', '{field} 필드는 하나 이상의 소문자가 있어야 합니다.');
return FALSE;
}
// 비밀번호에 소문자가 하나도 없는 경우
if(preg_match_all($regex_uppercase, $password) < 1)
{
$this->form_validation->set_message('valid_password', '{field} 필드는 하나 이상의 대문자가 있어야 합니다.');
return FALSE;
}
// 비밀번호에 숫자가 하나도 없는 경우
if(preg_match_all($regex_number, $password) < 1)
{
$this->form_validation->set_message('valid_password', '{field} 필드는 하나 이상의 숫자가 있어야 합니다.');
return FALSE;
}
// 비밀번호에 특수문자가 하나도 없는 경우
if(preg_match_all($regex_special, $password) < 1)
{
$this->form_validation->set_message('valid_password', '{field} 필드는 하나 이상의 특수문자가 있어야 합니다..' . ' ' . htmlentities('!@#$%^&*()\-_=+{};:,<.>ยง~'));
return FALSE;
}
// 비밀번호가 5자리 미만인 경우
if(strlen($password) < 5)
{
$this->form_validation->set_message('valid_password', '{field} 필드의 길이는 5자 이상이어야 합니다.');
return FALSE;
}
// 비밀번호가 32자 초과하는 경우
if(strlen($password) > 32)
{
$this->form_validation->set_message('valid_password', '{field} 필드의 길이는 32자를 초과할 수 없습니다.');
return FALSE;
}
return TRUE;
}
// storong password end
}
?>
View
application/views/PasswordValidation.php 파일을 만들고 아래 코드를 입력합니다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Strong Password Validation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-9 offset-md-1">
<div class="user_about_content_box">
<div class="tab-pane">
<h3>Strong Password Validation</h3>
</div>
<div class="col-md-8">
<?php
if($this->session->flashdata('success'))
{
echo '<span class="text-success" style="font-weight:bold">'.$this->session->flashdata('success').'</span>';
}
?>
</div>
<form method="post" action="<?php echo base_url('passwordcontroller/index'); ?>">
<div class="col-md-8">
<div class="form-group" id="prime_cat">
<input type="email" value="<?php echo set_value('email'); ?>" name="email" class="form-control input-group-lg" placeholder="Email">
</div>
<?php
if(form_error('email'))
{
echo '<span style="color:red">'.form_error('email').'</span>';
}
?>
<div class="form-group" id="prime_cat">
<input type="text" value="<?php echo set_value('new_password'); ?>" name="new_password" class="form-control input-group-lg" placeholder="New Password">
</div>
<?php
if(form_error('new_password'))
{
echo "<span style='color:red'>".form_error('new_password')."</span>";
}
?>
<div class="form-group" id="prime_cat">
<input type="password" value="<?php echo set_value('confirm_password'); ?>" name="confirm_password" class="form-control input-group-lg" placeholder="Confirm Password">
</div>
<?php
if(form_error('confirm_password'))
{
echo "<span style='color:red'>".form_error('confirm_password')."</span>";
}
?>
<div class="form-group col-md-12">
<input class="btn btn-primary" type="submit" value="Create account">
</div>
</div>
</form>
</div> <!--Content box ends-->
</div>
</div>
</div>
</body>
</html>
아래 URL로 이동합니다.
localhost/passwordcontroller/index
패스워드를 잘못 입력한 경우
패스워드를 정상적으로 입력한 경우
Github : https://github.com/jun0925/study/commit/aaf4c190f7370347949d3cc6def04d4811e3fe10
728x90
'CodeIgniter3' 카테고리의 다른 글
22. [CodeIgniter3] MySQL에서 CSV파일 내보내기 (0) | 2022.01.06 |
---|---|
21. [CodeIgniter3] CSV데이터를 MySQL로 가져오기 (0) | 2022.01.06 |
19. [CodeIgniter3] jQuery Ajax를 이용하여 데이터 조회하기 (0) | 2022.01.05 |
18. [CodeIgniter3] jQuery Ajax를 사용하여 데이터 추가 (0) | 2022.01.05 |
17. [CodeIgniter3] URL 라우팅 & CRUD-Blog 예제 (0) | 2022.01.04 |