Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Filters

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 112
    Comment on it

    Welcome to Findnerd. We are going to discuss PHP filtering today. We all knows version by version PHP introducing new ways of coding and many improvements. PHP has launched the PHP7. There are many new features added as well as did many improvement. We need to return back to the filters which introduced in PHP5.2. Filtering is useful to Sanitizing the data as well as validation of the data. We use the different types of validation in web form.

    Sanitize will remove the unwanted characters from the data and you can also validate it after that. Lets take an small example of email. Email has its own formatting. Am i right?. Like this demo@domain.com. Firstly we have to remove the unwanted characters from the string and then validate it that it is exactly an email or not.

    FILTER_SANITIZE_EMAIL will be used to sanitize the data and FILTER_VALIDATE_EMAIL will be used as a validator. Please have a look.

    <html>
    <head>
    </head>
    <body>
    <form name="findnerd_frm" method="post">
       <p> Name:</p>
        <input type="text" name="findnerd_name" value="<?php echo $_POST['findnerd_name']; ?>" size="50" /><br/><br/>
       <p> Email Address:  </p>
        <input type="text" name="findnerd_email" value="<?php echo $_POST['findnerd_email']; ?>" size="50"/> <br/><br/>
        <input type="submit" name="proceed" value="Continue" />
    </form>
    </body>
    </html>
    

    You can see very simple example which have two fields, one is name and email. we have to vaildate the name as well as email. Lets start with name. you can remove the unwanted character via sanitize but you can not validate it as a string. you need to check if it is blank or not after removal.

    <?php
    $error = array();
    if(isset($_POST['proceed'])){
    <-- name validation -->
    if($_POST['findnerd_name']!=''){
    $_POST['findnerd_name'] = filter_var($_POST['findnerd_name'], FILTER_SANITIZE_STRING);
    if($_POST['findnerd_name']==''){
    $error['name'] = "Please enter the valid characters";
    }
    }else{
    $error['name'] = "Please enter the name first";
    }
    
    <-- email validation -->
    if($_POST['findnerd_name']!=''){
    $email = filter_var($_POST['findnerd_email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error['email'] = "Please enter vaild email";
    }
    }else{
    $error['email'] = "Please enter email";
    }
    
    
    }
    ?>
    

    We have also mentioned the email sanitize as well as validition. You need to use the FILTER_SANITIZE_EMAIL for the sanitize and FILTER_VALIDATE_EMAIL for validation. You can also use the other filter ids for other data formation.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: