How to Validate and Sanitise PHP Form Input for Security


Validating and sanitising form input is essential for securing PHP applications.
Proper handling of user data helps prevent common vulnerabilities such as SQL injection and XSS (Cross-Site Scripting) attacks.

In this tutorial, you’ll learn how to validate and sanitise form input to ensure your application is secure and reliable.



1. Importance of Validation and Sanitisation


  • Validation: Ensures that the input meets specific criteria (e.g., correct format, required fields are not empty).
  • Sanitisation: Cleans input data to remove harmful characters, preventing malicious input from being executed.


Validation and sanitisation work together to protect your application from attacks and ensure user data is correctly processed.



2. Basic Validation Example


Here’s how to validate form input in PHP:

PHP Code
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  3. $name = trim($_POST['name']); // Remove extra spaces
  4. $email = trim($_POST['email']); // Remove extra spaces
  5.  
  6. if (empty($name)) {
  7. echo "Name is required.";
  8. } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  9. echo "Invalid email format.";
  10. } else {
  11. echo "Validation passed!";
  12. }
  13. }
  14. ?>


  • trim(): Removes unnecessary spaces from user input.
  • empty(): Checks if a field is empty.
  • filter_var(): Validates input against predefined filters, such as email format.




3. Basic Sanitisation Example


Sanitisation cleans user input to ensure it is safe to use. Here’s an example:

PHP Code
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  3. $name = htmlspecialchars($_POST['name']); // Converts HTML characters to prevent XSS
  4. $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); // Removes illegal characters from email
  5.  
  6. echo "Sanitised Name: $name";
  7. echo "Sanitised Email: $email";
  8. }
  9. ?>


  • htmlspecialchars(): Converts special characters to HTML entities to prevent XSS.
  • filter_var(): Sanitises input using predefined filters like FILTER_SANITIZE_EMAIL.




4. Preventing SQL Injection


When working with databases, always use prepared statements to prevent SQL injection:

PHP Code
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  3. $connection = new mysqli('localhost', 'user', 'password', 'database');
  4. $stmt = $connection->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
  5. $stmt->bind_param("ss", $name, $email);
  6.  
  7. $name = trim($_POST['name']);
  8. $email = trim($_POST['email']);
  9.  
  10. $stmt->execute();
  11. $stmt->close();
  12. $connection->close();
  13. }
  14. ?>


  • Prepared Statements: Use placeholders ("?") and bind parameters to securely handle user input.
  • mysqli_real_escape_string(): Use if you cannot implement prepared statements.




5. Combining Validation and Sanitisation


Here’s an example combining both validation and sanitisation:

PHP Code
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  3. $name = htmlspecialchars(trim($_POST['name']));
  4. $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
  5.  
  6. if (empty($name)) {
  7. echo "Name is required.";
  8. } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  9. echo "Invalid email format.";
  10. } else {
  11. // Process the data (e.g., store in database)
  12. echo "Input is valid and safe.";
  13. }
  14. }
  15. ?>




Conclusion


Validation and sanitisation are critical for securing web applications. By implementing these techniques, you can protect your application from common vulnerabilities and ensure reliable data handling.
Always combine validation, sanitisation, and secure database practices for robust security.
AI's Avatar
Author:
Views:
41
Rating:
There are currently no comments for this tutorial, login or register to leave one.