-
php5 폼 전송 값 및 문자 체크하기Programming/PHP 2010. 10. 5. 11:07출처: http://it.moyiza.com/?mid=programming&category=72&document_srl=2540
아스키 코드 값을 통해 구현했습니다
정규식 쓰면 더 간단하게 코딩이 되겠지만
아무리 테스트 해봐도
정규식 보다 아스키체크가 더 빠르더라고요
그래서 아스키 코드로만 체크했음당
아무튼
폼값으로 전송된 값을 체크 하기 위한
클래스 입니다.
더 필요한 기능들은 알아서 추가 ㅎㅎㅎ
# 폼체크및문자체크용
01.
$error_msg
[1] =
'데이타 값을 입력하세요'
;
02.
03.
$error_msg
[2] =
'공백없이 입력하세요'
;
04.
05.
$error_msg
[3] =
'숫자는 입력할 수 없습니다'
;
06.
07.
$error_msg
[4] =
'특수문자는 입력할 수 없습니다'
;
08.
09.
$error_msg
[5] =
'첫글자는 영문으로 입력하세요'
;
10.
11.
$error_msg
[6] =
'한글은 입력할 수 없습니다'
;
12.
13.
$error_msg
[7] =
'문자를 입력할 수 있는 범위를 초과할 수 없습니다'
;
14.
15.
$error_msg
[8] =
'연속해서 같은 문자를 사용할 수 없습니다'
;
16.
17.
$error_msg
[9] =
'값을 정확하게 입력하세요'
;
18.
19.
$error_msg
[10] =
'이메일 주소를 정확하게 입력하세요'
;
20.
21.
$error_msg
[11] =
"'-'를 제외한 이메일 도메인 주소에 특수문자는 입력할 수 없습니다"
;
22.
23.
$error_msg
[12] =
"홈페이지 주소는 '/,-,_,.,~,:'를 제외한 특수문자는 입력할 수 없습니다"
;
001.
<?php
002.
003.
/** ======================================================
004.
005.
| @Author : 김종관
006.
007.
| @Email : apmsoft@gmail.com
008.
009.
| @HomePage : http://www.apmsoftax.com
010.
011.
| @Editor : Eclipse(default)
012.
013.
| @UPDATE : 2010-02-04
014.
015.
----------------------------------------------------------*/
016.
017.
018.
# purpose : 문자를 체크(Ascii 문자 코드를 활용하여) 한다 / preg,
ereg
정규식 보다 훨 빠름
019.
020.
class
IsChecker{
021.
022.
private
$str
;
023.
024.
private
$len
= 0;
025.
026.
027.
028.
public
function
__construct(
$s
){
029.
030.
if
(!
empty
(
$s
)){
031.
032.
$this
->str = trim(
$s
);
033.
034.
$this
->len =
strlen
(
$s
);
035.
036.
}
037.
038.
}
039.
040.
041.
042.
# null 값인지 체크한다 [ 널값이면 : true / 아니면 : false ]
043.
044.
public
function
isNull(){
045.
046.
$result
= false;
047.
048.
$asciiNumber
= Ord(
$this
->str);
049.
050.
if
(
empty
(
$asciiNumber
))
return
true;
051.
052.
return
$result
;
053.
054.
}
055.
056.
057.
058.
059.
# 문자와 문자사이 공백이 있는지 체크 [ 공백 있으면 : true / 없으면 : false ]
060.
061.
public
function
isSpace(){
062.
063.
$result
= false;
064.
065.
$str_split
= split(
"[[:space:]]+"
,
$this
->str);
066.
067.
$count
=
count
(
$str_split
);
068.
069.
for
(
$i
=0;
$i
<
$count
;
$i
++){
070.
071.
if
(
$i
>0){
072.
073.
$result
= true;
074.
075.
break
;
076.
077.
}
078.
079.
}
080.
081.
return
$result
;
082.
083.
}
084.
085.
086.
087.
# 연속적으로 똑같은 문자는 입력할 수 없다 [ 반복문자 max 이상이면 : true / 아니면 : false ]
088.
089.
# ex : 010-111-1111,010-222-1111 형태제한
090.
091.
# max = 3;
// 반복문자 3개 "초과" 입력제한
092.
093.
public
function
isSameRepeatString(
$max
=3){
094.
095.
$result
= false;
096.
097.
$sameCount
= 0;
098.
099.
$preAsciiNumber
= 0;
100.
101.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
102.
103.
$asciiNumber
= Ord(
$this
->str[
$i
]);
104.
105.
if
( (
$preAsciiNumber
==
$asciiNumber
) && (
$preAsciiNumber
>0) )
$sameCount
+= 1;
106.
107.
else
$preAsciiNumber
=
$asciiNumber
;
108.
109.
110.
111.
if
(
$sameCount
==
$max
){
112.
113.
$result
= true;
114.
115.
break
;
116.
117.
}
118.
119.
}
120.
121.
return
$result
;
122.
123.
}
124.
125.
126.
127.
# 숫자인지 체크 [ 숫자면 : true / 아니면 : false ]
128.
129.
# Ascii table = 48 ~ 57
130.
131.
public
function
isNumber(){
132.
133.
$result
= true;
134.
135.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
136.
137.
$asciiNumber
= Ord(
$this
->str[
$i
]);
138.
139.
if
(
$asciiNumber
<47 ||
$asciiNumber
>57){
140.
141.
$result
= false;
142.
143.
break
;
144.
145.
}
146.
147.
}
148.
149.
return
$result
;
150.
151.
}
152.
153.
154.
# 영문인지 체크 [ 영문이면 : true / 아니면 : false ]
155.
156.
# Ascii table = 대문자[75~90], 소문자[97~122]
157.
158.
public
function
isAlphabet(){
159.
160.
$result
= true;
161.
162.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
163.
164.
$asciiNumber
= Ord(
$this
->str[
$i
]);
165.
166.
if
((
$asciiNumber
>64 &&
$asciiNumber
<91) || (
$asciiNumber
>96 &&
$asciiNumber
<123)){}
167.
168.
else
{
$result
= false; }
169.
170.
}
171.
172.
return
$result
;
173.
174.
}
175.
176.
177.
# 영문이 대문자 인지체크 [ 대문자이면 : true / 아니면 : false ]
178.
179.
# Ascii table = 대문자[75~90]
180.
181.
public
function
isUpAlphabet(){
182.
183.
$result
= true;
184.
185.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
186.
187.
$asciiNumber
= Ord(
$this
->str[
$i
]);
188.
189.
if
(
$asciiNumber
<65 ||
$asciiNumber
>90){
190.
191.
$result
= false;
192.
193.
break
;
194.
195.
}
196.
197.
}
198.
199.
return
$result
;
200.
201.
}
202.
203.
204.
# 영문이 소문자 인지체크 [ 소문자면 : true / 아니면 : false ]
205.
206.
# Ascii table = 소문자[97~122]
207.
208.
public
function
isLowAlphabet(){
209.
210.
$result
= true;
211.
212.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
213.
214.
$asciiNumber
= Ord(
$this
->str[
$i
]);
215.
216.
if
(
$asciiNumber
<97 ||
$asciiNumber
>122){
217.
218.
$result
= false;
219.
220.
break
;
221.
222.
}
223.
224.
}
225.
226.
return
$result
;
227.
228.
}
229.
230.
231.
232.
# 한글인지 체크한다 [ 한글이면 : true / 아니면 : false ]
233.
234.
# Ascii table = 128 >
235.
236.
public
function
isKorean(){
237.
238.
$result
= true;
239.
240.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
241.
242.
$asciiNumber
= Ord(
$this
->str[
$i
]);
243.
244.
if
(
$asciiNumber
<128){
245.
246.
$result
= false;
247.
248.
break
;
249.
250.
}
251.
252.
}
253.
254.
return
$result
;
255.
256.
}
257.
258.
259.
260.
# 특수문자 입력여부 체크 [ 특수문자 찾으면 : true / 못찾으면 : false ]
261.
262.
# allow =
"-,_"
; 허용시킬
263.
264.
# space 공백은 자동 제외
265.
266.
public
function
isEtcString(
$allow
){
267.
268.
# 허용된 특수문자 키
269.
270.
$allowArgs
=
array
();
271.
272.
$tmpArgs
= (!
empty
(
$allow
)) ?
explode
(
','
,
$allow
) :
''
;
273.
274.
if
(
is_array
(
$tmpArgs
)){
275.
276.
foreach
(
$tmpArgs
as
$k
=>
$v
){
277.
278.
$knumber
= Ord(
$v
);
279.
280.
$allowArgs
[
's'
.
$knumber
] =
$v
;
281.
282.
}
283.
284.
}
285.
286.
287.
288.
$result
= false;
289.
290.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
291.
292.
$asciiNumber
= Ord(
$this
->str[
$i
]);
293.
294.
if
(
array_key_exists
(
's'
.
$asciiNumber
,
$allowArgs
) === false){
295.
296.
if
( (
$asciiNumber
<48) && (
$asciiNumber
!= 32) ){
$result
= true;
break
; }
297.
298.
else
if
(
$asciiNumber
>57 &&
$asciiNumber
<65){
$result
= true;
break
; }
299.
300.
else
if
(
$asciiNumber
>90 &&
$asciiNumber
<97){
$result
= true;
break
; }
301.
302.
else
if
(
$asciiNumber
>122 &&
$asciiNumber
<128){
$result
= true;
break
; }
303.
304.
}
305.
306.
}
307.
308.
return
$result
;
309.
310.
}
311.
312.
313.
314.
# 첫번째 문자가 영문인지 체크한다[ 찾으면 : true / 못찾으면 : false ]
315.
316.
public
function
isFirstAlphabet(){
317.
318.
$result
= true;
319.
320.
$asciiNumber
= Ord(
$this
->str[0]);
321.
322.
if
((
$asciiNumber
>64 &&
$asciiNumber
<91) || (
$asciiNumber
>96 &&
$asciiNumber
<123)){}
323.
324.
else
{
$result
= false; }
325.
326.
return
$result
;
327.
328.
}
329.
330.
331.
332.
# 문자길이 체크 한글/영문/숫자/특수문자/공백 전부포함
333.
334.
# min : 최소길이 / max : 최대길이
335.
336.
public
function
isStringLength(
$min
,
$max
){
337.
338.
$strCount
= 0;
339.
340.
for
(
$i
=0;
$i
<
$this
->len;
$i
++){
341.
342.
$asciiNumber
= Ord(
$this
->str[
$i
]);
343.
344.
if
(
$asciiNumber
<=127 &&
$asciiNumber
>=0){
$strCount
++; }
345.
346.
else
if
(
$asciiNumber
<=223 &&
$asciiNumber
>=194){
$strCount
++;
$i
+1; }
347.
348.
else
if
(
$asciiNumber
<=239 &&
$asciiNumber
>=224){
$strCount
++;
$i
+2; }
349.
350.
else
if
(
$asciiNumber
<=244 &&
$asciiNumber
>=240){
$strCount
++;
$i
+3; }
351.
352.
}
353.
354.
355.
356.
if
(
$strCount
<
$min
)
return
false;
357.
358.
else
if
(
$strCount
>
$max
)
return
false;
359.
360.
else
return
true;
361.
362.
}
363.
364.
365.
366.
# 두 문자가 서로 같은지 비교
367.
368.
public
function
equals(
$s
){
369.
370.
$result
= true;
371.
372.
if
(
is_string
(
$eStr
)){ # 문자인지 체크
373.
374.
if
(
strcmp
(
$this
->str,
$s
))
$result
= false;
375.
376.
}
else
{
377.
378.
if
(
$this
->str !=
$s
)
$result
= false;
379.
380.
}
381.
382.
return
$result
;
383.
384.
}
385.
386.
}
387.
388.
?>
001.
002.
<?php
003.
004.
/** ======================================================
005.
006.
| @Author : 김종관
007.
008.
| @Email : apmsoft@gmail.com
009.
010.
| @HomePage : http://www.apmsoftax.com
011.
012.
| @Editor : Eclipse(default)
013.
014.
| @UPDATE : 2010-02-04
015.
016.
----------------------------------------------------------*/
017.
018.
019.
# purpose : 폼 전송값 체크하기 위함
020.
021.
class
FormChecker{
022.
023.
private
$phones
=
array
(
'070'
,
'1588'
,
'080'
,
'02'
,
'032'
,
'041'
,
'042'
);
024.
025.
private
$cellphones
=
array
(
'010'
,
'011'
,
'016'
,
'017'
,
'018'
,
'019'
);
026.
027.
028.
# 프라퍼티 값 입력
029.
030.
public
function
setArrayParams(
$propertyName
,
$val
){
031.
032.
if
(property_exists(
__CLASS__
,
$propertyName
)){
033.
034.
$this
->{
$propertyName
}[] =
$val
;
035.
036.
}
037.
038.
}
039.
040.
041.
042.
# 프라퍼티 값 리턴
043.
044.
public
function
getArrayParams(
$propertyName
){
045.
046.
if
(property_exists(
__CLASS__
,
$propertyName
)){
047.
048.
return
$this
->{
$propertyName
};
049.
050.
}
051.
052.
}
053.
054.
055.
056.
# 이름
057.
058.
public
function
chkName(
$krname
,
$value
){
059.
060.
$isChceker
=
new
IsChecker(
$value
);
061.
062.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
063.
064.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
065.
066.
if
(
$isChceker
->isNumber()){
throw
new
ErrorException(
$krname
,3); }
067.
068.
if
(
$isChceker
->isEtcString(
''
)){
throw
new
ErrorException(
$krname
,4); }
069.
070.
}
071.
072.
073.
074.
# 아이디
075.
076.
public
function
chkUserid(
$krname
,
$value
){
077.
078.
$isChceker
=
new
IsChecker(
$value
);
079.
080.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
081.
082.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
083.
084.
if
(!
$isChceker
->isStringLength(6,14)){
throw
new
ErrorException(
$krname
,7); }
085.
086.
if
(!
$isChceker
->isFirstAlphabet()){
throw
new
ErrorException(
$krname
,5); }
087.
088.
if
(
$isChceker
->isKorean()){
throw
new
ErrorException(
$krname
,6); }
089.
090.
if
(
$isChceker
->isEtcString(
''
)){
throw
new
ErrorException(
$krname
,4); }
091.
092.
}
093.
094.
095.
096.
# 비밀번호
097.
098.
public
function
chkPassword(
$krname
,
$value
){
099.
100.
$isChceker
=
new
IsChecker(
$value
);
101.
102.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
103.
104.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
105.
106.
if
(!
$isChceker
->isStringLength(6,30)){
throw
new
ErrorException(
$krname
,7); }
107.
108.
if
(
$isChceker
->isKorean()){
throw
new
ErrorException(
$krname
,6); }
109.
110.
if
(
$isChceker
->isEtcString(
''
)){
throw
new
ErrorException(
$krname
,4); }
111.
112.
}
113.
114.
115.
116.
# 일반전화 및 팩스 (070-3456-5677, 1588-1566, 080-2323-2322)
117.
118.
public
function
chkPhone(
$krname
,
$value
){
119.
120.
$isChceker
=
new
IsChecker(
$value
);
121.
122.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
123.
124.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
125.
126.
if
(
$isChceker
->isEtcString(
'-'
)){
throw
new
ErrorException(
$krname
,4); }
127.
128.
if
(
$isChceker
->isSameRepeatString(2)){
throw
new
ErrorException(
$krname
,8); }
129.
130.
131.
$args
=
explode
(
'-'
,
$value
);
132.
133.
if
(
array_search
(
$args
[0],
$this
->phones) === false)
throw
new
ErrorException(
$krname
,9);
134.
135.
}
136.
137.
138.
# 휴대전화 (010-1234-2344)
139.
140.
public
function
chkCellPhone(
$krname
,
$value
){
141.
142.
$isChceker
=
new
IsChecker(
$krname
,
$value
);
143.
144.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
145.
146.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
147.
148.
if
(
$isChceker
->isEtcString(
'-'
)){
throw
new
ErrorException(
$krname
,4); }
149.
150.
if
(
$isChceker
->isSameRepeatString(2)){
throw
new
ErrorException(
$krname
,8); }
151.
152.
153.
154.
$args
=
explode
(
'-'
,
$value
);
155.
156.
if
(
array_search
(
$args
[0],
$this
->cellphones) === false)
throw
new
ErrorException(
$krname
,9);
157.
158.
}
159.
160.
161.
162.
# 이메일 sed_-23@apmsoftax.com
163.
164.
public
function
chkEmail(
$krname
,
$value
){
165.
166.
$isChceker
=
new
IsChecker(
$value
);
167.
168.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
169.
170.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
171.
172.
if
(
$isChceker
->isKorean()){
throw
new
ErrorException(
$krname
,6); }
173.
174.
if
(
$isChceker
->isEtcString(
'@,-,_'
)){
throw
new
ErrorException(
$krname
,4); }
175.
176.
177.
178.
# @ 체크
179.
180.
$an64
=
strpos
(
'@'
,
$value
);
181.
182.
if
(
$an64
=== false){
183.
184.
throw
new
ErrorException(
$krname
,10);
185.
186.
}
else
{
187.
188.
#
"."
확인 및 도메인 체크
189.
190.
$tmpstr
=
substr
(
$s
,(
$an64
+1));
191.
192.
$an46
=
strpos
(
'.'
,
$tmpstr
);
193.
194.
if
(
$an46
=== false){
195.
196.
throw
new
ErrorException(
$krname
,10);
197.
198.
}
else
{
199.
200.
$domainName
=
substr
(
$tmpstr
,0,(
$an46
-1));
201.
202.
$domainExt
=
substr
(
$tmpstr
,(
$an46
+1));
203.
204.
205.
206.
# 도메인네임 체크
207.
208.
$isChceker
=
new
IsChecker(
$value
);
209.
210.
if
(
$isChceker
->isEtcString(
'-_'
))
throw
new
ErrorException(
$krname
,11);
211.
212.
}
213.
214.
}
215.
216.
}
217.
218.
219.
220.
# 홈페이지 주소 체크
221.
222.
public
function
chkUrl(
$krname
,
$value
){
223.
224.
$isChceker
=
new
IsChecker(
$value
);
225.
226.
if
(
$isChceker
->isNull()){
throw
new
ErrorException(
$krname
,1); }
227.
228.
if
(
$isChceker
->isSpace()){
throw
new
ErrorException(
$krname
,2); }
229.
230.
if
(
$isChceker
->isKorean()){
throw
new
ErrorException(
$krname
,6); }
231.
232.
if
(
$isChceker
->isEtcString(
'/,-,_,.,~,:'
)){
throw
new
ErrorException(
$krname
,12); }
233.
234.
}
235.
236.
}
237.
238.
239.
?>
폼체킹하기
01.
<?php
02.
03.
04.
05.
$path
=
$_SERVER
[
'DOCUMENT_ROOT'
];
06.
07.
include_once
$path
.
'/config/config.php'
;
08.
09.
10.
11.
try{
12.
13.
$formcheck
=
new
FormChecker;
14.
15.
16.
17.
# 이름체크
18.
19.
$formcheck
->chkName(
'[이름]'
,
'나당'
);
20.
21.
22.
23.
# 일반전화 체크
24.
25.
$formcheck
->setArrayParams(
'phones'
,
'054'
);
26.
27.
$formcheck
->chkPhone(
'[전화번호]'
,
'054-1113-2342'
);
28.
29.
30.
31.
# 아이디
32.
33.
$formcheck
->chkUserid(
'[아이디]'
,
'0apsmfe'
);
34.
35.
36.
37.
}catch(ErrorException
$e
){
38.
39.
$out
->outPrintln(
'error_mesage : '
.
$e
->getMessage().
' '
.
$error_msg
[
$e
->getCode()]);
40.
41.
}
42.
43.
44.
45.
?>