#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Filter_var what are you doing?


    Why does it print Not valid?
    PHP Code:
    <?php 
    class Validator
        
    {
            public function 
    validate($string$type)
                {
                    if(
    $type=='email')
                        {
                            if(!
    filter_var($stringFILTER_VALIDATE_EMAIL))    
                                {
                                    return 
    FALSE;    
                                }
                        }
                }
        
        }

    $object = new Validator();
    $string 'ben@yahoo.com';
    $type 'email';
    if(
    $object->validate($string$type))
        {
            echo 
    "Valid";    
        }
    else
        {
            echo 
    "Not Valid";    
        }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    because that piece of code isn't finished. What if filter_var() does succeed like it's supposed to do in your case? You don't seem to have considered that case in your if statement.

    That's why it's usually a good idea to complete the code before starting to test it.

    You should actually get rid of those unnecessary if statements and use the return value of filter_var() directly:

    PHP Code:
    <?php  

    class Validator 
        

            public function 
    validate($string$type
                { 
                    if(
    $type == 'email'
                        return 
    filter_var($stringFILTER_VALIDATE_EMAIL) !== false;
                } 
         
        }
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    filter_var changes the data and returns the changed data. It does not return a boolean.

    PHP Code:
                            if(filter_var($stringFILTER_VALIDATE_EMAIL) != $string
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by ManiacDan
    filter_var changes the data and returns the changed data. It does not return a boolean.
    filter_var() with FILTER_VALIDATE_EMAIL returns false if the validation failed and the original string if it succeeded.

    You seem to be confusing this with the FILTER_SANITIZE constants. Those do in fact make the function change the input.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo