get_setting('subscribe_custom1_label'), 'custom2' => get_setting('subscribe_custom2_label'), 'custom3' => get_setting('subscribe_custom3_label'), ]; $useCaptcha = get_setting('subscribe_captcha', '0') === '1'; // Generate a fresh captcha question if needed (stored in session) function captcha_generate(): void { $a = random_int(1, 12); $b = random_int(1, 12); $_SESSION['captcha_answer'] = $a + $b; $_SESSION['captcha_q'] = "$a + $b"; } if ($useCaptcha && empty($_SESSION['captcha_answer'])) { captcha_generate(); } $error = ''; $success = false; if ($_SERVER['REQUEST_METHOD'] === 'POST') { verify_csrf(); // Captcha check if ($useCaptcha) { $given = (int)trim($_POST['captcha'] ?? ''); $expect = (int)($_SESSION['captcha_answer'] ?? -1); if ($given !== $expect) { $error = 'Incorrect answer to the security question. Please try again.'; captcha_generate(); // refresh question after wrong answer } } if (!$error) { $email = trim(strtolower($_POST['email'] ?? '')); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error = 'Please enter a valid email address.'; } else { $row = db()->prepare("SELECT id FROM subscribers WHERE email = ?"); $row->execute([$email]); if ($row->fetch()) { $error = 'This email address is already subscribed.'; } else { $ipHash = hash('sha256', ($_SERVER['REMOTE_ADDR'] ?? '') . date('Y-m-d')); $data = [ 'email' => $email, 'first_name' => trim($_POST['first_name'] ?? ''), 'last_name' => trim($_POST['last_name'] ?? ''), 'zip_code' => trim($_POST['zip_code'] ?? ''), 'phone' => trim($_POST['phone'] ?? ''), 'comments' => trim($_POST['comments'] ?? ''), 'custom1' => trim($_POST['custom1'] ?? ''), 'custom2' => trim($_POST['custom2'] ?? ''), 'custom3' => trim($_POST['custom3'] ?? ''), 'ip_hash' => $ipHash, ]; db()->prepare( "INSERT INTO subscribers (email,first_name,last_name,zip_code,phone,comments,custom1,custom2,custom3,ip_hash) VALUES (:email,:first_name,:last_name,:zip_code,:phone,:comments,:custom1,:custom2,:custom3,:ip_hash)" )->execute($data); // Clear captcha from session on success unset($_SESSION['captcha_answer'], $_SESSION['captcha_q']); $success = true; // Notify opted-in admins/editors $displayName = trim(($data['first_name'] . ' ' . $data['last_name'])); notify_new_subscriber($email, $displayName); } } } // Refresh captcha question if there was any error and captcha is on if ($error && $useCaptcha && empty($_SESSION['captcha_answer'])) { captcha_generate(); } } $meta = build_meta([ 'title' => get_setting('subscribe_title', 'Subscribe') . ' — ' . get_setting('site_title', SITE_NAME), 'og_type' => 'website', ]); include ROOT_PATH . '/includes/header.php'; ?>
✓ You have been subscribed successfully. Thank you!

← Back to Home

What is ?
Cancel