25 PHP Form Validation Snippets

download 25 PHP Form Validation Snippets

of 12

Transcript of 25 PHP Form Validation Snippets

  • 7/25/2019 25 PHP Form Validation Snippets

    1/12

    25 PHP Form Validation Snippets

    November 6 by Clay

    Recently i have involve myself in another application development. Re!lar

    H!nred "ot Com visitors #ill notice that the site c!rrently o$er advertisementspace thro!h this form. %!t really& #e as a developers are al#ays loo'in for

    s!ch snippets or #ritin them o!t from scratch every sinle time reardless of

    ho# many time #e 'no# #e have store it some#here in o!r laptop( )an& its

    really fr!stratin searchin on *oole and +nd all sort of sol!tion and tryin to

    +!re o!t #hether the re!lar e,pression implemented is e,pensive or complete.

    So i came o!t #ith an idea to ease my life a bit and other developers by p!ttin

    !p an article s!ch as this for my-o!r references. /his can be made into a class if

    yo! li'e to0

    Validate 1mail

    e can perform an email validation thro!h this f!nction.

    f!nction isValid1mail3email04

    ret!rn erei78a9:;9?.78a9:;90@A7a9:;9?.7a9:;90@?.7a9:=

    42&B03& 3email0D

    Efter fainted for a fe# seconds #hen i sa# !nreal! +ndin& i decided to thro#

    !p pre8match sol!tion instead.

    f!nction isValid1mail3email04

    ret!rn pre8match-78a9:;9?.78a9:;90@A7a9:;9?.7a9:;90@?.

    7a9:=42&B03-i& 3email0D

    PHP 5.2 and above.

    f!nction fnValidate1mail3email0

    4

    ret!rn +lter8var3email& FG/1R8VEG"E/181)EG0D

    Saniti:e 1mail

  • 7/25/2019 25 PHP Form Validation Snippets

    2/12

    e can f!rther saniti:e o!r email to ens!re that everythin is alriht.

    f!nction fnSaniti:e1maill3strin0 4

    ret!rn pre8replace IJ?nK?rK?tKL;EKL;"KL;MKL;0i & & 3strin 0D

    PHP 5.2 and above.

    f!nction fnSaniti:e1maill3!rl0

    4

    ret!rn +lter8var3!rl& FG/1R8SENG/G181)EG0D

    Validate 1mail 1,ist

    /his is not possible b!t certain validation can be !se to validate email e,istence.

    f!nction chec'8email3email0

    4

    3email8error O falseD

    31mail O htmlspecialcharsstripslashesstrip8tastrim3email0000D --parse!nnecessary characters to prevent e,ploits

    if 31mail OO 0 4 email8error O tr!eD

    elseif (erei7a9:E9;9A7a9:E9;9?.7a9:E9;9

  • 7/25/2019 25 PHP Form Validation Snippets

    3/12

    if email8error0 4 ret!rn falseD else4ret!rn tr!eD

    Validate N!mber Qnly

    e can !se PHP b!ilt9in f!nction to validate #hether a iven val!e is a n!mber.

    f!nction fnValidateN!mber3val!e0

    4

    is8 do!ble3val!e0D

    is8 oat3val!e0D

    is8 int3val!e0D

    is8 inteer3val!e0D

    ret!rn is8n!meric3val!e0D

    PHP 5.2 and above.

    f!nction fnValidateN!mber3val!e0

    4

    ret!rn +lter8var3val!e& FG/1R8VEG"E/18FQE/0D -- oat

    ret!rn +lter8var3val!e& FG/1R8VEG"E/18GN/0D int

    Saniti:e N!mber

    e can force all val!e to be only n!meric by saniti:e them.

    f!nction fnSaniti:eN!mber3str0

    4

    letters and space only

    ret!rn pre8match-7;9

  • 7/25/2019 25 PHP Form Validation Snippets

    4/12

    f!nction fnSaniti:eN!mber3val!e0

    4

    ret!rn +lter8var3val!e& FG/1R8SENG/G18NT)%1R8FQE/0D -- oat

    ret!rn +lter8var3val!e& FG/1R8SENG/G18NT)%1R8GN/0D int

    Validate Strin Qnly

    Sometimes to validate name #e can !se this f!nction to restrict only letters and

    spaces.

    f!nction fnValidateStrinr3str0

    4

    letters and space only

    ret!rn pre8match-7E9a9:?s =>3-& 3str0D

    Saniti:e Strin

    e can saniti:e it instead of validate !ser inp!t.

    f!nction fnSaniti:eStrinr3str0

    4

    letters and space only

    ret!rn pre8replace-7E9a9:?s =-& & 3str0D

    PHP 5.2 and above. b!ilt9in f!nction by PHP provides a m!ch more po#erf!l

    saniti:e capability.

    f!nction fnSaniti:eStrinr3str0

    4

    ret!rn +lter8var3str& FG/1R8SENG/G18S/RGPP1"0D only Strin is allo#ed

    e. UbrH1QU-br O H1Q

  • 7/25/2019 25 PHP Form Validation Snippets

    5/12

    Validate Elphan!meric Characters

    /his validates alphan!meric characters.

    f!nction fnValidateElphan!meric3strin0

    4

    ret!rn ctype8aln!m 3strin0D

    Saniti:e Elphan!meric Characters

    /his saniti:e alphan!meric characters. e. WH1Q( "o #e have

  • 7/25/2019 25 PHP Form Validation Snippets

    6/12

    3path O isset3!rl7path=00 I 3!rl7path= J D

    if 3path OO 0

    4

    3path O -D

    3path .O isset3!rl7X!ery=00 I I3!rl7X!ery= J D

    if isset3!rl7host=0 EN" 3!rl7host= (O Aethostbyname3!rl7host=00

    4

    if PHP8V1RSGQN O 50

    4

    3headers O Aet8headers3!rl7scheme=J--3!rl7host=J3!rl7port=3path0D

    else

    4

    3fp O fsoc'open3!rl7host=& 3!rl7port=& 3errno& 3errstr& B;0D

    if (3fp0

    4

    ret!rn falseD

    fp!ts3fp& H1E" 3path H//P-Y.Y?r?nHostJ 3!rl7host=?r?n?r?n0D

    3headers O fread3fp& ;

  • 7/25/2019 25 PHP Form Validation Snippets

    7/12

    ret!rn bool0pre8matchH//P-.@?s>72;;KB;YKB;20=>?si& 3headers0D

    ret!rn falseD

    Validate TR Format

    /his f!nction #ill validate a iven !rl to ens!re the format is correct.

    f!nction fnValidateTrl3!rl04

    ret!rn pre8match-httpsI0J?-?-KftpJ?-?-4Y0?#>?.04Y&0?#42&3-i& 3!rl0D

    PHP 5.2 and above.

    f!nction fnValidateTrl3!rl0

    4

    ret!rn +lter8var3!rl& FG/1R8VEG"E/18TR0D

    Saniti:e TR

    PHP 5.2 and above.

    f!nction fnSaniti:eTrl3!rl0

    4

    ret!rn +lter8var3!rl& FG/1R8SENG/G18TR0D

    Validate Gmae 1,ist

    /his f!nction #ill chec' #hether a iven imae lin' e,ist and not only validate it.

    f!nction imae8e,ist3!rl0 4

    ifA+le8et8contents3!rl&;&NT&;&Y004ret!rn YDelse4 ret!rn ;D

  • 7/25/2019 25 PHP Form Validation Snippets

    8/12

    Validate GP Eddress

    /his f!nction #ill validate an GP address.

    f!nction fnValidateGP3GP04

    ret!rn pre8match-7Y9

  • 7/25/2019 25 PHP Form Validation Snippets

    9/12

    %efore #e validate #hether a iven !sername is matches the one in o!r

    database& #e can perform a validation chec' +rst to prevent any !nnecessary

    SZ call.

    f!nction fnValidateTsername3!sername04

    alphabet& diit& A& 8 and . are allo#. )inim!m 6 character. )a,im!m 5;

    characters email address may be more0

    ret!rn pre8match-7a9:E9?d8A.=46&5;3-i& 3!sername0D

    Validate Stron Pass#ord

    Enother ood thin is to validate #hether a partic!lar pass#ord iven by the

    !ser is stron eno!h. [o! can do that !sin this f!nction #hich reX!ired the

    pass#ord to have a minim!m of M characters& at least Y !ppercase& Y lo#ercase

    and Y n!mber.

    f!nction fnValidatePass#ord3pass#ord04

    m!st contain M characters& Y !ppercase& Y lo#ercase and Y n!mber

    ret!rn pre8match-IO.4M&30IO.@7E9a9:;9

  • 7/25/2019 25 PHP Form Validation Snippets

    10/12

    ret!rn pre8match-7;9

  • 7/25/2019 25 PHP Form Validation Snippets

    11/12

    /his is a date format [[[[9""9)) or [[9))9"" validation #hich validate from

    year ;;;;9

  • 7/25/2019 25 PHP Form Validation Snippets

    12/12

    --!sae call it some#here in beinnin of yo!r script

    8clean38PQS/0D

    8clean38*1/0D

    8clean38R1ZT1S/0D-- and so on..

    )a'e "ata Safe

    /his f!nction help to 'eep !s protected aainst SS& ^S and SZ in]ection by

    removin tas.

    f!nction 8clean3str04

    ret!rn is8array3str0 I array8map8clean& 3str0 J str8replace??& ????&

    strip8tastrimhtmlspecialcharset8maic8X!otes8pc0 I stripslashes3str0 J

    3str0& 1N/8ZTQ/1S0000D

    --!sae call it some#here in beinnin of yo!r script

    8clean38PQS/0D

    8clean38*1/0D

    8clean38R1ZT1S/0D-- and so on..