Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
writing a php input validation class
Old 01-25-2009, 08:51 AM writing a php input validation class
Skilled Talker

Posts: 83
Trades: 0
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..
killerwhale65 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-25-2009, 11:52 AM Re: writing a php input validation class
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
It's good OOP is really funky and quite easy to update. It also seems to be good code

Improving it wise. look into sterilize() http://uk2.php.net/manual/en/function.serialize.php for SQL stuff

Otherwise - good job!
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 01-25-2009, 12:24 PM Re: writing a php input validation class
Skilled Talker

Posts: 83
Trades: 0
Thanks i will look into this.

In the meantime i have expanded the code with an URL validation, and rewrote the email validation:

PHP Code:
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))) {
   
//no good
  
} else {
   if (!
$allow_wildcards) {
    
$val str_replace('%','\%',$val);
    
$val str_replace('_','\_',$val);
   }
   return 
mysql_real_escape_string($val);
  }
 }
 
 function 
email($val) {
  
// First, we check that there's one @ symbol, and that the lengths are right.
  
if (!ereg("^[^@]{1,64}@[^@]{1,255}$"$val)) {
   
// Email invalid because wrong number of characters in one section or wrong number of @ symbols.
   
return false;
  }
 
  
/*  
  * The caret (^) indicates the beginning of the string, $ the end.
  * start with at least 1 char in the range a-z, A-Z or 0-9, (not mandatory:) followed by a dot + chars (at least 1) in the range a-z, A-Z, 0-9, - or _ (for example user@domain.com or first.last@domain.com)
  * after the @ symbol, at least 1 char in the range a-z, A-Z, 0-9, - or _, (not mandatory:) followed by a dot + chars (at least 1) in the range a-z, A-Z, 0-9, - or _
  * this follows always by a dot and then the extension, containing only chars in the range [a-zA-Z] which must be between 2-6 chars long
  */  
  
if(preg_match("/^([a-zA-Z0-9])+([\.[a-zA-Z0-9_-]+])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/"$val)) {
   list(
$username,$domain)=split('@',$val);
   if(!
checkdnsrr($domain,'MX')) {
    if(!
checkdnsrr($domain,'A')) {
       
//no good
    
}
   }
   return 
true;
    } else {
     
//no good
    
}
 }
 
 function 
number($val) {
  return (int)
$val;
 }
 
 function 
url($val) {
   
/*Must start with either http(with an optional s):// or ftp:// followed by any number of alphanumeric characters and the understrike 
   (this allows for subdomains, etc) followed by an ending that is at least 2 characters long, and allows for an optional trailing slash*/
   
if (preg_match("/^(http(s?):\\/\\/|ftp:\\/\\/{1})((\w+\.)+)\w{2,}(\/?)$/i"$val)) {
    return 
$val;
   } else {
    
//no good
   
}
  }

killerwhale65 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to writing a php input validation class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17972 seconds with 12 queries