Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

madprabh's avatar

Best way to sanitize user input?

Hey Folks,

So I am building a public facing form feature where people would be required to input data into text fields and paragraph. Think survey.

What is the best way to sanitize user input for junk amnd special characters so that when I ultimately want to display the data on some gird the special characters wouldn't cause any problems.

Any pointers here will help me a lot! Thanks in advance.

0 likes
3 replies
LaryAI's avatar
Level 58

To sanitize user input effectively, you need to consider both sanitization and validation. Sanitization ensures that the input is safe to store and display, while validation ensures that the input meets your application's requirements.

Here are some steps and code examples to help you sanitize user input in a PHP application:

  1. Validation: Ensure the input meets your criteria (e.g., length, format).
  2. Sanitization: Remove or encode any potentially harmful characters.

Step 1: Validation

Use PHP's built-in functions to validate the input. For example, you can use filter_var for basic validation.

$input = $_POST['user_input'];

// Validate email
if (!filter_var($input, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format";
}

// Validate URL
if (!filter_var($input, FILTER_VALIDATE_URL)) {
    echo "Invalid URL format";
}

// Validate integer
if (!filter_var($input, FILTER_VALIDATE_INT)) {
    echo "Invalid integer format";
}

Step 2: Sanitization

Use PHP's filter_var with the appropriate filter to sanitize the input. For text fields and paragraphs, you can use FILTER_SANITIZE_STRING or htmlspecialchars.

$input = $_POST['user_input'];

// Sanitize string
$sanitized_input = filter_var($input, FILTER_SANITIZE_STRING);

// Alternatively, use htmlspecialchars to convert special characters to HTML entities
$sanitized_input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

Example: Combining Validation and Sanitization

Here's a complete example that combines both validation and sanitization for a text input:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $input = $_POST['user_input'];

    // Validate input (e.g., ensure it's not empty and has a reasonable length)
    if (empty($input)) {
        echo "Input is required";
    } elseif (strlen($input) > 255) {
        echo "Input is too long";
    } else {
        // Sanitize input
        $sanitized_input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
        
        // Now you can safely store or display $sanitized_input
        echo "Sanitized input: " . $sanitized_input;
    }
}

Additional Tips

  • Use Prepared Statements: When inserting user input into a database, always use prepared statements to prevent SQL injection.
  • Escape Output: When displaying user input, always escape it to prevent XSS attacks. This can be done using htmlspecialchars as shown above.
  • Use a Framework: If you're using a framework like Laravel, it provides built-in validation and sanitization methods that you can leverage.

By following these steps, you can ensure that user input is both validated and sanitized, making your application more secure and robust.

aquinoaldair's avatar

To sanitize text in Laravel in a simple way, you can do two key things:

  1. Validate the input: Use the request->validate() method to ensure that what the user sends is what you expect. For example, if it's a text field:

    $request->validate([
        'field' => 'required|string|max:255',
    ]);
    
    

This makes sure that what you're getting is a string, it's not empty, and it's no longer than 255 characters.

  1. Clean the HTML with Purifier: If the user can enter HTML, like in a text editor, it’s a good idea to clean that content. You can use the HTMLPurifier library for this. Install it with Composer and apply it like this:
composer require mews/purifier
  $clean_html = Purifier::clean($request->input('field'));

This will strip out any harmful tags or code but leave the good HTML (if you allow it).

With these two steps, you’ve got the basics of sanitizing text in Laravel covered.

madprabh's avatar

@aquinoaldair Thanks for your reply. I also want to be able to handle special characters. For example, whemn I have something like this in my text area

This is good "&****" 

and when I try to parse this as a json value it breaks my html table (because of the double quotes). Any ideas how to handle something like this?

Please or to participate in this conversation.