What are the potential risks of not validating form data on the server side in PHP, especially in the context of contact forms?

Not validating form data on the server side in PHP can lead to security vulnerabilities such as SQL injection, cross-site scripting (XSS), and other forms of attacks. To mitigate these risks, it is crucial to validate and sanitize user input on the server side before processing it.

// Validate and sanitize form data on the server side
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';
    $email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_SANITIZE_EMAIL) : '';
    $message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '';

    // Additional validation logic can be added here

    // Process the validated data
    // For example, sending an email or saving to a database
}