Are there specific best practices for defining and assigning strings in PHP code for contact forms?
When defining and assigning strings in PHP code for contact forms, it is best practice to use descriptive variable names and to sanitize user input to prevent potential security vulnerabilities such as SQL injection or cross-site scripting attacks. It is also recommended to use constants for strings that are reused throughout the codebase to improve maintainability.
<?php
// Define and assign strings for contact form
define('CONTACT_FORM_SUBJECT', 'New Contact Form Submission');
$to = 'contact@example.com';
$headers = 'From: webmaster@example.com';
// Sanitize user input
$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = htmlspecialchars($_POST['message']);
// Send email
mail($to, CONTACT_FORM_SUBJECT, $message, $headers);
?>
Related questions
-
PHP array_merge array_unique
-
mysql_ extension PHP
-
PHP functions libraries