[함수] php5 폼 전송 값 및 문자 체크하기 특수문자 체크 > PHP

본문 바로가기
사이트 내 전체검색

PHP

[함수] php5 폼 전송 값 및 문자 체크하기 특수문자 체크

페이지 정보

profile_image
작성자 최고관리자
댓글 0건 조회 5,887회 작성일 21-02-09 11:52

본문

# 특수문자 체크하기  아스키값으로 체크


public function isEtcString($allow)
 {
  # 허용된 특수문자 키
  $allowArgs = array();
  $tmpArgs = (!empty($allow)) ? explode(',',$allow) : '';
  if(is_array($tmpArgs))
  {
                  foreach($tmpArgs as $k => $v){
                                  $knumber = Ord($v);
                                  $allowArgs['s'.$knumber] = $v;
                  }
  }
 
  $result = false;
  for($i=0; $i<$this->len; $i++)
  {
                  $asciiNumber = Ord($this->str[$i]);
                  if(array_key_exists('s'.$asciiNumber, $allowArgs) === false)
                  {
                                  if( ($asciiNumber<48) && ($asciiNumber != 32) ){ $result = true; break; }
                                  else if($asciiNumber>57 && $asciiNumber<65){ $result = true; break; }
                                  else if($asciiNumber>90 && $asciiNumber<97){ $result = true; break; }
                                  else if($asciiNumber>122 && $asciiNumber<128){ $result = true; break; }
                  }
  }
  return $result;
 }



원소스 :
http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=71521

아래는 모든 내용

----------------------------------------------------------------------------------
아스키 코드 값을 통해 구현했습니다
 정규식 쓰면 더 간단하게 코딩이 되겠지만
 아무리 테스트 해봐도

 정규식 보다 아스키체크가 더 빠르더라고요
 그래서 아스키 코드로만 체크했음당


 아무튼
 폼값으로 전송된 값을 체크 하기 위한
 클래스 입니다.

더 필요한 기능들은 알아서 추가 ㅎㅎㅎ


# 폼체크및문자체크용
$error_msg[1] = '데이타 값을 입력하세요';
 $error_msg[2] = '공백없이 입력하세요';
 $error_msg[3] = '숫자는 입력할 수 없습니다';
 $error_msg[4] = '특수문자는 입력할 수 없습니다';
 $error_msg[5] = '첫글자는 영문으로 입력하세요';
 $error_msg[6] = '한글은 입력할 수 없습니다';
 $error_msg[7] = '문자를 입력할 수 있는 범위를 초과할 수 없습니다';
 $error_msg[8] = '연속해서 같은 문자를 사용할 수 없습니다';
 $error_msg[9] = '값을 정확하게 입력하세요';
 $error_msg[10] = '이메일 주소를 정확하게 입력하세요';
 $error_msg[11] = "'-'를 제외한 이메일 도메인 주소에 특수문자는 입력할 수 없습니다";
 $error_msg[12] = "홈페이지 주소는 '/,-,_,.,~,:'를 제외한 특수문자는 입력할 수 없습니다";



 <?php
 /** ======================================================
 | @Author : 김종관
| @Email : apmsoft@gmail.com
 | @HomePage : http://www.apmsoftax.com
 | @Editor : Eclipse(default)
 | @UPDATE : 2010-02-04
 ----------------------------------------------------------*/

 # purpose : 문자를 체크(Ascii 문자 코드를 활용하여) 한다 / preg,ereg 정규식 보다 훨 빠름
class IsChecker{
 private $str;
 private $len = 0;

 public function __construct($s){
 if(!empty($s)){
 $this->str = trim($s);
 $this->len = strlen($s);
 }
 }

 # null 값인지 체크한다 [ 널값이면 : true / 아니면 : false ]
 public function isNull(){
 $result = false;
 $asciiNumber = Ord($this->str);
 if(empty($asciiNumber)) return true;
 return $result;
 }


 # 문자와 문자사이 공백이 있는지 체크 [ 공백 있으면 : true / 없으면 : false ]
 public function isSpace(){
 $result = false;
 $str_split = split("[[:space:]]+",$this->str);
 $count = count($str_split);
 for($i=0; $i<$count; $i++){
 if($i>0){
 $result = true;
 break;
 }
 }
 return $result;
 }

