PHP Forms: Validation and Sanitization Essentials Quiz Quiz

This quiz covers essential PHP form validation and sanitization techniques, focusing on user input handling, common functions, and best practices. Assess your understanding of securing and validating form data, preventing errors, and building safer web applications with PHP.

  1. Basic Understanding of Form Validation

    Which PHP function is most commonly used to check if a required field from a form has been submitted and is not empty?

    1. validate()
    2. empty()
    3. isset()
    4. check_submit()

    Explanation: The empty() function checks if a value is empty, making it ideal for validating required fields. While isset() only checks if a variable is set, it does not verify if it is empty or not. The functions validate() and check_submit() do not exist in standard PHP and are thus incorrect choices. Always use empty() to ensure the user has entered some data.

  2. Sanitizing Email Input

    Suppose a PHP script receives an email address from a form. Which built-in function best sanitizes the input before validation or use?

    1. clean_input()
    2. sanitize_email()
    3. filter_var()
    4. htmlentities()

    Explanation: filter_var() with the FILTER_SANITIZE_EMAIL flag is designed to sanitize email inputs by removing illegal characters. The function sanitize_email() does not exist in standard PHP, and clean_input() is not built in. htmlentities() converts special characters to HTML entities but is not ideal for sanitizing emails, making filter_var() the best choice.

  3. Preventing XSS in User Input

    When displaying user input from a PHP form back to the page, which function helps prevent cross-site scripting (XSS)?

    1. htmlspecialchars()
    2. trim()
    3. isset()
    4. addslashes()

    Explanation: htmlspecialchars() converts special characters to HTML entities, which protects against XSS attacks when showing user input in the browser. The trim() function merely removes whitespace, while isset() checks if a variable is set but does not sanitize input. addslashes() adds slashes to escape certain characters but is not sufficient to prevent XSS.

  4. Validating Numbers from a Form

    If a user submits their age through a form, which function is best suited to ensure the input is a valid integer in PHP?

    1. filter_var() with FILTER_VALIDATE_INT
    2. number_check()
    3. strval()
    4. is_number()

    Explanation: filter_var() used with the FILTER_VALIDATE_INT flag checks that the input is a valid integer. is_number() and number_check() are not built-in PHP functions. strval() converts a value to a string and does not validate whether input is an integer, making filter_var() with FILTER_VALIDATE_INT the correct approach.

  5. Trimming White Space

    Which function removes unnecessary spaces, tabs, and newlines from the beginning and end of input strings in PHP?

    1. clearspace()
    2. string_strip()
    3. remove_blanks()
    4. trim()

    Explanation: The trim() function efficiently strips whitespace characters from both ends of a string. clearspace(), string_strip(), and remove_blanks() are not standard PHP functions, so they would not work for this purpose. Using trim() ensures that extra spaces do not cause validation issues.

  6. Handling HTML Form Data

    What is the primary purpose of the htmlspecialchars() function when processing form data in PHP?

    1. To validate numeric input
    2. To hash passwords for storage
    3. To convert special characters to HTML entities
    4. To encrypt submitted form data

    Explanation: htmlspecialchars() converts special characters, like less-than and greater-than signs, to HTML entities, which helps prevent breaking HTML or XSS attacks. It does not hash passwords or encrypt data, and it does not validate numbers, which are features outside its purpose. The main use is for safe display of user input.

  7. Checking Submitted Data

    When using the POST method in PHP, how can you check if a field named 'username' was submitted from a form?

    1. validate('username')
    2. isset($_GET['username'])
    3. checkfield('username')
    4. isset($_POST['username'])

    Explanation: Using isset($_POST['username']) checks if the username field was submitted via POST and is set. $_GET['username'] would only work with the GET method, not POST. checkfield() and validate() are not native PHP functions, so they are invalid in this context.

  8. Sanitizing String Input

    Which function is recommended for removing or encoding undesired characters from a general string input in PHP?

    1. sanitize_var()
    2. parse_str()
    3. filter_var() with FILTER_SANITIZE_STRING
    4. clean_string()

    Explanation: filter_var() with FILTER_SANITIZE_STRING is designed to strip tags and remove or encode unwanted characters from string data. clean_string() and sanitize_var() are not built-in functions. parse_str() parses query strings into variables, not for sanitizing random string inputs.

  9. Validating Email Format

    Which PHP filter should be used with filter_var() to validate the format of an email address submitted through a form?

    1. FILTER_VALIDATE_EMAIL
    2. EMAIL_VALIDATE_FILTER
    3. VALIDATE_EMAIL
    4. FILTER_SANITIZE_EMAIL

    Explanation: FILTER_VALIDATE_EMAIL is the correct filter for validating whether a given value matches standard email formatting rules. EMAIL_VALIDATE_FILTER and VALIDATE_EMAIL are incorrect filter names, and FILTER_SANITIZE_EMAIL is used for sanitizing, not validating, email addresses.

  10. Handling Arrays in Form Inputs

    If a form allows users to select multiple options named 'colors[]', how should you access this data safely in PHP?

    1. Accessing $_POST_colors as a variable
    2. Directly using $_POST['colors'] as a string
    3. By checking if is_array($_POST['colors']) and then processing the values
    4. Using $_POST-u003Ecolors to retrieve values

    Explanation: Using is_array($_POST['colors']) ensures that the submitted data is in array form, which is important for multi-select inputs, before processing it. Treating $_POST['colors'] as a string may result in errors if multiple values are submitted. $_POST_colors and $_POST-u003Ecolors are invalid ways to reference POST data in PHP.