Hello,
I am currently writing a PHP input validation class to secure all my php projects:
PHP Code:
<?php class validation { /** Check single-line inputs: Returns true if text contains newline character */ function has_newlines($text) { return preg_match("/(%0A|%0D|\n+|\r+)/i", $text); } /** Check multi-line inputs: Returns true if text contains newline followed by email-header specific string*/ function has_emailheaders($text) { return preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i", $text); } function input($val,$type='input',$allow_wildcards = false) { if (($type=='input' && has_newlines($val)) || ($type=='textarea' && has_emailheaders($val))) { // not good } else { if (!$allow_wildcards) { $val = str_replace('%','\%',$val); $val = str_replace('_','\_',$val); } return mysql_real_escape_string($val); } } function email($val) { if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $val)) { return $val; } else { // not good } } function number($val) { return (int)$val; } } ?>
To be used like this:
PHP Code:
<?php $email_address = $validate->email($_POST['email']); $single_line_text = $validate->input($_POST[inputfield]); $multi_line_text = $validate->input($_POST['textarea']); $numeric = $validate->number($_POST['number']); ?>
It should check:
*numeric fields are actually nmeric
*textarea field does not contain email header injection possibilities
*input field does not contain email header injection possibilities
*email address is valid
*remove wildcards
*mysql_real_escape_string() everything
What is your opinion about this? Can this be enhanced? Is it (in)secure? Pleas let me know how i can further improve this!
Thanks alot,
Matt
Last edited by killerwhale65; 01-25-2009 at 08:53 AM..
|