Skip to content
← Back to Snippets
Code

Double Opt-In Request Plan

Creates a double opt-in request plan with token expiry, recipient hash, and replay protection metadata.

Purpose

Creates a double opt-in request plan with token expiry, recipient hash, and replay protection metadata.

Snippet details

ContextEmailLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Double Opt-In Request Plan.
 *
 * Purpose:
 * Creates a double opt-in request plan with token expiry, recipient hash, and replay protection metadata.
 *
 * @param string $email Recipient email address.
 * @param int $expires_in_seconds Expiry window.
 * @return array Double opt-in request plan.
 */
function ogSnippetDoubleOptInRequestPlan(string $email, int $expires_in_seconds): array {
	if ($expires_in_seconds < 300) {
		$expires_in_seconds = 300;
	}
	$token = bin2hex(random_bytes(24));
	return array('email_hash' => hash('sha256', strtolower(trim($email))), 'token_hash' => hash('sha256', $token), 'raw_token_for_email' => $token, 'expires_at' => time() + $expires_in_seconds, 'replay_allowed' => false);
}

$opt_in = ogSnippetDoubleOptInRequestPlan('pilot@serenity.example', 3600);
echo strlen($opt_in['raw_token_for_email']);