 # 연속적으로 똑같은 문자는 입력할 수 없다  [ 반복문자 max 이상이면 : true / 아니면 : false ]
 # ex : 010-111-1111,010-222-1111 형태제한
# max = 3; // 반복문자 3개 "초과" 입력제한
public function isSameRepeatString($max=3){
 $result = false;
 $sameCount = 0;
 $preAsciiNumber = 0;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if( ($preAsciiNumber == $asciiNumber) && ($preAsciiNumber>0) ) $sameCount += 1;
 else $preAsciiNumber = $asciiNumber;

 if($sameCount==$max){
 $result = true;
 break;
 }
 }
 return $result;
 }

 # 숫자인지 체크 [ 숫자면 : true / 아니면 : false ]
 # Ascii table = 48 ~ 57
 public function isNumber(){
 $result = true;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if($asciiNumber<47 || $asciiNumber>57){
 $result = false;
 break;
 }
 }
 return $result;
 }

 # 영문인지 체크 [ 영문이면 : true / 아니면 : false ]
 # Ascii table = 대문자[75~90], 소문자[97~122]
 public function isAlphabet(){
 $result = true;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if(($asciiNumber>64 && $asciiNumber<91) || ($asciiNumber>96 && $asciiNumber<123)){}
 else{ $result = false; }
 }
 return $result;
 }

 # 영문이 대문자 인지체크 [ 대문자이면 : true / 아니면 : false ]
 # Ascii table = 대문자[75~90]
 public function isUpAlphabet(){
 $result = true;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if($asciiNumber<65 || $asciiNumber>90){
 $result = false;
 break;
 }
 }
 return $result;
 }

 # 영문이 소문자 인지체크 [ 소문자면 : true / 아니면 : false ]
 # Ascii table = 소문자[97~122]
 public function isLowAlphabet(){
 $result = true;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if($asciiNumber<97 || $asciiNumber>122){
 $result = false;
 break;
 }
 }
 return $result;
 }

 # 한글인지 체크한다 [ 한글이면 : true / 아니면 : false ]
 # Ascii table = 128 >
 public function isKorean(){
 $result = true;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if($asciiNumber<128){
 $result = false;
 break;
 }
 }
 return $result;
 }

 # 특수문자 입력여부 체크 [ 특수문자 찾으면 : true / 못찾으면 : false ]
 # allow = "-,_"; 허용시킬
# space 공백은 자동 제외
public function isEtcString($allow){
 # 허용된 특수문자 키
$allowArgs = array();
 $tmpArgs = (!empty($allow)) ? explode(',',$allow) : '';
 if(is_array($tmpArgs)){
 foreach($tmpArgs as $k => $v){
 $knumber = Ord($v);
 $allowArgs['s'.$knumber] = $v;
 }
 }

 $result = false;
 for($i=0; $i<$this->len; $i++){
 $asciiNumber = Ord($this->str[$i]);
 if(array_key_exists('s'.$asciiNumber, $allowArgs) === false){
 if( ($asciiNumber<48) && ($asciiNumber != 32) ){ $result = true; break; }
 else if($asciiNumber>57 && $asciiNumber<65){ $result = true; break; }
 else if($asciiNumber>90 && $asciiNumber<97){ $result = true; break; }
 else if($asciiNumber>122 && $asciiNumber<128){ $result = true; break; }
 }
 }
 return $result;
 }

 # 첫번째 문자가 영문인지 체크한다[ 찾으면 : true / 못찾으면 : false ]
 public function isFirstAlphabet(){
 $result = true;
 $asciiNumber = Ord($this->str[0]);
 if(($asciiNumber>64 && $asciiNumber<91) || ($asciiNumber>96 && $asciiNumber<123)){}
 else{ $result = false; }
 return $result;
 }

 # 문자길이 체크 한글/영문/숫자/특수문자/공백 전부포함
# min : 최소길이 / max : 최대길이
public function isStringLength($min,$max){
 $strCount = 0;
 for($i=0;$i<$this->len;$i++){
 $asciiNumber = Ord($this->str[$i]);
 if($asciiNumber<=127 && $asciiNumber>=0){ $strCount++; }
 else if($asciiNumber<=223 && $asciiNumber>=194){ $strCount++; $i+1; }
 else if($asciiNumber<=239 && $asciiNumber>=224){ $strCount++; $i+2; }
 else if($asciiNumber<=244 && $asciiNumber>=240){ $strCount++; $i+3; }
 }

 if($strCount<$min) return false;
 else if($strCount>$max) return false;
 else return true;
 }

 # 두 문자가 서로 같은지 비교
public function equals($s){
 $result = true;
 if(is_string($eStr)){ # 문자인지 체크
if(strcmp($this->str, $s)) $result= false;
 }else{
 if($this->str != $s ) $result = false;
 }
 return $result;
 }
 }
 ?>


 <?php
 /** ======================================================
 | @Author : 김종관
| @Email : apmsoft@gmail.com
 | @HomePage : http://www.apmsoftax.com
 | @Editor : Eclipse(default)
 | @UPDATE : 2010-02-04
 ----------------------------------------------------------*/

 # purpose : 폼 전송값 체크하기 위함
class FormChecker{
 private $phones = array('070','1588','080','02','032','041','042');
 private $cellphones = array('010','011','016','017','018','019');

