'', 'email' => '', 'message' => '']; // Start session for CSRF token and rate limiting if (session_status() === PHP_SESSION_NONE) { session_start(); } // Generate CSRF token if not exists if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } // Process form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['contact_form_submit'])) { $formSubmitted = true; // Security: CSRF Token Validation if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) { $formErrors[] = 'Invalid security code. Please try again.'; } // Spam Prevention 1: Honeypot field (should be empty) if (!empty($_POST['website'])) { $formErrors[] = 'Spam detected.'; } // Spam Prevention 2: Time-based check (form must be visible for at least 3 seconds) $formStartTime = isset($_POST['form_start_time']) ? (int)$_POST['form_start_time'] : 0; $timeDiff = time() - $formStartTime; if ($timeDiff < 3) { $formErrors[] = 'Form submitted too quickly.'; } // Spam Prevention 3: Referrer check (only if referrer is present and clearly from different domain) if (!empty($_SERVER['HTTP_REFERER'])) { $referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); $currentHost = $_SERVER['HTTP_HOST']; // Only block if referrer exists and doesn't match (allows empty referrer for privacy browsers) if ($referrer && $referrer !== $currentHost && $referrer !== 'localhost') { $formErrors[] = 'Invalid form submission.'; } } // Spam Prevention 4: Rate limiting (session-based) $lastSubmitTime = isset($_SESSION['last_contact_submit']) ? $_SESSION['last_contact_submit'] : 0; if (time() - $lastSubmitTime < 60) { $formErrors[] = 'Please wait a bit before submitting again.'; } // Get and sanitize form data $formData['name'] = trim($_POST['name'] ?? ''); $formData['email'] = trim($_POST['email'] ?? ''); // Normalize line endings in message (convert \r\n to \n) $formData['message'] = trim(str_replace("\r\n", "\n", $_POST['message'] ?? '')); // Validation if (empty($formData['name'])) { $formErrors[] = 'Please provide your name.'; } elseif (strlen($formData['name']) > 100) { $formErrors[] = 'The name is too long.'; } if (empty($formData['email'])) { $formErrors[] = 'Please provide your email address.'; } elseif (!filter_var($formData['email'], FILTER_VALIDATE_EMAIL)) { $formErrors[] = 'Invalid email address.'; } elseif (strlen($formData['email']) > 100) { $formErrors[] = 'The email address is too long.'; } if (empty($formData['message'])) { $formErrors[] = 'Please write a message.'; } elseif (strlen($formData['message']) < 10) { $formErrors[] = 'The message is too short (minimum 10 characters).'; } elseif (strlen($formData['message']) > 5000) { $formErrors[] = 'The message is too long (maximum 5000 characters).'; } // Spam Prevention 5: Check for suspicious patterns $spamPatterns = [ '/\[url=/i', '/\[link=/i', '/debug = 1; // Uncomment to see SMTP debug output $mail->SetSMTPhost($smtpConfig['host']); $mail->SetSMTPport($smtpConfig['port']); $mail->SetSMTPuser($smtpConfig['username']); $mail->SetSMTPpass($smtpConfig['password']); $mail->SetSender([$smtpConfig['from_email'] => $smtpConfig['from_name']]); $mail->AddRecipient([$smtpConfig['to_email'] => $smtpConfig['to_name']]); $mail->AddReplyTo([$formData['email'] => $formData['name']]); $mail->SetSubject('New inquiry from contact form'); $mail->SetBodyText($emailBody); // Capture any output from PHPMailer.Lite (it might exit() on error) ob_start(); $mailSent = @$mail->Send('smtp'); $smtpOutput = ob_get_clean(); // Check if there was an error in the output if (!$mailSent || stripos($smtpOutput, 'error') !== false || stripos($smtpOutput, '✗') !== false) { $mailSent = false; error_log("SMTP Send failed. Output: " . strip_tags($smtpOutput)); $formErrors[] = 'An error occurred while sending the message. Please try again later.'; } } catch (\Exception $e) { $mailSent = false; error_log("PHPMailer Exception: " . $e->getMessage()); $formErrors[] = 'An error occurred while sending the message. Please try again later.'; } } } else { // Fallback to native mail() function $headers = "From: kontaktskjema@stopplidelsen.no\r\n"; $headers .= "Reply-To: " . $formData['email'] . "\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; $mailSent = mail('ruben@stopplidelsen.no', 'New inquiry from contact form', $emailBody, $headers); } if ($mailSent) { $formSuccess = true; $_SESSION['last_contact_submit'] = time(); // Clear form data on success $formData = ['name' => '', 'email' => '', 'message' => '']; } else { if (empty($formErrors)) { $formErrors[] = 'An error occurred while sending the message. Please try again later.'; } } } } // Generate form start time token $currentTime = time(); ?>

Contact Form

Do you have questions, suggestions or want to participate in our work? Fill out the form below! But remember that it may take time before you get a response, we are not many and run everything on a voluntary basis.

Thank you for your inquiry!

We have received your message and will respond as soon as possible.

Please correct the following:

Minimum 10 characters, maximum 5000 characters.