A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Validation, Security and Speed - Does my app have these?

  1. #1
    Member
    Join Date
    Jan 2007
    Posts
    79

    Question Validation, Security and Speed - Does my app have these?

    Hi all,
    I am currently working on a building community website in PHP. This contains forms that a user can fill right from registration to lot of other functionality. I am not an Object-oriented guy, so I am using functions most of the time to handle my application. I know I have to learn OOPS, but currently need to develop this website and get it running soon.

    Anyway, here's a sample of what I let my app. do:
    Consider a page (register.php) that has a form where a user has 3 fields to fill up, say: First Name, Last Name and Email. Upon submission of this form, I want to validate the form and show the corresponding errors to the users:

    Code:
    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    
    <label for="name">Name:</label>  
    <input type="text" name="name" id="name" /><br />
      
    <label for="lname">Last Name:</label>   
    <input type="text" name="lname" id="lname" /><br />
    
    <label for="email">Email:</label>   
    <input type="text" name="email" id="email" /><br />
    
    
    <input type="submit" name="submit" id="submit" value="Submit" />
    </form>
    This form will POST the info to the same page. So here's the code that will process the POST'ed info:

    PHP Code:
    <?php

    require("functions.php");

    if( isset(
    $_POST['submit']) )
        {
            
    $errors fn_register();
            
            if( 
    count($errors) )
            {
                
    //Show error messages
            
    }
            else
            {
                
    //Send welcome mail to the user or do database stuff...
            
    }
            
        }

    ?>
    PHP Code:
    <?php

    //functions.php page:

    function sql_quote$value )
    {
         if( 
    get_magic_quotes_gpc() )
        {
            
              
    $value stripslashes$value );
        }
        else
        { 
              
    $value addslashes$value );
        } 
        if( 
    function_exists"mysql_real_escape_string" ) )
        {
              
    $value mysql_real_escape_string$value );
        }
        
        return 
    $value;

    }


    function 
    clean($str) {
    $str strip_tags($str'<br>,<br />');
    $str trim($str);
    $str sql_quote($str); 

    return 
    $str;

    }


     foreach (
    $_POST as &$value)  
        {
                   if (!
    is_array($value)) 
            { 
                           
    $value clean($value); 

                   }
                   else 
            { 
                           
    clean($value);
                   }
           }

     foreach (
    $_GET as &$value)  
        {
                   if (!
    is_array($value)) 
            { 
                           
    $value clean($value); 
                   }
                   else 
            { 
                           
    clean($value);
                   }
           } 


    function 
    validate_name$fld$min$max$rule$label ) {
        
        if( 
    $rule == 'required' 
        {    
            if ( 
    trim($fld) == '' 
            {
                
    $str "$label: Cannot be left blank.";
                return 
    $str;
            }        
        }
             

        if ( isset(
    $fld) && trim($fld) != '' 
        {    
            if ( isset(
    $fld) && $fld != '' && !preg_match("/^[a-zA-Z\ ]+$/"$fld)) 
        {
                
    $str "$label: Invalid characters used! Only Lowercase, Uppercase alphabets and Spaces are allowed";
            }

        else if ( 
    strlen($fld) < $min or strlen($fld) > $max )  
        {
                
    $curr_char strlen($fld);
                
    $str "$label: Must be atleast $min character &amp; less than $max char. Entered characters: $curr_char";
            }
            else    
            {
                
    $str 0;
            }        
        }
        else
        {
            
    $str 0;
        }
        
        return 
    $str;   
    }


    function 
    validate_email$fld$min$max$rule$label ) {
        
        if( 
    $rule == 'required' 
        {    
            if ( 
    trim($fld) == '' 
            {
                
    $str "$label: Cannot be left blank.";
                return 
    $str;
            }        
        }
             

        if ( isset(
    $fld) && trim($fld) != '' 
        {    
            if ( !
    eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$'$fld) ) 
        {
                
    $str "$label: Invalid format. Please check.";
            }
        else if ( 
    strlen($fld) < $min or strlen($fld) > $max )  
        {
                
    $curr_char strlen($fld);
                
    $str "$label: Must be atleast $min character &amp; less than $max char. Entered characters: $curr_char";
            }
            else    
            {
                
    $str 0;
            }        
        }
        else
        {
            
    $str 0;
        }
        
        return 
    $str;   
    }

    function 
    val_rules$str$val_type$rule='required' ){
        
        switch (
    $val_type
        {
            case 
    'name':
                    
    $val validate_name$str320$rule'First Name');
            break;
            
            case 
    'lname':
                    
    $val validate_name$str1020$rule'Last Name');
            break;        
            
            case 
    'email':
                    
    $val validate_email$str1060$rule'Email');
            break;
            

        }
        
        return 
    $val;
    }


    function 
    fn_register() {
        
        
    $errors = array();

        
    $val_name        val_rules$_POST['name'], 'name' );
        
    $val_lname           val_rules$_POST['lname'], 'lname''optional' );
        
    $val_email        val_rules$_POST['email'], 'email' );
     
        if ( 
    $val_name != '0' )         { $errors['name']   = $val_name;  }
        if ( 
    $val_lname != '0' )         { $errors['lname']  = $val_lname; }
        if ( 
    $val_email != '0' )         { $errors['email']  = $val_email; }
        
         return 
    $errors;
    }

    //END of functions.php page
    ?>
    OK, now it might look like there's a lot, but lemme break it down target wise:
    1. I wanted the foreach ($_POST as &$value) and foreach ($_GET as &$value) loops to loop through the received info from the user submission and strip/remove all malicious input.

    2. I am calling a function called clean on the input first to achieve the objective as stated above. This function will process each of the input, whether individual field values or even arrays and allow only <br /> tags and remove everything else. The rest of it is obvious.

    3. Once this happens, the new/cleaned values will be processed by the fn_register() function and based on the values returned after the validation, we get the corresponding errors or NULL values (as applicable).

    So here's my questions:
    1. This pretty much makes me feel secure as I am forcing the user to correct malicious data and won't process the final data unless the errors are corrected. Am I correct?

    2. Does the method that I follow guarantee the speed (as I am using lots of functions and their corresponding calls)? The fields of a form differ and the minimum number of fields I may have at any given point of time in any form may be 3 and can go upto as high as 100 (or even more, I am not sure as the website is still being developed). Will having 100's of fields and their validation in the above way, reduce the speed of application (say upto half a million users are accessing the website at the same time?). What can I do to improve the speed and reduce function calls (if possible)?

    3, Can I do something to improve the current ways of validation?

    I am holding off object oriented approach and using FILTERS in PHP for the later. So please, I request you all to suggest me way to improve/tweak the current ways and suggest me if the script is vulnerable or safe enough to be used in a Live production environment. If not, what I can do to be able to use it live?

    Thank you all in advance.

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    some comments: if your form has a simple (single-line) input such as name, why would you even expect to get an array - rather than dump one as potential hacking attempt?
    Setting $str to an error message in one place and to 0 in another one might end up showing stray "0"s on the web page. Consider using blank string "" here

    Computers are getting faster and faster every time, and you are probably not expecting a million of users completing a form at the same time

    Beware of verifying names - there are sometimes funny characters with all kinds of things on top of them. Are you sure that 10 to 60 chars is correct for limiting emails, and that last names cannot be shorter than 10 chars?

    Musicman

  3. #3
    Member
    Join Date
    Jan 2007
    Posts
    79
    Hi Musicman,
    Thanks for your reply. There could be many inputs (apart from just a Name or Last name). I just took it as an example to explain better.

    Actually, I don't think I will ever get to see the 0's (nor will I see any blank spaces if I change it to what you suggested) when setting $str value. The code that I have written in fn_register() tells this. My only purpose is to show the error message.

    I agree with you. Not a million need to be completing the form at the same time. But there are many forms and some are more complex with multiple joins, inner joins, outer joins and such. So all these could add up to consume resources. So I was trying to keep my app. fairly simple and fast and that's why I wanted any advices on the same.

    Limiting names or email to the characters that I mentioned were just examples. Could be increased or reduced as needed.

    Hope I was able to explain my intentions.

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    question about speed: validating (i.e. just looking at strings in memory) will have less of a performance impact than sql queries could have. So, if you need to look at performance, you would better review sql queries, decide on index use etc.

    Musicman

  5. #5
    Member
    Join Date
    Jan 2007
    Posts
    79
    Nice one, Musicman. I will have that in mind. Thank you.

  6. #6
    Member
    Join Date
    Jan 2007
    Posts
    79

    Exclamation Anyone else?

    Does anyone still want to contribute to the idea pool? It will serve the purpose of many if we can cover the topics here in this thread.

    Thanks again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center