PHP Form Validation

PHP Form Validation

File handling is an important part of any web application. You often need to open and process a file for different tasks.

Validation means check the input submitted by the user. There are two types of validation are available in PHP. They are as follows:

  • Client-Side Validation −Validation is performed on the client machine web browsers.
  • Server Side Validation −After submitted by data, The data has sent to a server and perform validation checks in server machine.

The validation rules for the form above are as follows:

Field Validation Rules
NameRequired. + Must only contain letters and whitespace
E-mailRequired. + Must contain a valid email address (with @ and .)
WebsiteOptional. If present, it must contain a valid URL
CommentOptional. Multi-line input field (textarea)
GenderRequired. Must select one

Text Fields

The name, email, and website fields are text input elements, and the comment field is a textarea. The HTML code looks like this:

  •       
                            Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    Website: <input type="text" name="website"><br>
    Comment: <textarea name="comment" rows="5" cols="40">>/textarea>
                        

    Radio Buttons

    The gender fields are radio buttons and the HTML code looks like this:

  •       
                            Gender:
    <input type="radio" name="gender" value="female">Female
    <input type="radio" name="gender" value="male">Male
    <input type="radio" name="gender" value="other">Other
                        

    The Form Element

    When the form is submitted, the form data is sent with method="post".

    The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script. So, the $_SERVER["PHP_SELF"] sends the submitted form data to the page itself, instead of jumping to a different page. This way, the user will get error messages on the same page as the form.

    The htmlspecialchars() function converts special characters to HTML entities. This means that it will replace HTML characters like < and > with < and >. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in forms.

  •       
         <form method="post" action=" lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    

    PHP Form Security

    The $_SERVER["PHP_SELF"] variable can be used by hackers! If PHP_SELF is used in your page then a user can enter a slash (/) and then some Cross Site Scripting (XSS) commands to execute.

    Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users.Assume we have the following form in a page named "test_form.php":

    Now, if a user enters the normal URL in the address bar like "http://www.example.com/test_form.php", the above code will be translated to:

  •       
                            <form method="post" action="test_form.php">
                        
                        

    However, consider that a user enters the following URL in the address bar: http://www.example.com/test_form.php/ %22%3E%3Cscript%3Ealert('hacked')%3C/script%3E In this case, the above code will be translated to:

  •       
    <form method="post" action="test_form.php/"><script>alert('hacked')</script>
    

    This code adds a script tag and an alert command. And when the page loads, the JavaScript code will be executed (the user will see an alert box). This is just a simple and harmless example how the PHP_SELF variable can be exploited.

    Be aware of that any JavaScript code can be added inside the <script> tag! A hacker can redirect the user to a file on another server, and that file can hold malicious code that can alter the global variables or submit the form to another address to save the user data, for example.

    How To Avoid $_SERVER["PHP_SELF"] Exploits?

    $_SERVER["PHP_SELF"] exploits can be avoided by using the htmlspecialchars( ) function.

  •       
                <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            

    The htmlspecialchars() function converts special characters to HTML entities. Now if the user tries to exploit the PHP_SELF variable, it will result in the following output:

    Validate Form Data With PHP

    The first thing we will do is to pass all variables through PHP's htmlspecialchars() function. When we use the htmlspecialchars() function; then if a user tries to submit the following in a text field:

    <script>location.href('http://www.hacked.com')</script> -:

    this would not be executed, because it would be saved as HTML escaped code, like this:

    <script>location.href('http://www.hacked.com')</script>

    The code is now safe to be displayed on a page or inside an e-mail. We will also do two more things when the user submits the form.

    We will also do two more things when the user submits the form:

    • Strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim( ) function)
    • Remove backslashes (\) from the user input data (with the PHP stripslashes() function)

    The next step is to create a function that will do all the checking for us (which is much more convenient than writing the same code over and over again). We will name the function test_input(). Now, we can check each $_POST variable with the test_input() function, and the script looks like this:

  •       
                            <?php
     define variables and set to empty values
    $name = $email = $gender = $comment = $website = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = test_input($_POST["name"]);
        $email = test_input($_POST["email"]);
        $website = test_input($_POST["website"]);
        $comment = test_input($_POST["comment"]);
        $gender = test_input($_POST["gender"]);
    }
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>
                        

    Notice that at the start of the script, we check whether the form has been submitted using $_SERVER["REQUEST_METHOD"]. If the REQUEST_METHOD is POST, then the form has been submitted - and it should be validated. If it has not been submitted, skip the validation and display a blank form.

    However, in the example above, all input fields are optional. The script works fine even if the user does not enter any data.

    Required Fields

    In the following code we have added some new variables: $nameErr, $emailErr, $genderErr, and $websiteErr. These error variables will hold error messages for the required fields. We have also added an if else statement for each $_POST variable. This checks if the $_POST variable is empty (with the PHP empty() function). If it is empty, an error message is stored in the different error variables, and if it is not empty, it sends the user input data through the test_input() function:

  •       
    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
        $nameErr = "Name is required";
        } else {
        $name = test_input($_POST["name"]);
        }
    
        if (empty($_POST["email"])) {
        $emailErr = "Email is required";
        } else {
        $email = test_input($_POST["email"]);
        }
    
        if (empty($_POST["website"])) {
        $website = "";
        } else {
        $website = test_input($_POST["website"]);
        }
    
        if (empty($_POST["comment"])) {
        $comment = "";
        } else {
        $comment = test_input($_POST["comment"]);
        }
    
        if (empty($_POST["gender"])) {
        $genderErr = "Gender is required";
        } else {
        $gender = test_input($_POST["gender"]);
        }
    }
    ?>
                        

    PHP - Display The Error Messages

    Then in the HTML form, we add a little script after each required field, which generates the correct error message if needed (that is if the user tries to submit the form without filling out the required fields):

  •       
                            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    
        Name: <input type="text" name="name">
        <span class="error">* <?php echo $nameErr;?></span>
        

    E-mail: <input type="text" name="email"< <span class="error">* <?php echo $emailErr;?<</span>

    Website: <input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span>

    <textarea name="comment" rows="5" cols="40"></textarea>

    Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <input type="radio" name="gender" value="other">Other <span class="error">* <?php echo $genderErr;?></span>

    <input type="submit" name="submit" value="Submit"> </form> ?>

    The next step is to validate the input data, that is "Does the Name field contain only letters and whitespace?", and "Does the E-mail field contain a valid e-mail address syntax?", and if filled out, "Does the Website field contain a valid URL?".

    PHP - Validate Name

    The code below shows a simple way to check if the name field only contains letters, dashes, apostrophes and whitespaces. If the value of the name field is not valid, then store an error message. The preg_match() function searches a string for pattern, returning true if the pattern exists, and false otherwise.

  •       
                            $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
        $nameErr = "Only letters and white space allowed";
    }
                        

    PHP - Validate E-mail

    The easiest and safest way to check whether an email address is well-formed is to use PHP's filter_var() function. In the code below, if the e-mail address is not well-formed, then store an error message:

  •       
                            $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email format";
    }
    
                        

    PHP - Validate URL

    The code below shows a way to check if a URL address syntax is valid (this regular expression also allows dashes in the URL). If the URL address syntax is not valid, then store an error message:

  •       
                             
    $website = test_input($_POST["website"]);
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)
    [-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
        $websiteErr = "Invalid URL";
    }
    
                        

    PHP - Validate Name, E-mail, and URL

  •       
                             
    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
        $nameErr = "Name is required";
        } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed";
        }
        }
    
        if (empty($_POST["email"])) {
        $emailErr = "Email is required";
        } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format";
        }
        }
    
        if (empty($_POST["website"])) {
        $website = "";
        } else {
        $website = test_input($_POST["website"]);
        // check if URL address syntax is valid
         (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)
        [-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
            $websiteErr = "Invalid URL";
        }
        }
    
        if (empty($_POST["comment"])) {
        $comment = "";
        } else {
        $comment = test_input($_POST["comment"]);
        }
    
        if (empty($_POST["gender"])) {
        $genderErr = "Gender is required";
        } else {
        $gender = test_input($_POST["gender"]);
        }
    }
    ?>
    
                        

    PHP - Keep The Values in The Form

    To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the <textarea> and </textarea> tags. The little script outputs the value of the $name, $email, $website, and $comment variables.

    Then, we also need to show which radio button that was checked. For this, we must manipulate the checked attribute (not the value attribute for radio buttons):

  •       
                            <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
        $nameErr = "Name is required";
        } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed";
        }
        }
        
        if (empty($_POST["email"])) {
        $emailErr = "Email is required";
        } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format";
        }
        }
        
        if (empty($_POST["website"])) {
        $website = "";
        } else {
        $website = test_input($_POST["website"]);
        // check if URL address syntax is valid (this regular
         expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)
        [-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
            $websiteErr = "Invalid URL";
        }
        }
    
        if (empty($_POST["comment"])) {
        $comment = "";
        } else {
        $comment = test_input($_POST["comment"]);
        }
    
        if (empty($_POST["gender"])) {
        $genderErr = "Gender is required";
        } else {
        $gender = test_input($_POST["gender"]);
        }
    }
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1