 # 프라퍼티 값 입력
public function setArrayParams($propertyName,$val){
 if(property_exists(__CLASS__,$propertyName)){
 $this->{$propertyName}[] = $val;
 }
 }

 # 프라퍼티 값 리턴
public function getArrayParams($propertyName){
 if(property_exists(__CLASS__,$propertyName)){
 return $this->{$propertyName};
 }
 }

 # 이름
public function chkName($krname,$value){
 $isChceker = new IsChecker($value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if($isChceker->isNumber()){ throw new ErrorException($krname,3); }
 if($isChceker->isEtcString('')){ throw new ErrorException($krname,4); }
 }

 # 아이디
public function chkUserid($krname,$value){
 $isChceker = new IsChecker($value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if(!$isChceker->isStringLength(6,14)){ throw new ErrorException($krname,7); }
 if(!$isChceker->isFirstAlphabet()){ throw new ErrorException($krname,5); }
 if($isChceker->isKorean()){ throw new ErrorException($krname,6); }
 if($isChceker->isEtcString('')){ throw new ErrorException($krname,4); }
 }

 # 비밀번호
public function chkPassword($krname,$value){
 $isChceker = new IsChecker($value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if(!$isChceker->isStringLength(6,30)){ throw new ErrorException($krname,7); }
 if($isChceker->isKorean()){ throw new ErrorException($krname,6); }
 if($isChceker->isEtcString('')){ throw new ErrorException($krname,4); }
 }

 # 일반전화 및 팩스 (070-3456-5677, 1588-1566, 080-2323-2322)
 public function chkPhone($krname,$value){
 $isChceker = new IsChecker($value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if($isChceker->isEtcString('-')){ throw new ErrorException($krname,4); }
 if($isChceker->isSameRepeatString(2)){ throw new ErrorException($krname,8); }

 $args = explode('-',$value);
 if(array_search($args[0],$this->phones) === false) throw new ErrorException($krname,9);
 }

 # 휴대전화 (010-1234-2344)
 public function chkCellPhone($krname,$value){
 $isChceker = new IsChecker($krname,$value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if($isChceker->isEtcString('-')){ throw new ErrorException($krname,4); }
 if($isChceker->isSameRepeatString(2)){ throw new ErrorException($krname,8); }

 $args = explode('-',$value);
 if(array_search($args[0],$this->cellphones) === false) throw new ErrorException($krname,9);
 }

 # 이메일 sed_-23@apmsoftax.com
 public function chkEmail($krname,$value){
 $isChceker = new IsChecker($value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if($isChceker->isKorean()){ throw new ErrorException($krname,6); }
 if($isChceker->isEtcString('@,-,_')){ throw new ErrorException($krname,4); }

 # @ 체크
$an64 = strpos('@',$value);
 if($an64 === false){
 throw new ErrorException($krname,10);
 }else{
 # "." 확인 및 도메인 체크
$tmpstr = substr($s,($an64+1));
 $an46 = strpos('.',$tmpstr);
 if($an46 === false){
 throw new ErrorException($krname,10);
 }else{
 $domainName = substr($tmpstr,0,($an46-1));
 $domainExt = substr($tmpstr,($an46+1));

 # 도메인네임 체크
$isChceker = new IsChecker($value);
 if($isChceker->isEtcString('-_'))throw new ErrorException($krname,11);
 }
 }
 }

 # 홈페이지 주소 체크
public function chkUrl($krname,$value){
 $isChceker = new IsChecker($value);
 if($isChceker->isNull()){ throw new ErrorException($krname,1); }
 if($isChceker->isSpace()){ throw new ErrorException($krname,2); }
 if($isChceker->isKorean()){ throw new ErrorException($krname,6); }
 if($isChceker->isEtcString('/,-,_,.,~,:')){ throw new ErrorException($krname,12); }
 }
 }

 ?>



폼체킹하기

<?php

 $path = $_SERVER['DOCUMENT_ROOT'];
 include_once $path.'/config/config.php';

 try{
 $formcheck = new FormChecker;

 # 이름체크
$formcheck->chkName('[이름]','나당');

 # 일반전화 체크
$formcheck->setArrayParams('phones','054');
 $formcheck->chkPhone('[전화번호]','054-1113-2342');

 # 아이디
$formcheck->chkUserid('[아이디]','0apsmfe');

 }catch(ErrorException $e){
 $out->outPrintln('error_mesage : '.$e->getMessage().' '.$error_msg[$e->getCode()]);
 }

 ?>

댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

  • 게시물이 없습니다.

접속자집계

오늘
1,875
어제
4,039
최대
6,642
전체
830,390
contact : webmaster@beautipia.co.kr
Copyright © beautipia.co.kr. All rights reserved.