What common error might occur when implementing email confirmation in PHP?
One common error that might occur when implementing email confirmation in PHP is not properly sanitizing and validating user input before sending the confirmation email. This can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To solve this issue, always sanitize and validate user input to prevent any malicious code from being executed.
// Sanitize and validate user input
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
// Send confirmation email
$to = $email;
$subject = 'Email Confirmation';
$message = 'Please click the link below to confirm your email address: http://www.example.com/confirm.php?email=' . $email;
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);